方法一:
设置滚动条的页面添加 ,或者在html中添加
scroll-behavior: smooth;
设置了改属性后,点击锚点定位的元素后,滚动条就会 平滑的滚动到定位的位置
方法二:
Dom.scrollIntoView()
scrollIntoView() 方法让当前的元素滚动到浏览器窗口的可视区域内。该方法支持传入一个 scrollIntoViewOptions 对象,其中的 behavior 属性定义滚动时的动画过渡效果,可选值为 auto 或 smooth,默认值为 auto,无动画效果;smooth 可使页面平滑滚动。
document.getElementById(i)?.scrollIntoView({
behavior: "smooth"
})
方法三:
window.scrollTo(x, y) 可以通过传入文档(HTML 中的 document)中的x轴坐标和y轴坐标将页面滚动到某个位置。该 API 还支持传入一个 options 对象,其中left属性等同于x轴坐标;top属性等同于y轴坐标;behavior 属性表示滚动行为,其类型为 String,可选值有 3 个:smooth (平滑滚动 )、instant (瞬间滚动)、auto (默认值)。auto 实测效果等同于 instant 。
document.getElementById(i)?.scrollo({
left:0,
top:1,
behavor:'auto'
})