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


<!DOCTYPE html>  
<html>  
<head>  
<meta charset="utf-8">  
<title>js判斷滾動條停止</title>  
<style type="text/css">
    .box {
        height: 3000px;
    }
</style>
<script type="text/javascript">  
    var topValue = 0, // 上次滾動條到頂部的距離
        interval = null; // 定時器
    document.onscroll = function() {
        if(interval == null) {
            // 未發起時,啟動定時器,1秒1執行
            interval = setInterval(function() {
                if(document.documentElement.scrollTop == topValue) {
                    alert("scroll bar is stopping!");
                    clearInterval(interval);
                    interval = null;
                }
            }, 1000);
        }
        topValue = document.documentElement.scrollTop;
    }  
</script>  
</head>  
    <div class="box"></div>
<body>  
</body>  
</html> 

這個需求是我在寫一個固釘的時候遇到的問題,當滾動條滾動時固釘隱藏,當停止滾動時固釘出現


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM