如何检测滚动方向

当有人向下滚动某个元素时,我想运行一个函数。像这样:


 $('div').scrollDown(function(){ alert('down') });

 $('div').scrollUp(function(){ alert('up') });

但是这些功能不存在。有解决这个问题的方法吗?他们似乎能够做到。不幸的是源代码被压缩,所以没有运气... 


慕盖茨4494581
浏览 379回答 3
3回答

阿晨1998

我最终设法弄清楚了,所以如果有人在寻找答案:&nbsp;//Firefox&nbsp;$('#elem').bind('DOMMouseScroll', function(e){&nbsp; &nbsp; &nbsp;if(e.originalEvent.detail > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//scroll down&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log('Down');&nbsp; &nbsp; &nbsp;}else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//scroll up&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log('Up');&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;//prevent page fom scrolling&nbsp; &nbsp; &nbsp;return false;&nbsp;});&nbsp;//IE, Opera, Safari&nbsp;$('#elem').bind('mousewheel', function(e){&nbsp; &nbsp; &nbsp;if(e.originalEvent.wheelDelta < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//scroll down&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log('Down');&nbsp; &nbsp; &nbsp;}else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//scroll up&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log('Up');&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;//prevent page fom scrolling&nbsp; &nbsp; &nbsp;return false;&nbsp;});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JQuery