jQuery 左右动画只发生一次
jQuery Animate Left and Right only happening once
我正在尝试为我网站的轮播中的某些内容制作动画,以便它根据按下的按钮向左或向右移动。这是我的示例代码:
HTML
<div class="red-bg">
<div class="blue-bg" id="toAnimate">
<p>Some text</p>
<p>Some other text</p>
<button id="leftButton">left</button>
<button id="rightButton">right</button>
</div>
</div>
CSS
.red-bg {
background: red;
height: 250px;
margin-left: 300px;
padding: 20px;
width: 250px;
}
.blue-bg {
background: blue;
position: relative;
}
JS
$(document).ready(function() {
function animateLeft() {
$("#toAnimate").animate({
opacity: 0,
right: 100
}, 500, function() {
$('#toAnimate').css('right', '0');
$('#toAnimate').css('opacity', '1');
});
}
function animateRight() {
$("#toAnimate").animate({
opacity: 0,
left: 100
}, 500, function() {
$('#toAnimate').css('left', '0');
$('#toAnimate').css('opacity', '1');
});
}
$('#leftButton').click(function() {
animateLeft();
});
$('#rightButton').click(function() {
animateRight();
});
});
这是我的 CodePen 的 link:http://codepen.io/leofontes/pen/NRgOxb
基本上,我的情况是,如果我先按 left 按钮,它会起作用并向左移动,但是如果我按 right,left 停止工作,仅淡出但不向左(向右仍然有效)。
知道为什么会出现这种行为,或者我该如何解决?谢谢!
如果需要更多信息,请告诉我
问题:
在您的代码中,您尝试从右到左 100px 和从左到右
First ,you remove your left style in your element or do like this
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.0.3.js"></script>
</head>
<body>
<div class="red-bg">
<div class="blue-bg" id="toAnimate">
<p>Some text</p>
<p>Some other text</p>
<button id="leftButton">left</button>
<button id="rightButton">right</button>
</div>
</div>
</body>
</html>
JAVA 脚本
$(document).ready(function() {
function animateLeft() {
$("#toAnimate").animate({
opacity: 0,
left: -100
}, 500, function() {
$('#toAnimate').css('left', '0');
$('#toAnimate').css('opacity', '1');
});
}
function animateRight() {
$("#toAnimate").animate({
opacity: 0,
left: 100
}, 500, function() {
$('#toAnimate').css('left', '0');
$('#toAnimate').css('opacity', '1');
});
}
$('#leftButton').click(function() {
animateLeft();
});
$('#rightButton').click(function() {
animateRight();
});
});
我正在尝试为我网站的轮播中的某些内容制作动画,以便它根据按下的按钮向左或向右移动。这是我的示例代码:
HTML
<div class="red-bg">
<div class="blue-bg" id="toAnimate">
<p>Some text</p>
<p>Some other text</p>
<button id="leftButton">left</button>
<button id="rightButton">right</button>
</div>
</div>
CSS
.red-bg {
background: red;
height: 250px;
margin-left: 300px;
padding: 20px;
width: 250px;
}
.blue-bg {
background: blue;
position: relative;
}
JS
$(document).ready(function() {
function animateLeft() {
$("#toAnimate").animate({
opacity: 0,
right: 100
}, 500, function() {
$('#toAnimate').css('right', '0');
$('#toAnimate').css('opacity', '1');
});
}
function animateRight() {
$("#toAnimate").animate({
opacity: 0,
left: 100
}, 500, function() {
$('#toAnimate').css('left', '0');
$('#toAnimate').css('opacity', '1');
});
}
$('#leftButton').click(function() {
animateLeft();
});
$('#rightButton').click(function() {
animateRight();
});
});
这是我的 CodePen 的 link:http://codepen.io/leofontes/pen/NRgOxb
基本上,我的情况是,如果我先按 left 按钮,它会起作用并向左移动,但是如果我按 right,left 停止工作,仅淡出但不向左(向右仍然有效)。
知道为什么会出现这种行为,或者我该如何解决?谢谢! 如果需要更多信息,请告诉我
问题: 在您的代码中,您尝试从右到左 100px 和从左到右
First ,you remove your left style in your element or do like this
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.0.3.js"></script>
</head>
<body>
<div class="red-bg">
<div class="blue-bg" id="toAnimate">
<p>Some text</p>
<p>Some other text</p>
<button id="leftButton">left</button>
<button id="rightButton">right</button>
</div>
</div>
</body>
</html>
JAVA 脚本
$(document).ready(function() {
function animateLeft() {
$("#toAnimate").animate({
opacity: 0,
left: -100
}, 500, function() {
$('#toAnimate').css('left', '0');
$('#toAnimate').css('opacity', '1');
});
}
function animateRight() {
$("#toAnimate").animate({
opacity: 0,
left: 100
}, 500, function() {
$('#toAnimate').css('left', '0');
$('#toAnimate').css('opacity', '1');
});
}
$('#leftButton').click(function() {
animateLeft();
});
$('#rightButton').click(function() {
animateRight();
});
});