當用戶滾動并嘗試使用 .animate() 方法時,我想讓影像彈出和彈出視圖。它在技術上有效,但非常滯后,有時甚至在視窗滾動回頂部時都不會回傳。這是一個示例小提琴:EXAMPLE
$(window).scroll(function(){
if($(this).scrollTop() > 50){
$('.rot-frog').animate({
'bottom': '-80',
'right': '-40'
}, 500);
}else{
$('.rot-frog').animate({
'bottom': '-12',
'right': '-6'
}, 500);
}
});
我沒有收到控制臺錯誤,我嘗試了各種 CSS 屬性,但得到了相同的延遲結果。所以我假設它與 .scroll() 和 .animate() 組合有關,我只是不知道為什么。我試過搜索類似的問題,但無法找到我想要的。有人可以告訴我為什么會這樣嗎?
uj5u.com熱心網友回復:
scroll 事件在滾動移動期間(不是在滾動結束時)觸發,因此要執行大量影片功能。
如果每次向上或向下滾動移動只呼叫一次 animate 函式,問題就會解決。
嘗試這個:
var animateOnce = true;
$(window).scroll(function(){
if($(this).scrollTop() > 50 && animateOnce == true){
animateOnce = false;
$('.rot-frog').animate({
'bottom': '-80',
'right': '-40'
}, 500);
}else if($(this).scrollTop() <= 50 && animateOnce == false){
animateOnce = true;
$('.rot-frog').animate({
'bottom': '-12',
'right': '-6'
}, 500);
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348603.html
