Login
芋圆社区 > 编程 > JavaScript > 滚动条滚动到底部的判断

滚动条滚动到底部的判断

654
0
2022-12-05
2022-12-05
Hey、小怪兽


JavaScript


  • • 完整代码如下:
  • <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <title>Document</title>
            <style>
                div {
                    width: 100px;
                    height: 500px;
                    background-color: grey;
                }
            </style>
        </head>
        <body>
            <div>123</div>
            <div>123</div>
            <div>123</div>
    
            <script>
                // 滚动条在Y轴上的滚动距离
                function getScrollTop() {
                    var scrollTop = 0,
                        bodyScrollTop = 0,
                        documentScrollTop = 0;
    
                    if (document.body) {
                        bodyScrollTop = document.body.scrollTop;
                    }
                    if (document.documentElement) {
                        documentScrollTop = document.documentElement.scrollTop;
                    }
                    scrollTop = bodyScrollTop - documentScrollTop > 0 ? bodyScrollTop : documentScrollTop;
                    return scrollTop;
                }
                // 文档的总高度
                function getScrollHeight() {
                    var scrollHeight = 0,
                        bodyScrollHeight = 0,
                        documentScrollHeight = 0;
                    if (document.body) {
                        bodyScrollHeight = document.body.scrollHeight;
                    }
                    if (document.documentElement) {
                        documentScrollHeight = document.documentElement.scrollHeight;
                    }
                    scrollHeight = bodyScrollHeight - documentScrollHeight > 0 ? bodyScrollHeight : documentScrollHeight;
                    return scrollHeight;
                }
                // 浏览器视口的高度
                function getWindowHeight() {
                    var windowHeight = 0;
                    if (document.compatMode == "CSS1Compat") {
                        windowHeight = document.documentElement.clientHeight;
                    } else {
                        windowHeight = document.body.clientHeight;
                    }
                    return windowHeight;
                }
    
                window.onscroll = function () {
                    if (getScrollTop() + getWindowHeight() == getScrollHeight()) {
                        alert("到底部了!");
                    }
                };
            </script>
        </body>
    </html>

jQuery


  • • 完整代码如下:
  • <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <title>Document</title>
            <style>
                div {
                    width: 100px;
                    height: 500px;
                    background-color: grey;
                }
            </style>
        </head>
        <body>
            <div>123</div>
            <div>123</div>
            <div>123</div>
    
            <!-- 自己的jquery文件 -->
            <script src="./jquery-3.5.1.min.js"></script>
            <script>
                $(window).scroll(function () {
                    let winH = $(window).height();
                    let wsT = $(window).scrollTop();
                    let docH = $(document).height();
    
                    if (winH + wsT == docH) {
                        alert("到底部啦!");
                    }
                });
            </script>
        </body>
    </html>

上一篇:AES加密 CBC模式

下一篇:console.log展开值不一样

Message Board
回复
回复内容不允许为空
留言字数要大于2,小于200!
提交成功,5s后刷新页面!
编程导航

Array 基础方法

Array 进阶方法 - ES6

公主连结.cysp转.skel

获取视频的第一帧

定制Console.log

WebAssembly简单案例

一些常用的方法【持续更新】

AES加密 CBC模式

滚动条滚动到底部的判断

console.log展开值不一样

JSON解析与序列化

|| 运算符的坑

对象转为字符串

call,apply,bind

获取数组中对象的某一属性值

jQuery合并ajax请求

批量命名全局变量

forEach方法

round,ceil,floor

Copyright © 2020 芋圆社区

Powered by 浙ICP备2020039309号-1

此页面不支持夜间模式!

已进入夜间模式!

已进入普通模式!

搜索框不允许为空

签到成功!经验+5!芋圆币+2!

签到失败!今日已签到!

需要登录社区账号才可以进入!

复制成功
寄,页面未加载完成或页面无锚点