如何确定jQuery滚动事件的方向?我在寻找这样的东西:$(window).scroll(function(event){
if (/* magic code*/ ){
// upscroll code
} else {
// downscroll code
}});有什么想法吗?
3 回答
绝地无双
TA贡献1946条经验 获得超4个赞
scrollTop
scrollTop
var lastScrollTop = 0;$(window).scroll(function(event){ var st = $(this).scrollTop(); if (st > lastScrollTop){ // downscroll code } else { // upscroll code } lastScrollTop = st;});
蓝山帝景
TA贡献1843条经验 获得超7个赞
$(window).bind('mousewheel', function(event) { if (event.originalEvent.wheelDelta >= 0) { console.log('Scroll up'); } else { console.log('Scroll down'); }});
$(element).scroll
mousewheel
originalEvent
wheelDelta
mousewheel
event.preventDefault()
慕沐林林
TA贡献2016条经验 获得超9个赞
(function () { var previousScroll = 0; $(window).scroll(function(){ var currentScroll = $(this).scrollTop(); if (currentScroll > previousScroll){ alert('down'); } else { alert('up'); } previousScroll = currentScroll; });}()); //run this anonymous function immediately
添加回答
举报
0/150
提交
取消