Set scroll position

Javascript

Javascript Problem Overview


I'm trying to set the scroll position on a page so the scroller is scrolled all the way to the top.

I think I need something like this but it's not working:

(function () { alert('hello'); document.body.scrollTop = 0; } ());

Any ideas?

Javascript Solutions


Solution 1 - Javascript

You can use window.scrollTo(), like this:

window.scrollTo(0, 0); // values are x,y-offset

Solution 2 - Javascript

Also worth noting window.scrollBy(dx,dy) (ref)

Solution 3 - Javascript

Note that if you want to scroll an element instead of the full window, elements don't have the scrollTo and scrollBy methods. You should:

var el = document.getElementById("myel"); // Or whatever method to get the element

// To set the scroll
el.scrollTop = 0;
el.scrollLeft = 0;

// To increment the scroll
el.scrollTop += 100;
el.scrollLeft += 100;

You can also mimic the window.scrollTo and window.scrollBy functions to all the existant HTML elements in the webpage on browsers that don't support it natively:

Object.defineProperty(HTMLElement.prototype, "scrollTo", {
    value: function(x, y) {
        el.scrollTop = y;
        el.scrollLeft = x;
    },
    enumerable: false
});

Object.defineProperty(HTMLElement.prototype, "scrollBy", {
    value: function(x, y) {
        el.scrollTop += y;
        el.scrollLeft += x;
    },
    enumerable: false
});

so you can do:

var el = document.getElementById("myel"); // Or whatever method to get the element, again

// To set the scroll
el.scrollTo(0, 0);

// To increment the scroll
el.scrollBy(100, 100);

NOTE: Object.defineProperty is encouraged, as directly adding properties to the prototype is a breaking bad habit (When you see it :-).

Solution 4 - Javascript

... Or just replace body by documentElement:

document.documentElement.scrollTop = 0;

Solution 5 - Javascript

If you want to set the scroll position of document.body, you can scroll the entire window altogether using window.scrollTo(); it takes either a pair of coordinates (x,y) or an options object – if you just want to scroll nicely to the top, try window.scrollTo({top:0,behavior:'smooth'});.

However, in some instances, you have an element to scroll (and not the entire document). For that case, elements also provide a scrollTo() method using the same arguments.

document.querySelector('ul#list').scrollTo(0,0);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike RifginView Question on Stackoverflow
Solution 1 - JavascriptNick CraverView Answer on Stackoverflow
Solution 2 - JavascriptannakataView Answer on Stackoverflow
Solution 3 - JavascriptJorge Fuentes GonzálezView Answer on Stackoverflow
Solution 4 - Javascriptmaxime schoeniView Answer on Stackoverflow
Solution 5 - JavascriptdakabView Answer on Stackoverflow