监听是否到达页面滑动的可视区域最底部

本文介绍了如何使用JavaScript获取网页的滚动位置、总高度及浏览器视口高度,并通过监听滚动事件实现特定功能,如判断页面是否滚动到底部。
 1 function getScrollTop(){
 2                 var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
 3                 if(document.body){
 4                 bodyScrollTop = document.body.scrollTop;
 5               }
 6               if(document.documentElement){
 7                 documentScrollTop = document.documentElement.scrollTop;
 8               }
 9               scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
10               return scrollTop;
11             }
12             //文档的总高度
13             function getScrollHeight(){
14                 var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
15                 if(document.body){
16                 bodyScrollHeight = document.body.scrollHeight;
17               }
18               if(document.documentElement){
19                 documentScrollHeight = document.documentElement.scrollHeight;
20               }
21               scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
22               return scrollHeight;
23             }
24             //浏览器视口的高度
25             function getWindowHeight(){
26                 var windowHeight = 0;
27               if(document.compatMode == "CSS1Compat"){
28                 windowHeight = document.documentElement.clientHeight;
29               }else{
30                 windowHeight = document.body.clientHeight;
31               }
32               return windowHeight;
33             }
34             //监听滚动条的事件内容
35             window.onscroll = function(){
36               if(getScrollTop() + getWindowHeight() == getScrollHeight()){
37                     console.log("getScrollTop:"+getScrollTop()+"**getWindowHeight:"+getWindowHeight()+"**getScrollHeight:"+getScrollHeight())
38               }
39             };

 

转载于:https://www.cnblogs.com/Salicejy/p/10839536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值