How do I get CSS mediaqueries to work with jQuery $(window).innerWidth()?

后端 未结 5 1384
再見小時候
再見小時候 2020-12-09 04:13

I\'m trying to let jQuery take care of a menu, while CSS does something to the window background when resizing the browser.

So jQuery does something like this:

5条回答
  •  一个人的身影
    2020-12-09 05:07

    If you happen to be using Modernizr, you can include the media query polyfill, which simplifies media query checks to:

    if (Modernizr.mq('all and (max-width: 966px)')) {
        ...
    }
    

    Which is conveniently identical to your CSS:

    @media all and (max-width: 966px) {
        ...
    }
    

    If you can't use Modernizr's polyfill, then stick with checking against Math.max(document.width, window.innerWidth).

提交回复
热议问题