简体   繁体   English

scrollTo在Safari中不起作用

[英]scrollTo not working in Safari

I have this scrollable list 我有这个滚动列表

<ol>
    <li>...</li>
    ...
</ol>

DEMO DEMO

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. scrollTowindow对象的属性。 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. 有关scrollToscrollTop更多信息,请分别参考Mozilla / Window / scrollToMozilla / Element / scrollTop

NOTE 注意

document.querySelector(selectors) returns the first element within the document . document.querySelector(selectors)返回内的第一个元素document If your document 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 by document.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM