jquery判断页面滚动条是上滚还是下滚

2025-01-20 05:53:52
推荐回答(3个)
回答1:

function scroll( fn ) {
var beforeScrollTop = document.body.scrollTop,
fn = fn || function() {};
window.addEventListener("scroll", function() {
var afterScrollTop = document.body.scrollTop,
delta = afterScrollTop - beforeScrollTop;
if( delta === 0 ) return false;
fn( delta > 0 ? "down" : "up" );
beforeScrollTop = afterScrollTop;
}, false);
}

调用方法:
scroll(function(direction) { console.log(direction) });

回答2:

var top = 0;
$(document).scroll(function(){
if( $('body').scrollTop()>=top ){
console.warn('下');
}else{
console.info('上');
}
top = $('body').scrollTop();
});

回答3:

改进二楼
//监听滚动条
var top2 = 0;
$(window).scroll(function (event) {
var top = $(document).scrollTop();

$(document).scroll(function () {
if ($('body').scrollTop() >= top2) {
tip("下");
top2 = $('body').scrollTop();
} else {
tip("上");
top2 = top2 - 1;
}
});
});