项目里需要用到屏幕滚到的行为。查询api 之后发现了滚动接口中的两个。然后又很奇怪他们直接的区别,应该如何选择。
首先时mdn的解释,这里说一句,mdn此处的翻译不是特别的准确,也可能是按照功能去翻译的。
scroll()
The scroll() method of the Element interface scrolls the element to a particular set of coordinates inside a given element.
scrollTo()
The scrollTo() method of the Element interface scrolls to a particular set of coordinates inside a given element.
看起来区别不大,然后看一下规范草案给出的定义
放上链接: https://drafts.csswg.org/cssom-view/#dom-element-scrollto
核心表述是这句话 Let document be the element’s node document. 这个是浏览器实现的功能。
往下看 还有一句话: When the scrollTo() method is invoked, the user agent must act as if the scroll() method was invoked with the same arguments.
也就是说这俩对于浏览器实现的时候表现要一致。
总结来说,确实没什么区别。至于为什么有两个,我也不清楚。浏览器具体怎么去实现规范,这个就视情况而定了。

本文探讨了MDN中关于scroll()与scrollTo()方法的差异,指出两者在功能上几乎相同,浏览器实现时表现一致。文章分析了规范草案,揭示了调用scrollTo()方法等同于调用scroll()方法。

2517

被折叠的 条评论
为什么被折叠?



