3

How can i run some JQ code, only when the user screen resolution is higher/lower than X?

Samuel E.
  • 2,320
  • 2
  • 26
  • 31

4 Answers4

3

You can do it with pure JS:

if(screen.width>=1024)
{
   myFunction();
}
Igor Konopko
  • 764
  • 2
  • 13
  • 26
2

Well i'd advise against using screen resolution because not everyone maximizes their window (let alone full-screens it). Check the window width/height, and do whatever accordingly:

var winW = $(window).width(),
    winH = $(window).height();

if ( winW > X && winH > Y) { 
    waWaWeeWah(); // do stuff
}
Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96
1

Use window event listener, e.g.:

$(window).resize(function() {
    if( $(window).width() < yourThreshold ){

        // do your business

    }
});
moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • can i use css selectors with JQ to acomplish this? because when i use `$(window).width()` it dosent count the scroll bar, and its not dynamic – Samuel E. Aug 29 '12 at 15:10
  • Have a look around, e.g. http://stackoverflow.com/questions/596072/detect-window-width-and-compensate-for-scrollbars-javascript – moonwave99 Aug 29 '12 at 15:15
0

What about this suggestion I was trying to get some feedback on, it ties js to media queries: https://stackoverflow.com/questions/12203220/suggestion-for-responsive-design-jquery-and-mobile-sites

Community
  • 1
  • 1
iamrho
  • 1
  • 2