Creating a Scroll Up Button using HTML, CSS, and JavaScript Tutorial

In this tutorial, you will learn to create a simple scroll-to-top button using HTML, CSS, and JavaScript. The main purpose of this tutorial is to provide the students and beginners with snippets that are reusable that might be useful for their projects. A working sample source code that demonstrates the interface and functionality of a scroll-to-top button is provided in this article.

What is the use of the Scroll-to-Top Button?

Scroll-to-top button is one of the common features or components in a web application, especially for sites that display long content on a single page. This feature allows your site user to scroll to the top of the site page in just one click.

How to create a scroll-to-top button?

Using JavaScript, we can simply trigger the client page window or element to scroll to the top. The following snippets are the sample script for triggering the page to scroll to the top.

Using Pure JavaScript

  1. document.getElementById('scroll-top-btn').addEventListener("click", function(){
  2. document.getElementsByTagName('html')[0].scrollTop = 0
  3. })

Using jQuery

  1. $('#scroll-top-btn').click(function(){
  2. $('html').scrollTop(0)
  3. // or
  4. $('html').animate({scrollTop:0})
  5. })

Creating a Reusable Scroll-to-top Button

Here are the scripts of the pre-made and reusable scroll-to-button with a simple design. The button will only show if the page is currently not on the top of the page interface. The button automatically appears at the button-right side of the page screen after or while the user is scrolling.

CSS

  1. /** Scroll to Top Button */
  2. div#sc-custom-floating-scroll-top-btn {
  3. position: fixed;
  4. z-index: 10;
  5. background: #147db8;
  6. width: 60px;
  7. height: 60px;
  8. bottom: 30px;
  9. right: 15px;
  10. cursor: pointer;
  11. box-shadow: 0px 2px 4px #7e7e7e;
  12. animation: jump .5s infinite;
  13. }
  14. div#sc-custom-floating-scroll-top-btn:hover,
  15. div#sc-custom-floating-scroll-top-btn:active{
  16. animation: unset;
  17.  
  18. }
  19. @keyframes jump{
  20. 0%{
  21. bottom:30px;
  22. }
  23. 100%{
  24. bottom:35px;
  25. }
  26. }
  27. div#sc-custom-floating-scroll-top-btn>span {
  28. position: relative;
  29. display: flex;
  30. width: 100%;
  31. height: 100%;
  32. justify-content: center;
  33. align-items: center;
  34. }
  35. div#sc-custom-floating-scroll-top-btn>span:before {
  36. content: "";
  37. position: absolute;
  38. height: 30px;
  39. width: 9px;
  40. background: white;
  41. }
  42. div#sc-custom-floating-scroll-top-btn>span:after {
  43. content: "";
  44. position: absolute;
  45. background: #fff;
  46. border-left: 15px solid #147db8;
  47. border-right: 15px solid #147db8;
  48. border-bottom: 15px solid #fff;
  49. top: 7px;
  50. }

The script above will result in something like the following image:

scroll-to-top button

JavaScript

  1. const _scCustomSTbtn = document.createElement('div')
  2. _scCustomSTbtn.setAttribute('id', 'sc-custom-floating-scroll-top-btn')
  3. _scCustomSTbtn.innerHTML = `<span></span>`
  4. if(document.getElementsByTagName('html')[0].scrollTop == 0)
  5. _scCustomSTbtn.style.opacity = 0;
  6. document.body.prepend(_scCustomSTbtn)
  7.  
  8. window.onscroll = function(){
  9. if(document.getElementsByTagName('html')[0].scrollTop <= 0)
  10. _scCustomSTbtn.style.opacity = 0;
  11. else
  12. _scCustomSTbtn.style.opacity = 1;
  13.  
  14. }
  15.  
  16.  
  17. _scCustomSTbtn.addEventListener("click", function(){
  18. document.getElementsByTagName('html')[0].scrollTop = 0
  19. })
  20.  
  21. /** Using jQuery */
  22. // $('#sc-custom-floating-scroll-top-btn').click(function(){
  23. // $('html').scrollTop(0)
  24. // // or
  25. // // $('html').animate({scrollTop:0})
  26. // })

Here's a snapshot of a simple web page interface of how the scroll-to-top button looks in an actual web application page interface.

scroll-to-top button

There you go! You may also want to download the complete source code that I created for this tutorial. I have provided the source code zip file on this website and it can be downloaded for free. The download button is located below this tutorial's content.

That's it! I hope this Creating a Scroll Up Button using HTML, CSS, and JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment