滚动条到底 监听

复制代码
<!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>
</head>

<body>

<style>
html,
body {
height: 100%;
width: 100%;
background-color: aliceblue;
}
</style>
<script>

//来自于MDN
//Element.scrollHeight 这个只读属性是一个元素内容高度的度量,包括由于溢出导致的视图中不可见内容。
//Element.scrollTop 属性可以获取或设置一个元素的内容垂直滚动的像素数。

// 如果元素滚动到底,下面等式返回true,没有则返回false.

// element.scrollHeight - element.scrollTop === element.clientHeight
// 当容器不滚动但有溢出的子容器时,这些检查可以确定容器能否滚动:

// window.getComputedStyle(element).overflowY === 'visible' window.getComputedStyle(element).overflowY !== 'hidden'

//文档高度
function getDocumentTop() {
let bodyScrollTop = document.body ? document.body.scrollTop : 0;
let documentScrollTop = document.documentElement ? document.documentElement.scrollTop : 0;
return (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
}

//可视窗口高度
function getWindowHeight() {
let windowHeight = 0;
if (document.compatMode == "CSS1Compat") {
windowHeight = document.documentElement.clientHeight;
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
}


//滚动条滚动高度
function getScrollHeight() {
let bodyScrollHeight = document.body ? document.body.scrollHeight : 0;
let documentScrollHeight = document.documentElement ? document.documentElement.scrollHeight : 0;
return (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
}


window.onscroll = function () {
//监听事件内容
if (getScrollHeight() == getWindowHeight() + getDocumentTop()) {

document.body.style.height = getScrollHeight() + 10 + 'px'
//当滚动条到底时,这里是触发内容
//异步请求数据,局部刷新dom
}
}
</script>
</body>

</html>
复制代码

 

posted @   小七要走  阅读(232)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示