<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-2.2.3.min.js"></script>
<script src="jquery-ui.js"></script>
<script>
var x=0
setInterval(function(){
var Scrolltop=$(document).scrollTop()
if(x!=Scrolltop){alert(Scrolltop)
x=Scrolltop
}
},500)
</script>
</head>
<body>
<div style="height: 2000px"></div>
</body>
</html>
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-2.2.3.min.js"></script>
<script src="jquery-ui.js"></script>
<script>
var x=0
setInterval(function(){
var Scrolltop=$(document).scrollTop()
if(x!=Scrolltop){alert(Scrolltop)
x=Scrolltop
}
},500)
</script>
</head>
<body>
<div style="height: 2000px"></div>
</body>
</html>
如果使用jquery的话,可以这样写:
$(window).bind("scroll", function(){var top = $(this).scrollTop() // 当前窗口的滚动距离
})
如果使用原生js,可以这样写(摘自网上的):
/*** 获取<a href="https://www.baidu.com/s?wd=%E6%BB%9A%E5%8A%A8%E6%9D%A1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YzPARYuhu9m1n3mhDknWmd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHT1nWmLnW6snH03nHnkPHbYPs" target="_blank" class="baidu-highlight">滚动条</a>距离顶端的距离
* @return {}支持IE6
*/
function getScrollTop() {
var scrollPos
if (window.pageYOffset) {
scrollPos = window.pageYOffset }
else if (document.compatMode && document.compatMode != 'BackCompat')
{ scrollPos = document.documentElement.scrollTop }
else if (document.body) { scrollPos = document.body.scrollTop }
return scrollPos
}