1. 程式人生 > >JS判斷滾動條是否停止滾動

JS判斷滾動條是否停止滾動

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> New Document </title>
  <script type="text/javascript">
  <!--
	// 讓瀏覽器出現滾動條
	for(var i = 0; i < 100; i++) {
		document.write("<br/>");
	}
 
	var topValue = 0,// 上次滾動條到頂部的距離
		interval = null;// 定時器
	document.onscroll = function() {
		if(interval == null)// 未發起時,啟動定時器,1秒1執行
			interval = setInterval("test()", 1000);
		topValue = document.documentElement.scrollTop || document.body.scrollTop;;
	}
 
	function test() {
		// 判斷此刻到頂部的距離是否和1秒前的距離相等
                var t1 = document.documentElement.scrollTop || document.body.scrollTop;
		if( t1 == topValue) {
			alert("scroll is stopping!");
			clearInterval(interval);
			interval = null;
		}
	}
  //-->
  </script>
 </head>
 <body>
 </body>
</html>