Is there a simple way to force the browser to scroll to the top if a button is clicked?
I have tried
jQuery('html').scrollTop();
jQuery('body').scrollTop();
jQuery(window).scrollTop();
None of them seem to scroll to the top of the page.
Scroll one page at a time in all major browsers including Microsoft Internet Explorer and Mozilla Firefox by pressing the Spacebar key. Move back up the page by pressing Shift + Spacebar or the Home key on the keyboard.
window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.
The Google Chrome extension Scroll to top button adds a button for users who prefer to use their mouse while navigating in the browser. A button is placed in the upper right corner of the web browser when the user scrolls down on a page. Clicking on that button in the browser scrolls the page back to the top.
window. scroll(0, 0) will work just fine in all browsers.
Due to cross browser oddness, some browsers will respond to 'html'
and some to 'body'
. And maybe its just my luck, but .scrollTop(0)
has never worked for me to move the page. Give this a shot:
jQuery('html,body').animate({scrollTop:0},0);
This version is tested and cross browser for all desktop browsers, and mobile devices.
Simple pure javascript, also works in mobile
window.scrollTo(0,0);
You can use either this:
jQuery('body').scrollTop(0);
or this:
jQuery(window).scrollTop(0);
or finally this:
jQuery('html').scrollTop(0);
So, in order to call the scrollTop
method and make your page scrolling you should pass an argument with the numeric value representing the scrollTop position. Otherwise it will work as if you need to get the scrollTop position.
Two last methods should work constantly in all browsers, while the first might not work in some versions of IE.
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