Back to Top Smooth scroll using jQuery
You might have noticed "Back To Top" button on many webpages, it allows users to return back to the top section of the page instantly without having to use mouse scroll wheel. It's tiny but sometimes important feature to have on your website. Place <a name="top" id="top"></a> on top of your page, but below <body> tag, and place a link <a href="#top" id="goTop">Back to top</a> at the bottom of your page but before </body> tag as shown in code below:HTML
- 1
- 2
- 3
- 4
- 5
<body>
<a name="top" id="top"></a>
<!--- your page content goes here -->
<a href="#top" id="goTop">Back to top</a>
</body>
JQUERY
- 1
- 2
- 3
- 4
- 5
<script type='text/javascript'>
$('#goTop').on('click', function(e){
$("html, body").animate({scrollTop: $("#top").offset().top}, 500);
});
</script>