<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>domo</title>
<style>
.top{
width: 100%;
height: 200px;
background-color: red;
}
.body{
height: 10000px;
width: 100%;
background-color: yellow;
}
#box {
width: 100px;
height: 100px;
position: fixed;
top: 100px;
right: 100px;
opacity: 0;
background: blue;
}
</style>
</head>
<body>
<div class="top"></div>
<div class="body"></div>
<div id="box"></div>
<script>
window.onscroll = function () {
juli=document.documentElement.scrollTop;
if(juli>200){
box.style.opacity=1;
}
box.onclick=function(){
juli=document.documentElement.scrollTop;
var move=setInterval(function(){
juli=juli/2;
if(juli<=300){
clearInterval(move)
document.documentElement.scrollTop=0
}
else{
document.documentElement.scrollTop=juli
}
},200)/
}
}
</script>
</body>
</html>
样式展示
回顶部
需求分析
回到顶部,变速
实现原理
把回到的距离折半返回,直到剩下一小部分直接返回顶部