功能:
拖动滚动条到一定的距离显示返回顶部
思路:
1.先写一个返回顶部盒子,固定到页面的某个位置。隐藏元素
2.监听滚动事件
3.滚动到指定位置显示
代码:
<div class="return-top">
返回顶部
</div>
var top = document.querySelector('.return-top') // 获取元素加过度效果 (用ref也可以)
window.addEventListener('scroll',()=>{
// 滚动大于500显示
if(document.documentElement.scrollTop > 500 ){
top.style.opacity = '1'
top.style.transition = 'all 1s'
}else{
top.style.opacity = '0'
top.style.transition = 'all 1s'
}
})
本文介绍了一种在网页中实现滚动条返回顶部的功能。通过监听滚动事件,当滚动距离超过设定阈值时,显示返回顶部按钮。使用CSS和JavaScript进行过渡效果设置,提升用户体验。
&spm=1001.2101.3001.5002&articleId=107538359&d=1&t=3&u=a72216f3286b4c2e8b1063a6d9a5b32d)
814

被折叠的 条评论
为什么被折叠?



