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

后端 未结 5 1381
再見小時候
再見小時候 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 04:53

    window.matchMedia is a way you can fire events when media selectors kick on or off. It's new so not widely supported, but seems pretty useful.

    "Using media queries from code" from MDN

    0 讨论(0)
  • 2020-12-09 04:53
    var viewport = jQuery(window).innerWidth();
    
    if( viewport > 979 )
    {
      // your code here
    }
    
    0 讨论(0)
  • 2020-12-09 04:58

    In addition to the solution of JackieChiles:

    Using

    var width = Math.max( $(window).width(), window.innerWidth);
    will always get you the width without scrollbars in any browser.

    This solution is (IMHO) better because the JS does not depend on any CSS.

    Thanks to JackieChiles and Arjen for this solution!

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-09 05:09

    My solution to triggering JavaScript at the exact same time as media queries (even in the face of device or browser quirks as seen here) is to trigger my window.resize logic in response to a change that the media query made. Here's an example:

    CSS

        #my-div
        {
            width: 100px;
        }
    
        @media all and (max-width: 966px)
        {
            #my-div
            {
                width: 255px;
            }
        }
    

    JavaScript

        var myDivWidth;
    
        $(document).ready(function() {
            myDivWidth = $('#myDiv').width();
    
            $(window).resize(function () {
                if ($('#myDiv').width() != myDivWidth) {
                    //If the media query has been triggered
                    myDivWidth = $('#myDiv').width();
                    //Your resize logic here (the jQuery menu stuff in your case)
                }
            });
        });
    

    In this example, whenever #myDiv changes size (which will occur when the media query is triggered), your jQuery resize logic will also be run.

    For simplicity I used an element width, but you could easily use whatever property you are changing, such as the body background.

    Another advantage of this approach is that it keeps the window.resize function as lightweight as possible, which is always good because it is called every single time the window size changes by a single pixel (in most modern browsers anyway).

    The bug that you encountered seems to me to be a browser-specific problem, as you said. Although it seems incorrect intuitively, Firefox (and other browsers with the issue) actually seems to match the W3C recommendation for media queries more closely than the Webkit browsers. The recommendation states that the viewport width includes scrollbars, and JavaScript window width does not seem to include scrollbars, hence the disrepancy.

    0 讨论(0)
提交回复
热议问题