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) });
var top = 0;
$(document).scroll(function(){
if( $('body').scrollTop()>=top ){
console.warn('下');
}else{
console.info('上');
}
top = $('body').scrollTop();
});
改进二楼
//监听滚动条
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;
}
});
});