在 safari 浏览器中,scrollIntoView 无法按预期工作

Ran*_*h M 13 javascript safari scroll

我有一个可滚动的 div,里面有一个 SVG,其中包含给定 id 的点。我想将选定的点与 div 的中心对齐,我有这个解决方案

document.getElementById("myID").scrollIntoView({ 
    behavior: 'smooth', 
    block: 'nearest', 
    inline: 'center' 
 })
Run Code Online (Sandbox Code Playgroud)

这在 chrome 中工作正常,但在 safari 中不起作用,最好的解决方案是什么?

jma*_*svt 13

Safari 不支持此功能。具体来说,scrollIntoViewOptions不支持,因此您需要为此找到一个填充或完全采用另一种方法。

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

  • 这个库解决了这个问题,https://www.npmjs.com/package/seamless-scroll-polyfill (13认同)
  • @RanjithM 提议的图书馆很有魅力!谢谢。谢谢。 (2认同)