2

Here is my problem, I am searching to set the scroll to top of the page on load but currently Chrome, IE and Firefox reset the scroll position to the same as it was on previous page, so how can I set scroll to top?

It seems that scrollTo is just ignored by those web browsers and I tried many over methods using scrollTo (find on SO) but they all do nothing...

Need help! Thank you all!

2
  • Browser automatically sets the scroller to the top. Some code must be changing it. Commented Sep 8, 2016 at 9:28
  • I'am sure thats not the case, many people have the same problem and it has been reported as a convenience feature of chrome for exemple he rememeber the last scroll position and automatically set it back on reload/load :3 edit: Thank you anyay :) Commented Sep 8, 2016 at 9:34

4 Answers 4

11

Okay, in case it can help some of you guys, here is what i'am using right now (i just found it, like 30 seconds after posting the question lmao...) and it seems to work:

window.onbeforeunload = function() {window.scrollTo(0,0);}

Edit: it might be because the browser remembers the last scroll pos, so setting it to top before unload makes, for exemple, chrome remembering 0,0 instead of x,y

Sign up to request clarification or add additional context in comments.

Comments

1

You could try somthing like this :

<!DOCTYPE html>
<html>
<body onload="myFunction()">

<script>
function myFunction() {
    document.body.scrollTop = document.documentElement.scrollTop = 0;
}
</script>

</body>
</html>

Hope it helps :)

1 Comment

Thank you for the answer dude :) but i already tryed this and... it does not work :/ maybe script conflict or stuff like that, but anyway thanks :)
1

This is our vanilla javascript implementation. It has a simple easing effect so that the user doesn't get shocked after clicking the To Top button.

Its very small and gets even smaller when minified. Devs looking for an alternative to the jquery method but want the same results can try this.

HTML

<button id="to-top">To Top</button>

JS

document.querySelector("#to-top").addEventListener("click", function(){

var toTopInterval = setInterval(function(){

    var supportedScrollTop = document.body.scrollTop > 0 ? document.body : document.documentElement;

    if (supportedScrollTop.scrollTop > 0) {
        supportedScrollTop.scrollTop = supportedScrollTop.scrollTop - 50;
    }

    if (supportedScrollTop.scrollTop < 1) {
        clearInterval(toTopInterval);
    }

}, 10);

 },false);

1 Comment

Thanks dude, i already finded how to do that in 1 line with Angular, but thanks for the help anyway :)! (i will remember it, in case i need to do that in JS :) )
0

you can use $anchorScroll()

and here is a documentation for it angular-js: anchor-Scroll attribute

1 Comment

Ho! thats look pretty cool, i will check that as an alternative, thanks dude :D!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.