Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>window.resize</code> logic in response to a change that the media query made. Here's an example:</p> <p><strong>CSS</strong></p> <pre><code> #my-div { width: 100px; } @media all and (max-width: 966px) { #my-div { width: 255px; } } </code></pre> <p><strong>JavaScript</strong></p> <pre><code> 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) } }); }); </code></pre> <p>In this example, whenever <code>#myDiv</code> changes size (which will occur when the media query is triggered), your jQuery resize logic will also be run.</p> <p>For simplicity I used an element width, but you could easily use whatever property you are changing, such as the <code>body</code> background.</p> <p>Another advantage of this approach is that it keeps the <code>window.resize</code> 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).</p> <p>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 <a href="http://www.w3.org/TR/css3-mediaqueries/" rel="noreferrer">W3C recommendation for media queries</a> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload