概述

element.scrollIntoView({ block, inline, behavior }) 控制目标元素在容器内的对齐方式与滚动动画。适用于目录导航与错误提示定位。

用法/示例


document.querySelector('#target').scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'smooth' })

工程建议

  • scroll-padding/scroll-margin 配合避免被固定头部遮挡。
  • 结合 prefers-reduced-motion 控制平滑滚动的启用与禁用。
  • 在可访问性场景测试键盘与读屏器行为,保证焦点与叙述稳定。

参考与验证

  • MDN:Element.scrollIntoView() — https://developer.mozilla.org/docs/Web/API/Element/scrollIntoView

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部