gpt4 book ai didi

javascript - jQuery Animate Left 和 Right 只发生一次

转载 作者:行者123 更新时间:2023-12-03 06:00:49 24 4
gpt4 key购买 nike

我正在尝试为网站轮播上的某些内容设置动画,以便根据按下的按钮向左或向右移动。这是我的示例代码:

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 的链接:http://codepen.io/leofontes/pen/NRgOxb

基本上,我的情况是,如果我先按按钮,它就会向左工作并动画,但是如果我按,则 停止工作,仅淡出但不会向左移动(向右仍然有效)。

知道为什么会发生这种行为或者如何解决它?谢谢你!如果需要更多信息,请告诉我

最佳答案

问题:在您的代码中,您尝试从右到左 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();
});


});

关于javascript - jQuery Animate Left 和 Right 只发生一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39755038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com