jQuery 窗口宽度 else if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12083508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Window Width else if statement
提问by beefchimi
I am wondering why my last else if statement never gets executed. I am trying to do this:
我想知道为什么我的最后一个 else if 语句永远不会被执行。我正在尝试这样做:
$(document).ready(function() {
function checkWidth() {
var windowSize = $(window).width();
if (windowSize <= 479) {
console.log("screen width is less than 480");
}
else if (windowSize = 480 && windowSize <= 719) {
console.log("screen width is less than 720 but greater than or equal to 480");
}
else if (windowSize = 720 && windowSize <= 959) {
console.log("screen width is less than 960 but greater than or equal to 720");
}
else if (windowSize >= 960) {
console.log("screen width is greater than or equal to 960");
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});?
Everything gets logged in the console except for the last else if. What am I doing wrong?
除了最后一个 else if 之外,所有内容都记录在控制台中。我究竟做错了什么?
Thanks,
谢谢,
UPDATE:
更新:
For anyone still interested, I highly recommend the enquire.js plugin:
对于仍然感兴趣的任何人,我强烈推荐 enquire.js 插件:
http://wicky.nillia.ms/enquire.js/
http://wicky.nillia.ms/enquire.js/
Hands down best approach I've found to recognizing media queries in JS.
我发现在 JS 中识别媒体查询的最佳方法。
回答by Mahn
You are missing a couple >=in your code, and windowSize is not being compared but assigned a new value as a result of statements like windowSize = 480
. Try this version instead:
您的代码中缺少几个>=,并且 windowSize 没有被比较,而是由于诸如windowSize = 480
. 试试这个版本:
$(document).ready(function() {
function checkWidth() {
var windowSize = $(window).width();
if (windowSize <= 479) {
console.log("screen width is less than 480");
}
else if (windowSize <= 719) {
console.log("screen width is less than 720 but greater than or equal to 480");
}
else if (windowSize <= 959) {
console.log("screen width is less than 960 but greater than or equal to 720");
}
else if (windowSize >= 960) {
console.log("screen width is greater than or equal to 960");
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});?
回答by adeneo
You're missing a greater than sign :
您缺少一个大于号:
else if (windowSize = 720
and using just the equal sign ?
并只使用等号?
Try this instead:
试试这个:
$(document).ready(function() {
function checkWidth() {
var windowSize = $(window).width();
if (windowSize < 480) {
console.log("screen width is less than 480");
}
else if (windowSize < 720) {
console.log("screen width is less than 720 but greater than or equal to 480");
}
else if (windowSize < 960) {
console.log("screen width is less than 960 but greater than or equal to 720");
}
else {
console.log("screen width is greater than or equal to 960");
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});?
回答by Hyman
It's because of your else if statements. You're checking with a single equal sign, which is assigning the value.
这是因为你的 else if 语句。您正在使用单个等号进行检查,该等号正在分配值。
if ( windowSize = 480 && windowSize <= 719 )
when you should be doing
你应该什么时候做
if ( windowSize == 480 && windowSize <= 719 )
or >=, if that's the intended logic.
或 >=,如果这是预期的逻辑。