js的scroll事件

 这个代码是在id为box的div里写了很多p标签然后溢出,给#box设置overflow:scroll;然后在#box里滚动条滚动时就会输出

<script>
    var box = document.getElementById("box");
    // 元素对象的onscroll事件,当元素内容发生滚动时触发,它是一个频繁触发的事件
    box.onscroll = function(){
        // console.log("div发生滚动了");
        
        // scrollTop属性表示元素纵向滚动偏移量
        // console.log(box.scrollTop);
        
        // scrollHeight属性表示元素的内容高度
        // console.log(box.scrollHeight);
        
        // 元素内容高度 - 元素本身高度 = 最大偏移量
        // 元素的当前偏移量等于最大偏移量时,说明滚动到底部了
        if((box.scrollHeight - box.clientHeight) == box.scrollTop){
            console.log("到底了");
        }
    }
</script>

猜你喜欢

转载自blog.csdn.net/Mr_Sunset/article/details/81195621
今日推荐