jquery scroll to top


Jquery scroll to top animation is a beautiful light weight jquery effect that is effectively used to navigate to the to of the webpage when a user scrolls down to a considerable length.

The first thing we'll need in this case is an object that will be used as the navigation handle that might be an image or an icon. Let's add a fa icon for this example.

<a href="#" class="to-top"><i class="fa fa-chevron-up"></i></a>

Let's add the basic styling for this icon then.

<style>
.to-top { 	
	position: fixed; 
	bottom: 20px; 
	right: 20px; 
	text-decoration: none; 
	color: #fff; 
	background: #151f28; 
	padding: 10px 20px; 
	display: none; 
}
.to-top:hover{ 
	color: #151f28; 
	background: #fff; 
}
</style>	

We'll keep this icon fixed at the bottom of the window leaving 20px margin from the bottom and the right. Since we wrapped it with the HTMl <a> element, an underline will appear below the link text. To remove that, we added text-decoration: none;. Then defined the color of the icon as well as background color and padding. Since we won't need to display it while the user is at the top of the page, we'll hide it with display: none; attribute. We'll use jquery to display it once the user scrolls down to a defined height of the page.

We also defined some changes when the user hovers the mouse over it to make it more noticeable.

Now, we'll need to add the jquery for the effect to be functional and for that we'll require to add jquery.js file to our page. Add that file and simply paste the script given below.

<script>            
	jQuery(document).ready(function() {
		var duration = 500;
		jQuery(window).scroll(function() {
			if (jQuery(this).scrollTop() > 200) {
				jQuery('.to-top').fadeIn(duration);
			} else {
				jQuery('.to-top').fadeOut(duration);
			}
		});

		jQuery('.to-top').click(function(event) {
			event.preventDefault();
			jQuery('html').animate({scrollTop: 0}, duration);
			return false;
		})
	});
</script>	

Here, jQuery(document).ready(function() {}); prevents the script from being read by browser before the page is completely loaded. We created a variable named duration here for our convenience as it's being used multiple times in the script and it's be easier to change its value at one point rather than all those lines.

jQuery(window).scroll(function() {}); is triggered when a user scrolls the browser window. If the scroll amount exceeds 200px then the to-top class will fade in and the fadein animation duration would be the one we defined in varaible duration earlier else to-top class weill fade out using the same animation duration.

Well, half of the task is completed then. The icon will now be hidden until the user scrolls more that 200px. It will be hidden in case the user scrolls back to the top when the scroll amount becomes less than 200px.

Now, lets go for the scroll to top animation.

jQuery('.to-top').click(function(event) {}) will be triggered once the user clicks on the to-top class.

event.preventDefault(); will prevent the url link defined in the HTML <a> element.

Finally, jQuery('html').animate({scrollTop: 0}, duration); will be the key to get the animation to get scrolled to the very top of the HTML page applying animation duration as defined in variable duration. return false will prevent further execution of the script unless the event is triggered again.