我正在尝试为一些应该从右侧进入屏幕的卡片设置动画,在中间停一会儿,然后在无限循环中消失到左侧。这是我尝试过的:
function startAnimation(elem) {
$('#' + elem).fadeIn(150).animate({
left: '0'
}, 1500);
}
function endAnimation(elem) {
$('#' + elem).animate({
left: '-200%'
}, 1500);
$('#' + elem).fadeOut(100).animate({
left: '200%'
}, 300);
}
function scrollCards(elem, n) {
startAnimation(elem);
setTimeout(function() {
endAnimation(elem);
}, 700);
elem += 1;
elem = elem == n ? 0 : elem;
return elem;
}
n = 3;
var card = 0
var firstAnimationDone = false;
$('#0').fadeIn(150);
setInterval(function() {
if (!firstAnimationDone) {
endAnimation(card);
card = 1;
}
card = scrollCards(card, n);
firstAnimationDone = true;
}, 4500);
/* (boxArticle is here just to keep static the part of the page where the animation takes place) */
.boxArticle {
overflow: hidden;
height: 100px;
}
.boxAchievements {
position: relative;
height: 100px;
width: 100%;
left: 200%;
top: 5px;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="boxArticle">
<article class="boxAchievements" id="0">
<h2>My achievements</h2>
<p>Write 1</p>
</article>
<article class="boxAchievements" id="1">
<h2>My achievements</h2>
<p>Write 2</p>
</article>
<article class="boxAchievements" id="2">
<h2>My achievements</h2>
<p>Write 3</p>
</article>
</div>
当我添加setTimeout到scrollCards函数时,它会在中间停很长时间,无论我在方法中放置的间隔有多长,它都会使循环不同步,所以我有 2 张卡同时移动。