<button οnclick=\"returnTop()\" id=\"btnTop\" title=\"返回顶部\">返回顶部</button>
JavaScript:
// 当网页向下滑动 20px 出现\”返回顶部\” 按钮
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
console.log(121);
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById(\”btnTop\”).style.display = \”block\”;
} else {
document.getElementById(\”btnTop\”).style.display = \”none\”;
}
}
// 点击按钮,返回顶部
function returnTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
CSS:
/* return top */
#btnTop {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
border: none;
outline: none;
background-color: #89cff0;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 10px;
}
#btnTop:hover {
background-color: #1E90FF;
}