Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / CSS window.scrollTo(0,0) not working

Tags:

javascript

css

I am building a web app, and part of that web app needs to scroll to the top when an action is performed. This is handled using window.scrollTo(0,0) which works perfectly until I add some CSS styling below (which I require for this project).

Has anybody got any idea why the CSS below would stop window.scrollTo(0,0) working?

* {
    -webkit-transform: translate3d(0,0,0);
    -moz-transform: translate3d(0,0,0);
    -ms-transform: translate3d(0,0,0);
    -o-transform: translate3d(0,0,0);
    transform: translate3d(0,0,0);
}

P.S. I cannot use jQuery in my situation, just in case it's suggested that I do that.

like image 639
user1873468 Avatar asked Jan 31 '13 09:01

user1873468


People also ask

What is window scrollTo?

Definition and Usage The scrollTo() method scrolls the document to specified coordinates.

How do you scroll down in JavaScript?

Scenario 1: To scroll down the web page by pixel. Javascript method ScrollBy() scrolls the web page to the specific number of pixels. The syntax of ScrollBy() methods is : executeScript("window. scrollBy(x-pixels,y-pixels)");

How do you scroll up in JavaScript?

Method 1: Using window.scrollTo() The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.

How do I move to the top of the page in HTML?

window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.


3 Answers

Try the alternative :

document.body.scrollTop = 0;
like image 166
basarat Avatar answered Sep 21 '22 21:09

basarat


I my case removing this block fixed the issue

html, body {
  overflow-x: hidden;
}
like image 25
Dima Avatar answered Sep 20 '22 21:09

Dima


Turns out there was not a set height on the main element in the body, which meant scroll to wasnt working however the transition CSS was giving the element height automatically in webkit.... Fixed the CSS to give it a set height and it works again!

like image 21
user1873468 Avatar answered Sep 17 '22 21:09

user1873468