如果屏幕宽度小于960像素,请执行某些操作


221

如果我的屏幕宽度小于960像素,如何使jQuery做某事?不管我的窗口大小如何,以下代码始终会触发第二个警报:

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

    alert('More than 960');
}

1
您是在页面加载时执行此操作还是在调整浏览器大小时执行此操作?说出alert(screen.width)来查看用于决定的内容总是一个好主意。
2011年

1
为什么不进行媒体查询
bzlm 2011年

3
为什么不进行媒体查询?我可以想到许多情况下媒体查询毫无帮助的情况。如果jdln要使用jQuery,则假定他有充分的理由这样做。
2015年

对于媒体查询无效的情况,有很多用途,例如,仅当屏幕宽度大于例如1000像素时才激活砌体效果。
Pekka

Answers:



137

您可能希望将其与调整大小事件结合使用:

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

对于RJ:

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');
        }
    }
});

我尝试http://api.jquery.com/off/没有成功,所以我选择了eventFired标志。


在resize事件中包装只会使其在调整大小时触发,而不会在加载时触发。
泰德(Ted)

5
@Ted这就是为什么我说combine it with
jk。

1
@RJ参见上面的编辑。我不知道“清除”事件的含义,因此我避免了再次触发事件。
jk。

1
太棒了,我已经尝试了几天的工作来如何equalHeights在调整大小时绑定一个函数,但是宽度只能大于768。这就像一种魅力。谢谢
丹尼·英格兰

1
在该代码后添加一个resize事件是有意义的,这样,无论当前浏览器的大小如何,加载时它都将运行所需的代码
BennKingy

16

我建议不要将jQuery用于此类事情,然后继续window.innerWidth

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

1
为什么最好不要使用jQuery?
raison

1
@raison我发现$(window).width()将不包含滚动条-因此,如果您的密码为960px,它将返回944px,并且您的js不会按预期触发。该解决方案将返回960px
Sean T

14

您还可以使用带有javascript的媒体查询。

const mq = window.matchMedia( "(min-width: 960px)" );

if (mq.matches) {
       alert("window width >= 960px");
} else {
     alert("window width < 960px");
}

11

我建议(需要jQuery):

/*
 * 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 !');
  }
});

在CodePen中添加:http ://codepen.io/moabi/pen/QNRqpY?editors=0011

然后,您可以使用var:windowWidth轻松获得窗口的宽度,并使用:windowHeight轻松获得窗口的宽度

否则,请获取一个js库:http : //wicky.nillia.ms/enquire.js/


11
// 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();
});

9

$(window).width()

要么

$(document).width()

要么

$('body').width()

8

我知道我来晚了,但是我希望这对有类似问题的任何人有所帮助。页面出于任何原因刷新时也可以使用。

$(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();
    }

});});

1

试试这个代码

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>


-8

不,这都不行。您需要的是这个!!!

试试这个:

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

3
发布链接不是答案。
nedaRM

对不起,我有点着急!等等,我现在就在做...
Saurav Chaudhary

请注意,如果screen.width(最好document.body.clientWidth是使用Vanilla JS 捕获的)是<= 960(此处是if语句),则唯一的其他选项(ELSE是screen.width> 960),因此在此处使用else是多余的。使用document.body.clientWidth <=960 ? handleSmallerThan960() : handleLargerThan960()或某些变体。无需其他if(冗余代码)
Zargold
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.