Do something if screen width is less than 960 px

JavascriptJquery

Javascript Problem Overview


How can I make jQuery do something if my screen width is less than 960 pixels? The code below always fires the 2nd alert, regardless of my window size:

if (screen.width < 960) {
    alert('Less than 960');
}
else {

    alert('More than 960');
}

Javascript Solutions


Solution 1 - Javascript

Use jQuery to get the width of the window.

if ($(window).width() < 960) {
   alert('Less than 960');
}
else {
   alert('More than 960');
}

Solution 2 - Javascript

You might want to combine it with a resize event:

 $(window).resize(function() {
  if ($(window).width() < 960) {
     alert('Less than 960');
  }
 else {
    alert('More than 960');
 }
});

For R.J.:

var eventFired = 0;

if ($(window).width() < 960) {
    alert('Less than 960');

}
else {
    alert('More than 960');
    eventFired = 1;
}

$(window).on('resize', function() {
    if (!eventFired) {
        if ($(window).width() < 960) {
            alert('Less than 960 resize');
        } else {
            alert('More than 960 resize');
        }
    }
});

I tried http://api.jquery.com/off/ with no success so I went with the eventFired flag.

Solution 3 - Javascript

I recommend not to use jQuery for such thing and proceed with window.innerWidth:

if (window.innerWidth < 960) {
    doSomething();
}

Solution 4 - Javascript

You can also use a media query with javascript.

const mq = window.matchMedia( "(min-width: 960px)" );
		
if (mq.matches) {
	   alert("window width >= 960px");
} else {
     alert("window width < 960px");
}

Solution 5 - Javascript

// Adds and removes body class depending on screen width.
function screenClass() {
    if($(window).innerWidth() > 960) {
        $('body').addClass('big-screen').removeClass('small-screen');
    } else {
        $('body').addClass('small-screen').removeClass('big-screen');
    }
}

// Fire.
screenClass();

// And recheck when window gets resized.
$(window).bind('resize',function(){
    screenClass();
});

Solution 6 - Javascript

I would suggest (jQuery needed) :

/*
 * windowSize
 * call this function to get windowSize any time
 */
function windowSize() {
  windowHeight = window.innerHeight ? window.innerHeight : $(window).height();
  windowWidth = window.innerWidth ? window.innerWidth : $(window).width();

}

//Init Function of init it wherever you like...
windowSize();

// For example, get window size on window resize
$(window).resize(function() {
  windowSize();
  console.log('width is :', windowWidth, 'Height is :', windowHeight);
  if (windowWidth < 768) {
    console.log('width is under 768px !');
  }
});

Added in CodePen : http://codepen.io/moabi/pen/QNRqpY?editors=0011

Then you can get easily window's width with the var : windowWidth and Height with : windowHeight

otherwise, get a js library : http://wicky.nillia.ms/enquire.js/

Solution 7 - Javascript

use

$(window).width()

or

$(document).width()

or

$('body').width()

Solution 8 - Javascript

I know i'm late to answer this, but i hope it is of some help to anybody who have similar problem. It also works when page refreshes for any reason.

$(document).ready(function(){

if ($(window).width() < 960 && $(window).load()) {
		$("#up").hide();
	}
	
	if($(window).load()){
		if ($(window).width() < 960) {
		$("#up").hide();
		}
	}

$(window).resize(function() {
	if ($(window).width() < 960 && $(window).load()) {
		$("#up").hide();
	}
	else{
		$("#up").show();
	}
	
	if($(window).load()){
		if ($(window).width() < 960) {
		$("#up").hide();
		}
	}
	else{
		$("#up").show();
	}
	
});});

Solution 9 - Javascript

Try this code

if ($(window).width() < 960) {
 alert('width is less than 960px');
}
else {
 alert('More than 960');
}

   if ($(window).width() < 960) {
     alert('width is less than 960px');
    }
    else {
     alert('More than 960');
    }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Solution 10 - Javascript

nope, none of this will work. What you need is this!!!

Try this:

if (screen.width <= 960) {
  alert('Less than 960');
} else if (screen.width >960) {
  alert('More than 960');
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvanssView Question on Stackoverflow
Solution 1 - Javascriptaziz punjaniView Answer on Stackoverflow
Solution 2 - Javascriptjk.View Answer on Stackoverflow
Solution 3 - JavascriptDaniel KmakView Answer on Stackoverflow
Solution 4 - Javascriptuser7923788View Answer on Stackoverflow
Solution 5 - JavascriptleymannxView Answer on Stackoverflow
Solution 6 - JavascriptmoabiView Answer on Stackoverflow
Solution 7 - JavascriptGuardView Answer on Stackoverflow
Solution 8 - JavascriptVeljko StefanovicView Answer on Stackoverflow
Solution 9 - JavascriptJIBIN BJView Answer on Stackoverflow
Solution 10 - JavascriptSaurav ChaudharyView Answer on Stackoverflow