Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Back to Top Button using Javascript

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 215
    Comment on it

    Making a good client experience on your website it is necessary to keep individuals on the page. The most ideal approach to make a decent client experience is to make it simple for users to utilize, if your website is hard to utilize it disturbs users and they will proceed to other websites. In this blog you will figure out how you can make back to top button with jQuery.

    Below is the html code  :-

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    	<meta charset="utf-8">
    	<title>Back to Top</title>
        <!-- Scripts -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
    </head>
    
    <body>
    
    	<div class="container">
    
    			<h1>Sticky Back to Top Button</h1>
    			
    		<section>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    			<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
    		</section>
    		
    		<footer>
    				<p>Just some basic footer text.</p>
    			
    			<a href="#" class="top">Go Top</a>
    		</footer>
    	</div> <!-- end container -->
    </body>

    Below is the css  :-

    
    body {
    	background: #000000;
    	padding: 2em 0;	
    }
    
    
    p {
    	margin-bottom: 22px;
    }
    
    
    h1 {
    	font-size: 30px;
    	margin-bottom: 21px;
    }
    
    .container {
    	     background: white none repeat scroll 0 0;
                 margin: 0 auto;
                 padding: 30px;
                 width: 800px;
    }
    
    .top {
    	position: fixed;
    	bottom: 65px;
    	right: 2em;
    	text-decoration: none;
    	color: white;
    	background-color: rgba(0, 0, 0, 0.3);
    	font-size: 12px;
    	padding: 3px;
    	display: none;
    }
    
    .top:hover {
    	background-color: rgba(0, 0, 0, 0.6);
    }

    Below is the jQuery code :-

    When the user scrolls to the particular element then the scroll event occur. For all scrollable elements scroll event works. In the below code if the position is more than 200 then we want to display the go to top button we do this by using the scrollTop function. fadeIn is used to display the button. When the scrollTop is less than 200 then we don't need to show the button. But when it become greater than 200 then we add a click event to it. On click function we basically scroll page back to top and apply animation using the animate function . We set the scrollTop to 0 so that it will move to the top.

    $(document).ready(function() {
    			// Show or hide the sticky footer button
    			$(window).scroll(function() {
    				if ($(this).scrollTop() > 200) {
    					$('.top').fadeIn(200);
    				} else {
    					$('.top').fadeOut(200);
    				}
    			});
    			
    			// Animate the scroll to top
    			$('.top').click(function(event) {
    				event.preventDefault();
    				
    				$('html, body').animate({scrollTop: 0}, 300);
    			})
    		});

    Here is the working demo:-

    https://jsfiddle.net/osz8xh1r/

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: