使用CSS来实现平滑的滚动效果
需要平滑滚动效果的盒子加
scroll-behavior: smooth;
这个css属性即可
<style>
html {
height: 1000%;
background-color: lightblue;
scroll-behavior: smooth;
}
.box {
width: 100px;
height: 100px;
position: fixed;
right: 0;
background-color: lightcoral;
bottom: 100px;
}
</style>
<div class="box"></div>
<script>
document.querySelector('.box').onclick = () => document.documentElement.scrollTop = 0;
</script>