Add a nice back to top button on your web page
Posted By : Rohit Goyal | 26-Sep-2016
Description:-
Having a lot of content on page means a lot of scrolling.If you are a user you don't want scrolling when going to page.This issue can resolved by adding a back to top on page by clicking on that user get automatically scroll to top of a page. All you need to add CSS ,HTML & JS given below:-
<body> <button class="backToTop">Go<br>Top</button> </body>
body{ height:1200px; background:#ddd; } .backToTop { display: none; position: fixed; right: 10px; bottom: 40px; background: rgba(149,2,0,0.7); color: #fff; text-align: center; height: 50px; width: 50px; border-radius: 50%; border: 0px; cursor: pointer; transition: all linear 300ms; } .backToTop:hover { background: rgba(149,2,0,1); }
I don't have content on page.That's why i'm giving 1200px height to body. Initially back to top will not visible due to display:none property and i set position to fixed.
Then adjust the position from right and bottom.After that i give border-radius:50%, that make the button in circular shape and give transition to 300ms.On hover i give slightly dark background to that button.
Step 3:- Inculde JS
<script src="jquery-1.11.3.min.js"></script>
Add the jQuery library file just before the end of body tag.
You can download the latest version of JQuery library file from http://jquery.com/
Now add the code in your script tag just below your jquery library file :-
$(document).ready(function(){ var scroll_pos = 0 $(document).scroll(function() { scroll_pos = $(this).scrollTop(); if(scroll_pos > 150) { $('.backToTop').fadeIn(300); } else { $('.backToTop').fadeOut(300); } }); $('.backToTop').click(function(){ $('html,body').animate({scrollTop: 0}, 500); }); });
When document is ready we will give scroll_pos to 0.After that when scrolling reaches to greater than 150px the back to top become slowly fadeIn. When end user click on this button.This will take user automatically at the top of page and gets fadeOut in 300ms. Using jQuery animate we will set scroll position to zero in 500ms when user click on back to top button.
That's it.
Use this code in your Page and this leads to avoid user to scroll top.When there is so much content on page.
You can see the demo at => https://jsfiddle.net/rohitgoyal9293/xf34zcvy/
Note:- The above code will tested on latest version of firefox and chrome.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rohit Goyal
Rohit is an experienced Frontend Developer, having good experience in HTML5, CSS3, Bootstrap, Jquery, AngularJS, Angular4/5, Restful API Integration, ES6 . His hobbies are playing cards and cricket.