我想知道为什么我的最后一个 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);
});
除了最后一个 else if 之外,所有内容都记录在控制台中。我究竟做错了什么?
谢谢,
更新:
对于仍然感兴趣的人,我强烈推荐 enquire.js 插件:
http://wicky.nillia.ms/enquire.js/
我发现在 JS 中识别媒体查询的最佳方法。