I want some jQuery functions for a dropdown menu to run only if the the screen size is under 768px and a different dropdown menu function to run if it is above that value. How would I do this in JavaScript?
-
2possible duplicate of How to detect the screen resolution with JavaScript?Quentin– Quentin2011-11-03 14:47:28 +00:00Commented Nov 3, 2011 at 14:47
-
Window size is far more important than a screen resolution.Quentin– Quentin2011-11-03 14:47:49 +00:00Commented Nov 3, 2011 at 14:47
Add a comment
|
2 Answers
You can get the size of the window (screen size is mostly irrelevant, you care about the actual space you have for rendering) using the .height() and .width() methods:
if( $(window).width() > 768 ) {
// Large menu
} else {
// Small menu
}
3 Comments
David Brainer
Just for reference, a non-jQuery solution pulled from the adapt.js code:
var width = this.innerWidth || this.document.documentElement.clientWidth || this.document.body.clientWidth || 0;firefusion
That works! One last question is how to I get this to update on resize?
firefusion
Don't worry found the resize event :P Silly me.