js实现返回顶部按钮


html:





css:

.box {
width: 100%;
height: 100px;
background: skyblue;
}

.box1 {
width: 100%;
height: 2000px;
background: lightgray;
}

#btn {
position: fixed;
right: 20px;
bottom: 50px;
border: none;
height: 30px;
width: 80px;
display: none;
}

js:

var oBtn=document.getElementById("btn")
window.onscroll=function () {
if (document.documentElement.scrollTop> 800) {
oBtn.style.display='block';
} else{
oBtn.style.display='none';
}
}
btn.onclick=function () {
document.documentElement.scrollTop='0'
}