监听滚动已停止

本文介绍了一种使用JavaScript监听网页滚动并判断滚动是否停止的方法,通过设置定时器来检测滚动位置的变化,实现对页面滚动状态的精确捕捉。
<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1" />
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <link rel="stylesheet" type="text/css" href="css/font/iconfont.css">

</head>
<body>
    <div class="scroller-wrap" style="width:100%;height: 1900px;border: 1px red solid;">

    </div>
    <script>
        var isStopScroolTimer=null;
        var topValue=null;
        //获取滚动高度
        function getScrollTop()
        {
            var scrollTop=0;
            if(document.documentElement&&document.documentElement.scrollTop)
            {
                scrollTop=document.documentElement.scrollTop;
            }
            else if(document.body)
            {
                scrollTop=document.body.scrollTop;
            }
            return scrollTop;
        }
        document.onscroll=function(){   
            if(isStopScroolTimer == null) {
                isStopScroolTimer = setInterval("isStopScrool()", 100);  
            } 
        };

        function isStopScrool() {  
            // 判断此刻到顶部的距离是否和1秒前的距离相等  
            if(getScrollTop() == topValue) {   
                console.log('滑动结束'+getScrollTop());
                clearInterval(isStopScroolTimer);
                isStopScroolTimer=null;
            } else {
              topValue = getScrollTop();
          }
      } 
  </script>
</body>
</html>

 

转载于:https://www.cnblogs.com/theWayToAce/p/7144331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值