I want to put to the top bottom of the page which should scroll the page to the top. However I want the scrolling process to have animated effect. I think it is possible by using animate but have no idea how to ?
Please help. Thanks
In jQuery, the scrollTo() method is used to set or return the vertical scrollbar position for a selected element. This behavior can be used to scroll to the top of the page by applying this method on the window property. Setting the position parameter to 0 scrolls the page to the top.
You might need to trigger scrolling within JavaScript, in which case: window. scrollTo(0, 0); …is a sure bet to scroll the window (or any other element) back to the top.
This code must be added just after we included jQuery, in new <script></script> tags: $(function () { $(window). scroll(function() { if ($(this). scrollTop() - 200 > 0) { $('#to-top').
The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.
The way is pretty simple. Put a button at the bottom of the page and write an event something like this
$('#spnTop').on("click",function() {
$('html, body').animate({ scrollTop: 0 }, 'slow', function () {
alert("reached top");
});
});
Here is the fiddle for this
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