3

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?

2

2 Answers 2

8

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

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;
That works! One last question is how to I get this to update on resize?
Don't worry found the resize event :P Silly me.
1

In your ready function, you could test screen height and width like this :

if (screen.width<=768) 
{
//Do this
}
else

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.