當我使用jQuery時,我並不十分精明,而且我正在開發一個項目,該項目涉及在手機中爲動畫部分製作動畫,以便向下滾動演示手機中的內容。我有兩個手機,並排,有動畫。它的工作方式是左側滾動,暫停,然後右側滾動,然後暫停。他們會這樣做直到他們結束。他們也有一個球形圖標,可以與它一起動畫。jQuery scrollTop動畫循環問題
我得到它的工作,但該項目的範圍是當用戶滾動到頁面上的該部分時,手機內的滾動動畫將動畫。當我試圖讓它工作時,動畫循環回到我不希望它們做的頂部。我不知道是什麼導致了這個問題。
如果任何人都可以得到任何幫助,我會非常感激。我爲我寫得不好的jQuery提前道歉。如果有更好的方法來編寫動畫,我會修改它。
這是原來的jQuery爲我工作:
$(function(){
$(".portrait .mobile-container").animate({ scrollTop: 560 }, 1200).delay(3000).animate({ scrollTop: 1200 }, 1200);
$(".portrait .ball").animate({ top: 720 }, 1200, "linear").delay(3000).animate({ top: 1280 }, 1200, "linear");
$(".landscape .mobile-container").delay(2000).animate({ scrollTop: 900 }, 1500).delay(3000).animate({ scrollTop: 1300 }, 1500);
$(".landscape .ball").delay(2000).animate({ top: 1000 }, 1300, "linear").delay(3000).animate({ top: 1300 }, 1300, "linear");
});
這是我遇到的問題之一。你需要滾動到與手機的部分觸發動畫開始播放:
$(window).scroll(function() {
$('.mobile-device').each(function(){
var imagePos = $(this).offset().top;
var topOfWindow = $(window).scrollTop();
if (imagePos < topOfWindow+900) {
$('.portrait-contain', this).delay(1000).animate({ scrollTop: 560 }, 1000).delay(3000).animate({ scrollTop: 1590 }, 1000);
$('.portrait-contain .ball', this).delay(1000).animate({ top: 720 }, 1000, "linear").delay(3000).animate({ top: 1700 }, 1000, "linear");
$('.landscape-contain', this).delay(3000).animate({ scrollTop: 590 }, 1000).delay(3000).animate({ scrollTop: 1200 }, 1000);
$('.landscape-contain .ball', this).delay(3000).animate({ top: 650 }, 1000, "linear");
}
});
});
您希望動畫只有當你的手機可見時纔開始,對嗎? – Morven
是的,當你向下滾動到手機時,動畫開始,只有當用戶滾動到2手機部分並且動畫不應該重複時纔開始動畫 – user933061