连续两个if语句不能正常工作 [英] Two if statements in a row arent working
问题描述
我想在使用键盘的同时使用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');
^^此行应复制到moveLeft
和moveRight
函数的开头.
^^ This line should be copied to the beginning of both the moveLeft
and moveRight
functions.
这篇关于连续两个if语句不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!