【发布时间】: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的链接:
(只有左右——但你可以看到问题所在)
【问题讨论】:
-
请在问题中包含相关代码。打破 Codepen 链接以绕过代码要求并不是一回事。
-
由于拼写错误,投票结束。
=与==.
标签: jquery