srcollTop、clientHeight、scrollHeight详解

原生js:

scrollTop  ——   滚动条滚动的距离(注意:不是滚动条距顶端的距离)
scrollHeight —— 文档内容的实际高度,包含超出视图溢出的部分 
clientHeight ——  窗口可视范围的高度

当 clientHeight + scrollTop >= scrollHeight 时,表示已经抵达内容的底部了,可以加载更多内容。

scrollTop:

可以通过 window.pageYOffset 或  document.documentElement.scrollTop 来获取

scrollHeight:

可以通过 document.documentELement.scrollHeightdocument.bodyscrollHeight

clientHeight:

可以通过 window.innerHeight document.documentElement.clientHeight 来获取

 

下边这张图很清晰的说明了三者的表现:

补充

与之对应的jQuery中三者的表达方式:

<script>
    $(window).on("resize scroll",function() {
        var windowHeight = $(window).height();//当前窗口的高度             
        var scrollTop = $(window).scrollTop();//当前滚动条从上往下滚动的距离            
        var docHeight = $(document).height(); //当前文档的高度 
        console.log(scrollTop, windowHeight, docHeight);
        // 触底公式:(滚动条滚动的高度 + 可视窗口的高度 = 文档的高度)  这个是基本的公式  
        if (scrollTop + windowHeight >= docHeight) { 
            console.log("===加载更多数据===");
        }
    });
</script>
$(document).height();  ==  document.documentElement.scrollHeight;    // 文档内容的实际高度
$(window).scrollTop(); == document.documentElement.scrollTop;        // 滚动条滚动高度
$(window).height(); == document.documentElement.clientHeight;        // 可视窗口高度

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值