[英]Error while detecting scroll direction
從這個問題開始- 如何確定jQuery滾動事件的方向? ,我編寫了這段代碼。
var lastS=$(window).scrollTop();
function whatHappened()
{
var currentS=$(window).scrollTop();
console.log(lastS,currentS);
if(currentS>lastS)
{
lastS = currentS;
return "Scroll down";
}
else
{
lastS = currentS;
return "Scroll up";
}
}
$(window).on("mousewheel",function(){
console.log(whatHappened());
});
這似乎工作正常,但有一個小問題。 如果我向下滾動兩次以上然后向上滾動,則首先顯示向下滾動然后向上滾動。 反之亦然。 怎么了? 請幫忙。 提前致謝。
JS小提琴: http : //jsfiddle.net/3843e/
mousewheel事件是非標准的-在您的情況下,它可能是在更新滾動信息之前觸發的,因此您始終在“上一個”滾動高度上進行操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.