【问题标题】:Two if statements in a row arent working [closed]连续两个 if 语句不起作用[关闭]
【发布时间】:2015-01-28 20:24:54
【问题描述】:

我想在使用键盘时创建一个带有 if 语句的滑块,左 n 右,上和下。

我认为解决方案是 if 语句,但是当我单击 n 箭头键时,两个动画都开始了。

$(document).ready(function () {

  var content_left = $('.content').css('left');



function moveLeft() {

   if (content_left = "-100vw") { 
   console.log(content_left);
       $('.content').animate({
            left: "0vw"
        }, 1000);
    }

  if (content_left = "-200vw") { 
    console.log(content_left);
       $('.content').animate({
            left: "-100vw"
        }, 1000);
    }

    };



function moveRight() {


  if (content_left = "0vw") { 
       $('.content').animate({
            left: "-100vw"
        }, 100);
    }

 if (content_left = "-100vw") { 
    console.log(content_left);
       $('.content').animate({
            left: "-200vw"
        }, 100);
    }

    };


$(document).keydown(function(e){
    if (e.keyCode == 39) { 
       moveRight();
       return false;
    }

   if (e.keyCode == 37) { 
       moveLeft();
       return false;
    }
    });

 });

这里是一个有这个问题的codepen的链接:

CODE PEN

(只有左右——但你可以看到问题所在)

【问题讨论】:

  • 请在问题中包含相关代码。打破 Codepen 链接以绕过代码要求并不是一回事。
  • 由于拼写错误,投票结束。 ===.

标签: jquery


【解决方案1】:
if (content_left = "-100vw") { 

您在此处设置值而不是对其进行测试。请改用=====。其余的 if 语句也是如此。

您还使用过时的content_left 值。

  var content_left = $('.content').css('left');

^^ 此行应复制到moveLeftmoveRight 函数的开头。

【讨论】:

  • 非常感谢,但它仍然无法正常工作。
  • 我想我发现了另一个问题;已更新。
猜你喜欢
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多