JavaScript scrollTo method does nothing?
I fixed this problem with using a setTimout. I was using angularjs but maybe it can help in vanilla js too.
setTimeout(function () {
window.scrollTo(0, 300);
},2);
Sometimes it's not just the CSS issue, for me the browser was the culprit. I had to solve it with this code:
if ('scrollRestoration' in window.history) {
window.history.scrollRestoration = 'manual'
}
It lets developer take the ownership of scroll changes. Read more about it here
I was able to resolve this problem using jQuery method animate(). Here is an example of the implementation I went with:
$('#content').animate({ scrollTop: elementOffset }, 200);
The selector is getting the div with ID = "content". I am then applying the animate method on it with scrollTop as an option. The second parameter is the time in milliseconds for the animation duration. I hope this helps someone else.
If you have something like this:
html, body { height: 100%; overflow:auto; }
If both body and html have a height definition of 100% and also scrolling enabled, window.scrollTo (and all derived scrolling mechanisms) do not work, despite a scrollbar being displayed (which can be used by the user), when the contents exceed that 100% body height. This is because the scrollbar you see is not that of the window, but that of the body.
Solution:
html { height: 100%; overflow:auto; }
body { height: 100%; }