如果Javascript跳过其他的话?

时间:2016-04-29 09:32:38

标签: javascript

很明显,这将是我的一个愚蠢的错误,因为如果不工作,为什么还要另外一个?

但是我在某个地方找不到我无法找到的错误,现在我的javascript只是'跳过'我的其他if我的代码中的语句。

$(window).resize(function(){

    if ($(window).width() <= 1080) {
        responsiveLength = 2;
        maxMargin = -((imgLength - responsiveLength) * imgWidth);
        imgWrapper.css('margin-left','0');
    } else if ($(window).width() <= 700){
        console.log($(window).width());
        maxMargin = -((imgLength - 0) * imgWidth);
        imgWrapper.css('margin-left','0');
    } else {
        responsiveLength = 3;
        maxMargin = -((imgLength - responsiveLength) * imgWidth);    
        imgWrapper.css('margin-left','0');
    }

});

提前致谢

3 个答案:

答案 0 :(得分:3)

安排从最低分辨率到最高分辨率的条件。

if ($(window).width() <= 700) {
    ....
} else if ($(window).width() <= 1080) {
    ....
} else {
    ....
}

答案 1 :(得分:1)

您需要交换这两个因为700小于1080而且永远不会去那里。

if ($(window).width() <= 700) {
  console.log($(window).width());
  maxMargin = -((imgLength - 0) * imgWidth);
  imgWrapper.css('margin-left','0');
} else if ($(window).width() <= 1080) {
  responsiveLength = 2;
  maxMargin = -((imgLength - responsiveLength) * imgWidth);
  imgWrapper.css('margin-left','0');
} else {
  responsiveLength = 3;
  maxMargin = -((imgLength - responsiveLength) * imgWidth);    
  imgWrapper.css('margin-left','0');
}

替代解决方案,确保窗口大小超过700px

if ($(window).width() <= 1080 && $(window).width() > 700) {

逻辑解释

让我们考虑这里的逻辑。

  • ScreenSize:600px
    少于1080?是的。
    转到第一个区块。

  • ScreenSize:1000px
    少于1080?是的。
    转到第一个区块。

  • ScreenSize:1600px
    少于1080?第
    去第三街区。

答案 2 :(得分:1)

如果$(window).width() <= 1080暗示$(window).width() <= 700,那么它将无法到达其他地方。

我想你想做

if ($(window).width() <= 1080 && $(window).width() > 700) {
    responsiveLength = 2;
    maxMargin = -((imgLength - responsiveLength) * imgWidth);
    imgWrapper.css('margin-left','0');
} else if ($(window).width() <= 700){
    console.log($(window).width());
    maxMargin = -((imgLength - 0) * imgWidth);
    imgWrapper.css('margin-left','0');
} else {
    responsiveLength = 3;
    maxMargin = -((imgLength - responsiveLength) * imgWidth);    
    imgWrapper.css('margin-left','0');
}

或撤消订单

if ($(window).width() <= 700) {
    responsiveLength = 2;
    maxMargin = -((imgLength - responsiveLength) * imgWidth);
    imgWrapper.css('margin-left','0');
} else if ($(window).width() <= 1080){
    console.log($(window).width());
    maxMargin = -((imgLength - 0) * imgWidth);
    imgWrapper.css('margin-left','0');
} else {
    responsiveLength = 3;
    maxMargin = -((imgLength - responsiveLength) * imgWidth);    
    imgWrapper.css('margin-left','0');
}