Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29159

scrollTo not working in Safari

I have this scrollable list

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

DEMO

Now, I can scroll programatically using

document.querySelector('ol').scrollTo(100);

But this doesn't work in Safari. Although this seems to be trivial, I cannot find the alternative (without using jQuery)

How to make the list scrollable in Safari?

Upvotes: 4

Views: 17692

Answers (2)

krishnaxv
krishnaxv

Reputation: 956

scrollTo is a property of window object. And you are trying to apply it on an element.

Use 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.

NOTE

document.querySelector(selectors) returns the first element within the document. If your document contains multiple <ol> elements, it will always return the first element.

To select specific element, you can assign an ID & refer the element by document.querySelector('#ID').

Hope it helps!

Upvotes: 3

Roberts Kalderauskis
Roberts Kalderauskis

Reputation: 72

I believe this will help you: Scroll to position WITHIN a div (not window) using pure JS

There is alternative way to get what you wished.

Upvotes: 0

Related Questions