Creating a Simple Scroll-To-Top Animation ("Back to Top" Button)
By using the animate
and scrollTop
jQuery functions you don't need a plugin .
// Back to top
$('a.top').click(function () {
$(document.body).animate({scrollTop: 0}, 800);
return false;
});
<!-- Create an anchor tag -->
<a class="top" href="#">Back to top</a>
Changing the scrollTop
value changes where you want the scrollbar to land. All you're doing is animating the body of the document throughout the course of 800ms until it scrolls all the way to the top of the document.
Written by Matt Smith
Related protips
1 Response
This jQuery worked for me:
$( document ).ready(function() {
$('a.returnTopAction').click(function() {
$('html, body').animate({scrollTop: '0'}, 700);
});
});
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scrolling
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#