How to Implement Back To Top Feature in HTML, ASP.NET and PHP

In many websites having long contents we have seen a "Back To Top" or "Scroll To Top" button when a click takes you to the top of the webpage. We see this kind of feature normally on the product listing pages of e-Commerce websites. Today in this article we will implement the same feature in one of the easiest possible ways. To integrate this feature, we need a little HTML, some CSS for styling and a couple of lines of jQuery code. Using the following method, you can integrate the “Back To Top” feature on your website irrespective of the programming language of your website, be it ASP.NET or PHP.

To start with, we will create a HTML page with the following content in the <body> section.

  1. <body>  
  2.     <h1>  
  3.         Back To Top Demo by Nitesh Luharuka</h1>  
  4.     <div style="height: 1000px">  
  5.     </div>  
  6.     <span id="back-to-top">  
  7.         <img src='images/up-arrow.png' /></span>  
  8. </body> 

If you notice in the preceding, we have added a div with some height to test our feature. At the end, we have added a “<span>” tag having an image of an upward arrow. This arrow will be shown once the user starts scrolling down. To position the image, we will use a bit of CSS. Here's the CSS for the button. You need to add this in the head section of your page.

  1. #back-to-top    
  2. {    
  3.     positionfixed;    
  4.     bottom: 30px;    
  5.     top: 350px;    
  6. }    
  7. #back-to-top img    
  8. {    
  9.     cursorpointer;    
  10. } 

Now, our HTML is ready and for adding the code to the image, we will write the following code in our <head> section of the page. If you've already included jQuery, then you do not need to include the jQuery file again in your project.

  1. <script type="text/javascript">  
  2.     $(document).ready(function () {  
  3.         //Hide Back to top button  
  4.         $("#back-to-top").hide();  
  5.         $(window).scroll(function () {  
  6.             if ($(window).scrollTop() > 200) {  
  7.                 $('#back-to-top').fadeIn();  
  8.             } else {  
  9.                 $('#back-to-top').fadeOut();  
  10.             }  
  11.         });  
  12.         $('#back-to-top img').click(function () {  
  13.             $('body').animate({  
  14.                 scrollTop: 0  
  15.             }, 1000);  
  16.         });  
  17.     });  
  18. </script> 

In the code above, we have done a few things. Let me explain them one by one:

  • First, we hide the image when the document loads.
  • In the scroll() event handler, we have ensured that, as the user starts scrolling and has scrolled 200px (you can change this), the “Back To Top” image becomes visible to the user.
  • In the click() event handler of the button, we send the user back to the top of the page using a nice animation effect of jQuery.

I hope you like this article. Keep learning and sharing!


Similar Articles
Rebin Infotech
Think. Innovate. Grow.