top of page

Just Jolly Functions

Functions: Adding a Back-to-Top Button with HTML, CSS, and JavaScript

The "Back to Top" button is a user interface element that allows visitors to quickly navigate to the top of a webpage with a single click. It is typically displayed as a small, circular button with an upward-facing arrow (↑) or a custom icon, and it appears when the user scrolls down a certain distance on the page. This button provides a convenient way to improve user experience by reducing the need for excessive scrolling, especially on long pages.



Back-to-Top Button Code:

<button onclick="topFunction()" id="back-to-top" title="Go to top">↑</button>

<script>

  // Get the button
  var mybutton = document.getElementById("back-to-top");


  // When the user scrolls down 20px from the top of the document, show the button
  window.onscroll = function() {scrollFunction()};

  function scrollFunction() {
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
      mybutton.style.display = "block";
    } else {
      mybutton.style.display = "none";
    }
  }


  // When the user clicks on the button, scroll to the top of the document

  function topFunction() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
  }

</script>

Additional Customizations:

To customize the "back-to-top" button in your code, you can modify the styles, button text, or functionality. Below are several customizations you can implement:


1. Customizing Button Appearance

You can style the button using CSS to change its appearance, such as its size, background color, text style, and positioning.

<button onclick="topFunction()" id="back-to-top" title="Go to top">↑</button>

<style>
  /* Style the button */
  #back-to-top {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 100;
    font-size: 24px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    text-align: center;
    cursor: pointer;
    display: none;
    box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
  }

  /* Hover effect */
  #back-to-top:hover {
    background-color: #0056b3;
    transform: scale(1.1);
  }

  /* Active effect */
  #back-to-top:active {
    background-color: #004085;
  }
</style>

2. Change Button Text or Icon

You can change the "↑" icon to an image or use a different text.

Example with text:

<button onclick="topFunction()" id="back-to-top" title="Go to top">Back to Top</button>

Example with an image:

<button onclick="topFunction()" id="back-to-top" title="Go to top">
  <img src="path_to_image/icon.png" alt="Go to top" width="30" height="30">
</button>

3. Customize Scroll Position Trigger

If you want the button to appear after scrolling down a different amount, you can adjust the value in scrollFunction().


For example, show the button when scrolling down 100px:

if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
  mybutton.style.display = "block";
} else {
  mybutton.style.display = "none";
}

4. Smooth Scroll Animation

If you'd like the page to scroll smoothly when the button is clicked, you can use scrollIntoView() with smooth scrolling:

function topFunction() {
  document.documentElement.scrollIntoView({behavior: 'smooth'});
}

Alternatively, you can use the window.scroll method with smooth behavior:

function topFunction() {
  window.scroll({
    top: 0,
    left: 0,
    behavior: 'smooth'
  });
}

5. Customizing Button Visibility and Animation

You can add custom animations to the button's appearance and disappearance by changing the scrollFunction() logic.


For example, fade in and out the button:

/* Add CSS for fade-in/fade-out */
#back-to-top {
  opacity: 0;
  transition: opacity 0.5s ease-in-out;
}

#back-to-top.show {
  opacity: 1;
}

Then, in your scrollFunction() function, toggle the show class:

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.classList.add("show");
  } else {
    mybutton.classList.remove("show");
  }
}

6. Change Button Behavior on Mobile Devices

If you want the button to appear only on larger screens or to be styled differently on mobile, you can use media queries:

@media only screen and (max-width: 600px) {
  #back-to-top {
    background-color: #28a745; /* Different background for mobile */
    font-size: 20px;
  }
}

7. Add a Custom Animation on Button Click

You can add a custom animation that occurs when the button is clicked. For example, to simulate the button "bouncing" when clicked:

function topFunction() {
  mybutton.style.animation = 'bounce 1s ease-in-out';
  document.body.scrollTop = 0;
  document.documentElement.scrollTop = 0;

  // Remove the bounce animation after it completes
  setTimeout(function() {
    mybutton.style.animation = '';
  }, 1000);
}

// CSS for the bounce animation
@keyframes bounce {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

8. Customize Button Text on Scroll

You can change the text of the button when the user scrolls down (e.g., show "↑" when at the top and "Top" when scrolled down).

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.innerHTML = "Top";  // Change text when scrolled
    mybutton.style.display = "block";
  } else {
    mybutton.innerHTML = "↑";  // Change back to the original text
    mybutton.style.display = "none";
  }
}

9. Add a Tooltip

You can change the tooltip text when hovering over the button.

<button onclick="topFunction()" id="back-to-top" title="Scroll back to top!">↑</button>

To change the tooltip dynamically:

mybutton.title = "Scroll back to top quickly!";

10. Custom Button Size and Spacing

You can adjust the size, border-radius, and spacing to make the button stand out more.

#back-to-top {
  width: 60px;
  height: 60px;
  font-size: 28px;
  border-radius: 10px;  /* Rounded corners */
  padding: 10px;
  background-color: #f39c12;
}

These are just a few customizations you can make to improve the functionality and appearance of your "back-to-top" button. Depending on your post's design, you can mix and match these to create the ideal experience!

bottom of page