开发者

How to use jQuery conditional statement and event handling to make an element fade out and fade in depending on browser width?

开发者 https://www.devze.com 2023-03-04 14:51 出处:网络
I want to check the brows开发者_StackOverflow社区er width, and if it\'s more than 850, to fade in a div.

I want to check the brows开发者_StackOverflow社区er width, and if it's more than 850, to fade in a div.

Then whenever the browser is resized, I want to fade out that div if the browser width is less than 850, and fade it in again if the browser width is more than 850.

Thank you in advance for helping out with this!

Dimitri Vorontzov


As far as I know, you can use the $(window).width() (doc) to get the browser's width. Use the resize event to handle the changes in browser size. (doc)

The resize event is sent to the window element when the size of the browser window changes:

$(window).resize(function() {
    if ($(window).width() > 850) {
         // do something 
    } else {
         // do something else
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消