Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery and CSS media queries not in sync

I've been trying to understanding the way window width is determined and I was wondering if it's different for CSS and jQuery.

If for example I have the CSS media query:

@media (max-width: 979px) {
  /*my css here*/
}

and also some jQuery:

if ($(window).width() < 979) {
  //my jQuery here
}

I would expect the following: - When the browser is resized & refreshed at 980px, both of the above will be ignored - When the browser is resized & refreshed at 978px, both of the above will activate.

However what I'm finding is that - The jQuery and CSS don't kick in until about 964px and below. - Sometimes (if I have window.resize enabled) the jQuery and CSS kick in at seperate widths (a few pixels apart).

Can anyone shed any light on what's going on here?

By the way I'm basing the window width on Firebug's layout viewer.

like image 338
CaribouCode Avatar asked Aug 08 '13 14:08

CaribouCode


People also ask

Why is my media query not working in CSS?

CSS declared inline This may be the reason why your media queries aren't working. That is to say, you may have declared CSS within your HTML document. So if this is the case, simply go through the HTML document and remove the CSS declared inline. Alternatively, you can override the inline CSS with !

Can we use jQuery in media query?

}); Using this solution, regardless of how the browser treats the scrollbar, the jQuery and CSS media query will fire at exactly the same time. Baring in mind there are various wrappers and solutions that you could use, for something so small this was more than enough.

How do I link media queries in CSS?

The Placement of Media Queries The internal method includes adding the <style> tag to the <head> tag of the HTML file, and creating the media query within the parameters of the <style> tag. The external method involves creating a media query in an external CSS file and linking it to your HTML file via the <link> tag.

Do media queries go in CSS?

Media queries are commonly associated with CSS, but they can be used in HTML and JavaScript as well. There are a few ways we can use media queries directly in HTML.


2 Answers

The problem is related to the scrollbar width: window.innerWidth will return the right value that is read by CSS media queries. Note that $(window).innerWidth() will return the wrong value.

For a cross-browser solution can be used a function like this:

function viewport() {
    var e = window, a = 'inner';
    if (!('innerWidth' in window )) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}

var value = viewport().width;
like image 176
guari Avatar answered Oct 20 '22 02:10

guari


A bit off topic, but I assume you're trying to mimic when the javascript fires because you want to do something at the same time as the css breakpoint switches. If you want to have javascript do something when the media query fires, I've found it is easiest to poll for a change created by the css, rather than trying to mimic the way media queries fire (which is inconsistent in browsers due to reporting of scrollbar widths). For instance if at your breakpoint, element-x goes from position: relative to position: fixed, you can ensure your javascript is in line with your css by testing

if(elementX.css('position')== 'fixed'){
//code
}

rather than polling the document width.

like image 22
TorranceScott Avatar answered Oct 20 '22 00:10

TorranceScott