直播平台搭建,监听div滚动高度
直播平台搭建,监听div滚动高度
监听窗口的滚动
<!DOCTYPE html>
<html>
<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>window滚动事件</title>
</head>
<body style="height: 2000px;">
<button onclick="scrollWindow()" style="position: fixed; bottom: 0px;">返回顶部</button>
<script>
// 想在页面滚动的时候获取相关信息,试试下面的操作
window.onscroll = () => {
console.log('document.documentElement.scrollTop:', document.documentElement.scrollTop)
// todo 处理具体的页面逻辑
}
function scrollWindow() {
document.documentElement.scrollTop = 0
}
</script>
</body>
</html>
监听div容器的滚动
<!DOCTYPE html>
<html>
<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>
#box {
height: 200px;
width: 300px;
overflow: scroll;
width: 300px;
}
.content {
width: 100%;
height: 400px;
background-color: antiquewhite;
}
</style>
</head>
<body>
<div id="box">
<div>
比父级高的容器
</div>
</div>
<div>
<textarea id="text"></textarea>
</div>
<button onclick="append()">send</button>
<button onclick="toTop()">toTop</button>
<script>
var element = document.getElementById('box');
element.addEventListener('scroll', function(e) {
console.log('e.target.scrollTop:', e.target.scrollTop)
})
function toTop() {
element.scrollTop = 0
}
function append() {
let p = document.createElement("p");
let val = document.getElementById('text').value
var textnode=document.createTextNode(val);
p.appendChild(textnode);
element.appendChild(p)
document.getElementById('text').value = ''
// 滚动到底部
element.scrollTop = element.scrollHeight;
}
</script>
</body>
</html>
以上就是 直播平台搭建,监听div滚动高度,更多内容欢迎关注之后的文章