gpt4 book ai didi

javascript - animate() 并不总是有效

转载 作者:行者123 更新时间:2023-11-28 04:04:35 25 4
gpt4 key购买 nike

我正在尝试使用 jQuery 创建一个简单的 slider 。

$('.next').on('click', function() {
$('li').animate({'right': '400px'}, 300);
});

$('.back').on('click', function() {
$('li').animate({'left': 0}, 300);
});

问题是,当我单击带有 next 类的底部时,它只滑动一次,第二次单击不起作用。

我该如何解决这个问题?

https://jsfiddle.net/6t1wx95f/

最佳答案

简单,接下来是 $('li').animate({'left': '-=400px'}, 300);

$('.next').on('click', function() {
$('li').animate({'left': '-=400px'}, 300);
});

$('.back').on('click', function() {
if(parseInt($('li').css('left')) <= 0){
if(parseInt($('li').css('left')) >= -400){
$('li').animate({'left': "0"}, 300);
} else {
$('li').animate({'left': '+=400px'}, 300);
}
}
});
.slider {
overflow: hidden;
width: 500%;
}

.slider ul {
width: 500%;
}

.slider ul li {
float: left;
list-style: none;
position: relative;
transition: all 0.65s;
}

.next,
.back {
width: 100px;
padding: 5px;
text-align: center;
background-color: skyblue;
float: left;
margin: 10px;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slider">
<ul>
<li>
<img src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" alt="" class="active">
</li>
<li>
<img src="https://www.w3schools.com/css/img_lights.jpg" alt="">
</li>
<li>
<img src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg" alt="">
</li>
<li>
<img src="http://images.all-free-download.com/images/graphiclarge/a_london_cityscape_515129.jpg" alt="">
</li>
<li>
<img src="https://image.jimcdn.com/app/cms/image/transf/dimension=1920x400:format=jpg/path/s86d6d6c688ca86fa/image/ie4265a3cd27b2997/version/1451246087/image.jpg" alt="">
</li>
</ul>
</div>

<div class="back">Back</div>
<div class="next">Next</div>

关于javascript - animate() 并不总是有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46810970/

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