我製作了這個幻燈片,當你用.stop(true)
將它懸停在它上面時會暫停。當鼠標再次離開幻燈片時,它應該再次播放。從它運行的地方來看,它並不重要。 (這意味着它可以完全刪除舊的動畫隊列,然後重新開始).stop(true)似乎不能真正清除隊列嗎?
的事情是,如果我現在徘徊它的動畫過後繼續,直到達到其目標。它會停下來等待動畫完成所需的時間。我不確定發生了什麼事。
HTML:
<section class="photo-grid-slideshow">
<div class="photo-crop">
<h3>I wanna
<div class="xs-spacer"></div>
<a class="med-btn btn-white">Read more</a>
</h3>
<div class="photo-grid-container" style="background-image: url('Images and videos/odesza1.jpg');"></div>
</div>
<div class="photo-crop">
<h3>Dance
<div class="xs-spacer"></div>
<a class="med-btn btn-white">Read more</a>
</h3>
<div class="photo-grid-container" style="background-image: url('Images and videos/odesza3.jpg');"></div>
</div>
<div class="photo-crop">
<h3>With you
<div class="xs-spacer"></div>
<a class="med-btn btn-white">Read more</a>
</h3>
<div class="photo-grid-container" style="background-image: url('Images and videos/odesza2.png');"></div>
</div>
<div class="photo-crop">
<h3>With you
<div class="xs-spacer"></div>
<a class="med-btn btn-white">Read more</a>
</h3>
<div class="photo-grid-container" style="background-image: url('Images and videos/odesza4.jpg');"></div>
</div>
</section>
的CSS:
.photo-crop {
display: inline-block;
overflow: hidden;
float: left;
width: calc(100vw/3);
height: 100%;
padding: 0;
position: relative;
background-position: center center;
background-size: cover;
text-align: left;
}
.photo-grid-slideshow {
height: 300px;
display: inline-block;
min-width: 300%;
position: relative;
background: black;
padding: none;
overflow: hidden;
background: #444;
margin-left: 0;
left: 0;
}
腳本:
$(function(){
function animate(){
var p = $(".photo-grid-slideshow .photo-crop").css('width');
$(".photo-grid-slideshow .photo-crop:first-of-type").animate({marginLeft: '-=' + p}, 10000, "linear", function(){
$(this).css("margin-left", 0).appendTo('.photo-grid-slideshow');
animate(); // here we call it again
})
}
animate(); // start animation
})
$(".photo-grid-slideshow").mouseenter(function() {
$(".photo-grid-slideshow .photo-crop:first-of-type").stop().clearQueue();
})
$(".photo-grid-slideshow").mouseleave(function() {
$(function(){
function animate(){
var p = $(".photo-grid-slideshow .photo-crop").css('width');
$(".photo-grid-slideshow .photo-crop:first-of-type").animate({marginLeft: '-=' + p}, 10000, "linear", function(){
$(this).css("margin-left", 0).appendTo('.photo-grid-slideshow');
animate(); // here we call it again
})
}
animate(); // start animation
})
})
我明白了!感謝您提供詳細和明確的解釋。你的代碼有效,但出現了另一個問題由於它使每個動畫的時間相同,如果僅剩10px,它仍然會將該距離移動超過5000ms。但是現在我知道我可能可以解決這個問題!再次感謝你 :) –