jquery 如何判断鼠标向上向下移动

解决思路
javascript


当用户滚动指定的元素时,会发生 scroll 事件。html

scroll 事件适用于全部可滚动的元素和 window 对象(浏览器窗口)。java

scroll() 方法触发 scroll 事件,或规定当发生 scroll 事件时运行的函数。浏览器

触发 scroll 事件

语法

$(selector).scroll()


<html>
<head>
<script type="text/javascript" src="j.js"></script>
<script type="text/javascript">
x=0;
$(document).ready(function(){
var initTop = 0;
$(window).scroll(function(){
 var scrollTop = $(document).scrollTop();
 if(scrollTop > initTop){
  alert("下"); 
 } else {
  alert("上"); 
 }
 initTop = scrollTop;
});
  //});
});
</script>
</head>
<body>
<p>请试着滚动 DIV 中的文本:</p>
<div style="width:200px;height:1000px;overflow:scroll;">text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. 
<br /><br />
text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text.</div>
<p>滚动了 <span>0</span> 次。</p>
</body>

</html>ide