2012-08-23 130 views
4

我想知道为什么我的最后一个if语句永远不会被执行。我试图做到这一点:jQuery Window Width 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之外,一切都被记录在控制台中。我究竟做错了什么?

感谢,

UPDATE:

对任何人来说仍然有兴趣,我强烈建议enquire.js插件:

http://wicky.nillia.ms/enquire.js/

相传最好的办法我发现识别JS中的媒体查询。

+0

这是别的/如果,你有足够大的屏幕;。)... – adeneo

+0

是的我有一个比960像素更宽的屏幕。我可以console.log(windowSize)并获得正确的度量。如果我在(> = 960)之前删除其他两个if语句,则最后的语句得到正确执行。请帮我理解为什么这不适用于所有人。 – beefchimi

+0

只要不使用第二个运算符,您需要将单个'='更改为双'=='。 –

回答

16

您在代码中缺少一对> =,并且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); 
});​ 
+0

你甚至不需要“windowSize> = 480”等位,因为你已经知道这从前面的if。 –

+0

@shhac yup,已更新。 – Mahn

+0

非常感谢你们。每个人都贡献。我现在意识到我的错误。我非常感谢帮助。我会尽快接受这个正确的答案(你们很快!) – beefchimi

2

你缺少一个大于号:

else if (windowSize = 720 

在仅使用等号?

试试这个:

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

FIDDLE

+0

这将不起作用,因为如果你的屏幕尺寸是正确的480,720或960它不知道该怎么做。 –

+0

@NicholasCardot - 仍在编辑? – adeneo

+0

我不小心点击了编辑按钮而不是它下面的评论按钮。我刚刚点击退格键,然后点击评论按钮。它锁定了什么? –

2

这是因为你的else if语句的。你正在检查一个等号,这是分配值。

if (windowSize = 480 && windowSize <= 719) 

时,你应该做

if (windowSize == 480 && windowSize <= 719) 

或> =,如果是这样的预期逻辑。