[英]scrollTo not working in Safari
I have this scrollable list 我有这个滚动列表
<ol>
<li>...</li>
...
</ol>
Now, I can scroll programatically using 现在,我可以使用
document.querySelector('ol').scrollTo(100);
But this doesn't work in Safari. 但这在Safari中不起作用。 Although this seems to be trivial, I cannot find the alternative (without using jQuery)
尽管这似乎是微不足道的,但我找不到替代方法(不使用jQuery)
How to make the list scrollable in Safari? 如何在Safari中使列表可滚动?
scrollTo
is a property of window
object. scrollTo
是window
对象的属性。 And you are trying to apply it on an element
. 并且您正在尝试将其应用于
element
。
Use
element.scrollTop
使用
element.scrollTop
Code Snippet 代码段
document.querySelector('ol').scrollTop = 100;
It will do the trick! 它将成功!
For more information on scrollTo
& scrollTop
, refer Mozilla/Window/scrollTo & Mozilla/Element/scrollTop respectively. 有关
scrollTo
和scrollTop
更多信息,请分别参考Mozilla / Window / scrollTo和Mozilla / Element / scrollTop 。
NOTE
注意
document.querySelector(selectors)
returns the first element within thedocument
.document.querySelector(selectors)
返回内的第一个元素document
。 If yourdocument
contains multiple<ol>
elements, it will always return the first element.如果您的
document
包含多个<ol>
元素,它将始终返回第一个元素。To select specific element, you can assign an
ID
& refer the element bydocument.querySelector('#ID')
.要选择特定元素,可以分配一个
ID
并通过document.querySelector('#ID')
引用该元素。
Hope it helps! 希望能帮助到你!
I believe this will help you: Scroll to position WITHIN a div (not window) using pure JS 我相信这对您有帮助: 使用纯JS滚动到div(而不是窗口)中的位置
There is alternative way to get what you wished. 有另一种方式来获得您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.