页面:
<style type="text/css">
#b{
width: 500px;
height: 300px;
top: 10px;
left:30%;
overflow:scroll;
border: 1px solid red;
}
</style>
<body>
<div id="a">
<div id="b" onscroll="scrollxiaoxi()">
<div id="c">
<h5>asdf</h5>
<h5 >asdf1</h5>
<h5 >asdf2</h5>
<h5>asdf3 </h5>
<h5>asdf 4</h5>
<h5>asdf 5</h5>
<h5>asdf 6</h5>
<h5>asdf 7</h5>
<h5>asdf 8</h5>
<h5>asdf 9</h5>
<h5>asdf 0</h5>
<h5>asdf 11-</h5>
<h5>asdf 12</h5>
<h5>asdf 13</h5>
<h5>asdf 14</h5>
<h5>asdf15 </h5>
<h5>asdf16 </h5>
<h5>asdf17 </h5>
<h5>asdf18 </h5>
<h5>asdf 19</h5>
</div>
<div id="msg_end" style="height:0px; overflow:hidden"></div>
</div>
</div>
<div><button type="button" onclick="start()">开始刷新</button></div>
<div><button type="button" onclick="end()">暂停刷新</button></div>
<div><button type="button" onclick="again()">重新刷新</button></div>
JS部分:
//设置视图初始化为底部
<script type="text/javascript">
$(function () {
msg_end.scrollIntoView();
})
var timer;
var c=["1","2","3","4","5","6","7","8","9","10","11","12","13"];
timer=window.setInterval(sx, 500);
function sx(){
$("#c").text("")
for (i=0;i<c.length;i++){
$("#c").append(
"<h5>"+c[Math.floor((Math.random()*c.length))]+"</h5>"
)
}
msg_end.scrollIntoView();
}
function scrollxiaoxi () {
let scrollTop = document.querySelector('#b').scrollTop || document.body.scrollTop;
let clientHeight = document.querySelector('#b).clientHeight || document.body.clientHeight;
let scrollHeight = document.querySelector('#b').scrollHeight || document.body.scrollHeight;
// scrollTop(卷起来的高度) + clientHeight(可视高度) === scrollHeight(总高度)//判断是否在底部
// scrollHeight > clientHeight避免在空的时候刷新
if(scrollHeight > clientHeight && scrollTop + clientHeight === scrollHeight) {
if (timer) {
return timer;
} else {
timer = setInterval(sx, 500);
}
}else {
if (timer) {
timer = clearInterval(timer);
} else {
return;
}
}
}
//开始
function start() {
if (timer) {
return timer;
} else {
timer = setInterval(sx, 500);
}
}
function end() {
if (timer) {
timer = clearInterval(timer);
} else {
return;
}
}
function again(){
clearInterval(timer);
timer = setInterval(sx, 500);
}
</script>