<!DOCTYPE html><html><head> <title>滚动</title><style> #box{ width: 100%; height: 2000px; border: 1px solid green; }</style></head><body style="margin:0px;padding:0px;"><div id="box"></div> <script type="text/javascript"> 'use strict'; window.onscroll = function() { //为了保证兼容性,这里取两个值,哪个有值取哪一个 //scrollTop就是触发滚轮事件时滚轮的高度 var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; if(scrollTop){alert("滚动检测成功!"); } console.log("滚动距离" scrollTop); } </script></body></html>
评论