js-review/html/滑动底部判断.html

36 lines
986 B
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.parent {
height: 300px;
background-color: aquamarine;
overflow: scroll;
}
.son {
height: 1000px;
width: 80%;
margin: 0 auto;
background: linear-gradient(to bottom, #ff0000, #00ff00); /* 从上到下的颜色渐变 */
}
</style>
</head>
<body>
<div class="parent">
<div class="son"></div>
</div>
<script>
let parent = document.querySelector(".parent")
let son = document.querySelector(".son")
// 元素的ScrollTop(卷上去的高度) + clientHeight (元素盒子模型的内容高度 content + padding) >= scrollHeight(元素能滚动的高度)
parent.onscroll = function(){
if (parent.scrollTop + parent.clientHeight > parent.scrollHeight - 10) {
alert("到底部了")
}
}
</script>
</body>
</html>