平滑滚动到页面元素scrollIntoView

// 获取目标元素
const tableSection = document.querySelector('.data-table-section')
// 平滑滚动到目标元素
tableSection.scrollIntoView({ behavior: 'smooth', block: 'start' })
  1. 首先通过 document.querySelector('.data-table-section') 获取到目标 DOM 元素
  2. 调用元素的 scrollIntoView() 方法,并传入配置对象:
    • behavior: 'smooth':指定滚动方式为平滑过渡(默认是 auto 即时跳转)
    • block: 'start':设置元素与可视区域的垂直对齐方式为顶部对齐(可选值还有 centerendnearest
posted @ 2025-10-21 16:44  青云码上  阅读(2)  评论(0)    收藏  举报