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');
}
Use jQuery to get the width of the window.
if ($(window).width() < 960) {
alert('Less than 960');
}
else {
alert('More than 960');
}
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.
I recommend not to use jQuery for such thing and proceed with window.innerWidth:
if (window.innerWidth < 960) {
doSomething();
}
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");
}
// 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();
});
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/
use
$(window).width()
or
$(document).width()
or
$('body').width()
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();
}
});});
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>
My solution for the question using Vanilla JS is,
window.addEventListener("load", function () {
if (window.innerWidth < 960) { doSomething(); }
}
This works, but with one problem, whenever resizing the screen in dev tools, which is a common thing for developers, the function does something will not be fired.
For example, if we display the webpage on a computer with a screen size of more than 1000px, the function will not be fired, and when we resize the windows to less than the target(here 960px) we expect the event to be fired but that is not what happens.
By the time the script file is already processed and finished and will not be re-read, so we need to fire an event whenever we resize the screen and handle that event using "addEventListener", Just like this;
window.addEventListener("resize", (e) => {
const windowWidth = window.innerWidth;
if (windowWidth > 960) { doSomething(); }
if (windowWidth < 960) { doSomething(); }
});
The best practise is to combine both the code-snippets in the project.
Simple and clean solution using Vanilla JavaScript
let app = document.getElementById('app')
const changeColorFn = ( app, color ) => {
app.setAttribute("style",`background: ${color}`)
}
const winSizeFn = ( winWidth, callback, app, color ) => {
if (window.innerWidth < winWidth ) {
callback(app, color);
}
}
winSizeFn( '1200', changeColorFn, app, 'red' )
winSizeFn( '800', changeColorFn, app, 'green' )
winSizeFn( '500', changeColorFn, app, 'blue' )
window.addEventListener("resize", (e) => {
// add winSizeFn here if you want call function on window resize
})
<div id="app">My app content</div>
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');
}
Related
i want to apply this jquery code only for mobile and tablets
<script>
jQuery(window).scroll(function () {
var y = jQuery(this).scrollTop();
if (y > 440) {
jQuery('.dropdown,.navbar.fixed-top').fadeIn(600);
} else {
jQuery('.dropdown,.navbar.fixed-top').fadeOut(600);
}
});
</script>
One simple way is to grab the window width and run the function if the width is smaller than a certain width.
<script>
jQuery(window).scroll(function () {
if(jQuery(window).width() <= 1024){
var y = jQuery(this).scrollTop();
if (y > 440) {
jQuery('.dropdown,.navbar.fixed-top').fadeIn(600);
} else {
jQuery('.dropdown,.navbar.fixed-top').fadeOut(600);
}
});
}
</script>
I'm just trying to write a message in the console when the size of the window is less than 700 px.
The things I have tried is:
if(window.innerWidth < 700){
console.log("hello");
}
And
if(screen.width < 700){
console.log("hello");
}
I don't get any error meassages but the code doesn't run. If I ad "px" after the 700 I get the error meassage "Uncaught SyntaxError: Unexpected identifier".
You need to put this inside the window's resize event listener. And also you need to use window.innerWidth and it always returns an integer value.
if (window.attachEvent) {
window.attachEvent('onresize', function() {
if (window.innerWidth < 760)
console.log("Less than 760");
else
console.log("More than 760");
});
} else if (window.addEventListener) {
window.addEventListener('resize', function() {
if (window.innerWidth < 760)
console.log("Less than 760");
else
console.log("More than 760");
}, true);
} else {
//The browser does not support Javascript event binding
}
You need to listen for 'resize' event on 'window':
window.addEventListener('resize', resize);
function resize() {
if (window.innerWidth < 700) {
console.log('window.innerWidth < 700');
// window.removeEventListener('resize', resize); // once
}
}
more information about .addEventListener: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
onResize();
window.addEventListener('resize', onResize);
function onResize(){
var width = document.documentElement.clientWidth;
}
I need to be able to disable / re-enable Javascript on window resize?
Currently when the window is resized on the desktop, the nav bar sticks and is the only thing visible instead of the content.
<script>
if ( $(window).width() <= 1200 ) {
}else{
$('nav').addClass('original').clone().insertAfter('nav').addClass('cloned').css('position','fixed').css('top','0').css('margin- top','0').css('z-index','500').removeClass('original').hide();
scrollIntervalID = setInterval(stickIt, 10);
function stickIt() {
var orgElementPos = $('.original').offset();
orgElementTop = orgElementPos.top;
if ($(window).scrollTop() >= (orgElementTop)) {
as original element.
orgElement = $('.original');
coordsOrgElement = orgElement.offset();
leftOrgElement = coordsOrgElement.left;
widthOrgElement = orgElement.css('width');
th',widthOrgElement).show();
$('.original').css('visibility','hidden');
} else {
$('.cloned').hide();
$('.original').css('visibility','visible');
}
}
</script>
You can bind an event handler to the "resize" JavaScript event:
$(window).resize(function() {
if($(window).width() <= 1200) {
//You code here
}else {
//You code here
}
});
Your code will be executed whenever the browser window's size is changed.
$(window).resize(function() {
if($(window).width() <= 1200) {
//small screen code
}else {
//large screen code }
});
//trigger window resize event on load
$(window).trigger('resize');
you can do it by checking the window width
var winWidth = $(window).width(); // you can get the window width from this
//Then check with the condtion(compare with your need ex :)
if(winWidth <= 600)
{
//your work here
alert("window resize less than or equal to 600");
}
else
{
//your work here
alert("window resize greater than to 600");
}
I have a variable called size and a function that depends on it. As the variable depends on the screen width and needs to change if the width varies, I need to have it within a the window resize event. However I also want to call it from with other events such as a scroll event.
$(window).resize(function (){
if ($(window).width() > 1200) {
var size = "large";
}
else {
var size = "small";
}
function showRow(param) {
if (size == "large") {
$(".result.hidden").slice(0, 3).removeClass('hidden').each(someFunction);
}
else if (size == "small") {
$(".result.hidden").slice(0, 2).removeClass('hidden').each(someFunction);
}
}
});
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
showRow();
}
});
You can do this, showRow is a global var and calculating of the window size occurs in it, so you can call it inside of resize and for scrolling. This is assuming you wanted to also use showRow inside of your resize. If you don't have any other code inside of resize, then you can just delete that altogether in the first example
var showRow = function() {
if ($(window).width() > 1200) {
var size = "large";
}
else {
var size = "small";
}
if (size == "large") {
$(".result.hidden").slice(0, 3).removeClass('hidden').each(someFunction);
}
else if (size == "small") {
$(".result.hidden").slice(0, 2).removeClass('hidden').each(someFunction);
}
}
$(window).resize(function (){
showRow();
// other code
});
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
showRow();
}
});
If you don't want to use showRow inside of resize then
// initialize windowSize
var windowSize = $(window).width() > 1200 ? "large" : "small";
var showRow = function () {
if (windowSize == "large") {
$(".result.hidden").slice(0, 3).removeClass('hidden').each(someFunction);
} else if (windowSize == "small") {
$(".result.hidden").slice(0, 2).removeClass('hidden').each(someFunction);
}
}
$(window).resize(function () {
if ($(window).width() > 1200) {
windowSize = "large";
} else {
windowSize = "small";
}
// other code
});
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() > $(document).height() - 50) {
showRow();
}
});
Trying to grab the screen size and add an if / else query, but for some reason I can't get it to work
What I'm looking for is if the screen width is larger / equal 480px, the following should be outputted:
{$pPage = $cWidth}
Else this:
{$pPage = 1}
This is what I have so far:
<script type="text/javascript">
(function($) {
$(document).ready(function() {
{$pPage = $cWidth}
})(jQuery);
</script>
Anybody out there with advice how to add the the query in reference to the screen.width, so that it also works?
try this:
using jquery:
if ($(window).width() < 1280) {
alert('Less than 1280');
}
else {
alert('More than 1280');
}
OR
using javascript:
var screensize = document.documentElement.clientWidth;
if (screensize < 1280) {
alert('Less than 1280');
}
else {
alert('More than 1280');
}