先看效果
滚动加载代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
html,body{
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
}
div{
height: 300px;
}
.box1{
background-color: #5F9EA0;
}
.box2{
background-color: #87CEEB;
}
.box3{
background-color: #8A2BE2;
}
.box4{
background-color: #EBCB69;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
<script type="text/javascript">
const body = document.querySelector('body')
body.addEventListener('scroll',e=>{
const target = e.target
const scrollTop = target.scrollTop
const scrollHeight = target.scrollHeight
const clientHeight = target.clientHeight
if(scrollTop+clientHeight+20>=scrollHeight){
const div = document.createElement('div')
const R = Math.floor(Math.random()*255+1)
const G = Math.floor(Math.random()*255+1)
const B = Math.floor(Math.random()*255+1)
div.style.backgroundColor = `rgb(${R},${G},${B})`
body.appendChild(div)
}
},false)
</script>
</html>
滚动动画代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
html,body{
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
}
.top{
position: fixed;
width: 50px;
height: 50px;
border-radius: 50%;
bottom: 10px;
right: 20px;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #87CEEB;
background-color: #3b6aeb;
cursor: pointer;
}
</style>
</head>
<body>
<div class="top">↑</div>
</body>
<script type="text/javascript">
const body = document.querySelector('body')
const topTo = document.querySelector('.top')
topTo.addEventListener('click',()=>{
let scrollTop = body.scrollTop
const time = setInterval(()=>{
body.scrollTo(0,scrollTop)
if(scrollTop>30){
scrollTop-=20
}else{
scrollTop--
}
if(scrollTop<=0) clearInterval(time)
},1)
},false)
</script>
</html>
完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
html,body{
margin: 0;
padding: 0;
height: 100%;
overflow: auto;
}
div{
height: 300px;
}
.box1{
background-color: #5F9EA0;
}
.box2{
background-color: #87CEEB;
}
.box3{
background-color: #8A2BE2;
}
.box4{
background-color: #EBCB69;
}
.top{
position: fixed;
width: 50px;
height: 50px;
border-radius: 50%;
bottom: 10px;
right: 20px;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
color: #87CEEB;
background-color: #3b6aeb;
cursor: pointer;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="top">↑</div>
</body>
<script type="text/javascript">
const body = document.querySelector('body')
body.addEventListener('scroll',e=>{
const target = e.target
const scrollTop = target.scrollTop
const scrollHeight = target.scrollHeight
const clientHeight = target.clientHeight
if(scrollTop+clientHeight+20>=scrollHeight){
const div = document.createElement('div')
const R = Math.floor(Math.random()*255+1)
const G = Math.floor(Math.random()*255+1)
const B = Math.floor(Math.random()*255+1)
div.style.backgroundColor = `rgb(${R},${G},${B})`
body.appendChild(div)
}
},false)
const topTo = document.querySelector('.top')
topTo.addEventListener('click',()=>{
let scrollTop = body.scrollTop
const time = setInterval(()=>{
body.scrollTo(0,scrollTop)
if(scrollTop>30){
scrollTop-=20
}else{
scrollTop--
}
if(scrollTop<=0) clearInterval(time)
},1)
},false)
</script>
</html>
写这么多是不是挺麻烦,自己写滚动逻辑是为了可以自定义滚动的速度,其实scrollTo这个方法自带滚动过渡的只要加上一个属性就行。
topTo.addEventListener('click',()=>{
body.scrollTo({behavior: "smooth",top:0})
},false)
效果相同