连续两个if语句不能正常工作 [英] Two if statements in a row arent working

查看:339
本文介绍了连续两个if语句不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用键盘的同时使用if语句创建一个滑块,该滑块的左,右,上和下.

i want to create a slider with if statements while using the keyboard, left n right, up and down.

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

i thought the solution is an if statement, but when i´m clicking n arrow key both of the animations are starting.

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

 });

这是有此问题的代码笔的链接:

here is a link to a codepen with this problem:

代码笔

(仅左右),但是您可以看到问题所在了

(only left and right – but you can see what the problem is)

推荐答案

if (content_left = "-100vw") { 

您在此处设置值而不是对其进行测试.使用=====代替.其余的if语句也是如此.

You're setting the value here instead of testing it. Use == or === instead. The same goes for the rest of the if statements.

您还使用了过时的content_left值.

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

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

^^ This line should be copied to the beginning of both the moveLeft and moveRight functions.

这篇关于连续两个if语句不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆