admin管理员组

文章数量:1303530

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?

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?

Share Improve this question asked Nov 3, 2011 at 14:46 firefusionfirefusion 1,1072 gold badges14 silver badges26 bronze badges 2
  • 2 possible duplicate of How to detect the screen resolution with JavaScript? – Quentin Commented Nov 3, 2011 at 14:47
  • Window size is far more important than a screen resolution. – Quentin Commented Nov 3, 2011 at 14:47
Add a ment  | 

2 Answers 2

Reset to default 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
}

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

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

本文标签: jqueryRunning different JavaScript at different screen resolutionsStack Overflow