htm:
<div>
<a onclick="returnTop()" id="btnTop" title="返回顶部"><img src="./Image/labelpic/cd-top-arrow.svg"></a>
</div>
css:
#btnTop {
display: none;
text-align: center;
position: fixed;
bottom: 50px;
height: 50px;
width: 50px;
right: 10px;
z-index: 99;
border: none;
outline: none;
background-color: #14AAAA ;
color: white;
cursor: pointer;
padding: 10px;
border-radius: 2px;
}
#btnTop:hover {
background-color: #14AAAA;
opacity: 0.5;
}
js:
$(function () {
$(window).scroll(function () { //只要窗口滚动,就触发下面代码
// 当网页向下滑动 30px 出现"返回顶部" 按钮
if (document.body.scrollTop > 2000 || document.documentElement.scrollTop > 2500) {
document.getElementById("btnTop").style.display = "block";
} else {
document.getElementById("btnTop").style.display = "none";
}
});
});
// 点击按钮,返回顶部
function returnTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}