H5移动端实现回到顶部的具体代码
时间: 2023-03-31 09:00:29 浏览: 161
可以使用以下代码实现H5移动端回到顶部功能:
HTML:
<button id="back-to-top">回到顶部</button>
CSS:
#back-to-top {
position: fixed;
bottom: 20px;
right: 20px;
display: none;
}
JS:
window.onscroll = function() {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > 200) {
document.getElementById('back-to-top').style.display = 'block';
} else {
document.getElementById('back-to-top').style.display = 'none';
}
}
document.getElementById('back-to-top').onclick = function() {
document.documentElement.scrollTop = document.body.scrollTop = ;
}
阅读全文