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.
Definition and Usage The scrollTo() method scrolls the document to specified coordinates.
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)");
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.
window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.
Try the alternative :
document.body.scrollTop = 0;
I my case removing this block fixed the issue
html, body {
overflow-x: hidden;
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With