If you go to a page a and scroll around then refresh the page will refresh at the spot where you left it. This is great, however this also occurs on pages where there is a anchor location in the url. An example would be if you clicked on a link http://example.com/post/244#comment5
and refreshed the page after looking around you would not be at the anchor and the page jumps around. Is there any way to prevent this with javascript? So that no-matter-what you would always navigate to the anchor.
Disable in the browser: go to chrome://flags/#enable-scroll-anchoring and set "Scroll anchoring" to "Disabled".
If you open the Chrome Dev Console (CTRL-Shift-J), you can run the code from there and confirm that it works. It disables AutoScroll, but still allows the middle mouse button to be used to open links in new tabs and close open tabs. The code stops working if you leave the page you run it on.
Make sure your mouse cable isn't damaged. If you're using a wireless mouse, check or change your batteries. Make sure there's no dirt blocking your scroll wheel. If a controller is plugged in at the same time as your mouse, unplug it.
Disabling scroll with only CSS. There's another way to disable scrolling that is commonly used when opening modals or scrollable floating elements. And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.
On Chrome, even if you force scrollTop to 0 it will jump afterwards after the first scroll event.
You should bind the scroll to this:
$(window).on('beforeunload', function() { $(window).scrollTop(0); });
So the browser is tricked to believe that it was on the beginning before the refresh.
To disable automatic scroll restoration just add this tag to head section.
<script>history.scrollRestoration = "manual"</script>
It's not supported by IE. Browser compatibility.
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