bee*_*imi 4 jquery if-statement window width
我想知道为什么我的最后一个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);
});?
Run Code Online (Sandbox Code Playgroud)
一切都记录在控制台中,除了最后一个if.我究竟做错了什么?
谢谢,
更新:
对于仍然感兴趣的人,我强烈推荐enquire.js插件:
http://wicky.nillia.ms/enquire.js/
放下最好的方法我发现在JS中识别媒体查询.
Mah*_*ahn 17
您在代码中缺少一对> =,并且没有比较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);
});?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35765 次 |
| 最近记录: |