Salman
Salman

Reputation: 9447

Confusion about `Element.prototype.scrollTo`

I've always used window.scrollTo and never knew about Element.prototype.scrollTo. I am surprised by the discovery that it exists for Element too and works just fine.

But I tried looking for its documentation and couldn't find. My question is, should I safely use it without having to worry about browser quirks? Is there a documentation available for it? Or I should assume window.scrollTo documentation applies to Element.prototype.scrollTo as well?

I am doing

document.querySelector('.my-div').scrollTo({
    left: 10,
    top: 100,
    behavior: 'smooth'
})

Will this syntax support in all browsers? Including smooth scrolling?

Upvotes: 4

Views: 1984

Answers (2)

NTR
NTR

Reputation: 1355

The documentation is now updated. You can find it here with Window.scrollTo and Element.scrollTo: https://developer.mozilla.org/en-US/docs/Web/API/ScrollToOptions

IE and the old edge(not chromium based) don't seem to support it.

Upvotes: 1

Lucas
Lucas

Reputation: 423

This seems to be a non-standard method and is for example not supported in Internet Explorer 11.

IE11 Console output

I wouldn't recommend using it if you need to support many different browsers.

Upvotes: 1

Related Questions