項目裏需要用到屏幕滾到的行爲。查詢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.
也就是說這倆對於瀏覽器實現的時候表現要一致。
總結來說,確實沒什麼區別。至於爲什麼有兩個,我也不清楚。瀏覽器具體怎麼去實現規範,這個就視情況而定了。