// 获取滚动条高度的示例代码
// 方法一:获取整个文档的高度
function getDocHeight() {
    const body = document.body;
    const html = document.documentElement;
    // 返回文档的高度,兼容不同浏览器
    return Math.max(
        body.scrollHeight, body.offsetHeight, 
        html.clientHeight, html.scrollHeight, html.offsetHeight
    );
}
// 方法二:获取当前窗口的可视高度
function getWindowHeight() {
    return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
// 方法三:获取滚动条的高度(即文档超出窗口部分的高度)
function getScrollHeight() {
    return getDocHeight() - getWindowHeight();
}
// 调用函数并输出结果
console.log("滚动条高度: " + getScrollHeight() + "px");Math.max 来确保返回最大值,以兼容所有浏览器。最后通过 console.log 输出滚动条的高度。
上一篇:js 打开新的窗口
下一篇:js img
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站