Page Scroll To Top Using jQuery
Last Updated : Jul 1, 2023
In this tutorial we will show you how to scroll the page to the top using jQuery with animation effects whenever user clicks on scroll to top button the page scrolls to its top position with nice and simple jQuery animation.
You may also like Create Your Own Custom Scrollbars Using CSS And HTML
CHECK OUT THIS TUTORIAL LIVE DEMO →
To Scroll Page To Top Using jQuery It Takes Only Three Steps:-
- Make a HTML file and define markup
- Make a js file and define scripting
- Make a CSS file and define styling
Step 1. Make a HTML file and define markup
We make a HTML file and save it with a name scroll.html
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="scroll_script.js"></script> <link rel="stylesheet" type="text/css" href="scroll_style.css"> </head> <body> <div id="wrapper"> <p> This is some sample content </p> <br> <p> This is some sample content </p> <br> <input type="button" value="Scroll Top" id="scrollToTop" onclick="scrolltop();"> </div> </body> </html>
In this step we create some sample content and create a button which is used to scroll the page whenever user clicks on that.
You may also like auto scroll page using jQuery.
Step 2. Make a js file and define scripting
We make a js file and save it with a name scroll_script.js
$(document).ready(function(){ $(window).scroll(function(){ if ($(window).scrollTop() > 100) { $('#scrollToTop').fadeIn(); } else { $('#scrollToTop').fadeOut(); } }); }); function scrolltop() { $('html, body').animate({scrollTop : 0},500); }
In this step we use document.ready function to show and hide the button with jQuery fade
effect when the difference between scrollbar and window is greater than 100 and then we create scrolltop function which is used to scroll the page to top in this function we use jQuery animate function to scroll.
You may also like Load Results From Database On Page Scroll Using jQuery,Ajax And PHP
Step 3. Make a CSS file and define styling
We make a CSS file and save it with a name scroll_style.css
body { background-color:#F2F2F2; font-family:helvetica; } #wrapper { text-align:center; margin:0px auto; padding:0px; width:700px; } #scrollToTop { position:fixed; left:45%; bottom:50px; border:3px solid #585858; background-color:white; color:#585858; border-radius:100%; height:90px; width:90px; font-size:15px; display:none; }
Thats all, this is how to scroll page to top using jQuery.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on page scroll to top jquery helps you and the steps and method mentioned above are easy to follow and implement.