Gemini Tutorials Blog

Free Design, Development, & Management Articles

Home » Scroll to Top Using Vanilla JavaScript: With Complete Source Code
Scroll To Top Vanilla JavaScript

Scroll to Top Using Vanilla JavaScript: With Complete Source Code

Introduction to scroll to top:

In the fast-paced world of web development, where user experience reigns supreme, the quest to enhance navigation and usability remains an ongoing pursuit. Among the myriad techniques at a developer’s disposal, the implementation of a “Scroll to Top” functionality stands out as a simple yet highly effective solution. By seamlessly guiding users back to the top of a webpage with a single click, this feature streamlines the browsing experience and empowers visitors to navigate lengthy content effortlessly. In this article, we embark on a journey to explore the intricacies of integrating this invaluable tool into your website’s arsenal, leveraging the power of Vanilla JavaScript to craft a sophisticated and modern design that not only enhances functionality but also elevates aesthetics.

Navigating through vast amounts of content on a webpage can sometimes prove daunting for users, leading to frustration and disengagement. Recognizing this challenge, web developers continuously seek innovative ways to optimize the browsing experience and ensure seamless navigation. Enter the “Scroll to Top” functionality – a subtle yet impactful addition that addresses this concern head-on. By providing users with a convenient means to swiftly return to the top of a page, this feature promotes fluidity and ease of navigation, fostering a more enjoyable and efficient browsing experience. In the pages that follow, we delve deep into the mechanics of implementing this functionality using Vanilla JavaScript, exploring techniques to seamlessly integrate it into your website’s design while maintaining a sleek and modern aesthetic.

As we embark on this journey, our goal is clear: to equip you with the knowledge and tools necessary to enhance your website’s user experience through the implementation of a “Scroll to Top” functionality. Through the meticulous combination of JavaScript’s versatile capabilities and thoughtful design principles, we aim to empower you to create a browsing experience that not only delights users but also reinforces your website’s credibility and professionalism. Join us as we uncover the intricacies of this invaluable feature, unlocking its potential to revolutionize the way users interact with your website and solidify its position as a beacon of user-centric design in the vast expanse of the digital landscape.

HTML Code for scroll to top:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stylish Scroll To Top Using Vanilla JavaScript</title>
    <link rel="stylesheet" href="css.css">
</head>
<body>
    <!-- Content of Your Website Goes Here -->
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <!-- Scroll to Top Button -->
    <a href="#" id="scrollToTopButton" class="scroll-to-top">
        <img src="up-arrow.svg" alt="Scroll To Top" width="12" height="12">
        <span>Top</span>
    </a>
    <script src="js.js"></script>
</body>
</html>

CSS Code (css.css):

/* Add This To The Body */
body{
    overflow-x: hidden;
}
/* A Properties to Animate Button Gradient On Hover */
@property --myColor1 {
  syntax: '<color>';
  initial-value: rgba(117,245,85,1);
  inherits: false;
}
@property --myColor2 {
  syntax: '<color>';
  initial-value: rgb(208, 243, 84);
  inherits: false;
}
@property --myColor3 {
  syntax: '<color>';
  initial-value: rgba(252,235,73,1);
  inherits: false;
}
/* Style the Button */
.scroll-to-top {
    position: fixed;
    bottom: 20px;
    right: -60px;
    border: 2px solid #2ba17e;
    background: linear-gradient(0deg, var(--myColor1) 0%, var(--myColor2) 35%, var(--myColor3) 100%);
    width: 50px;
    height: 50px;
    border-radius: 10px;
    text-align: center;
    opacity: 0;
    transition: opacity 0.3s ease-out, right 0.3s ease-out, border 1s ease-out, --myColor1 0.5s ease-out, --myColor3 0.5s ease-out;
    z-index: 999;
    text-decoration: none;
}
.scroll-to-top img{
    position: relative;
    transition: top 0.3s ease-out;
    top: 6px;
}
.scroll-to-top span{
    display: block;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    color:#0d0d0d;
    position: relative;
    top: 7px;
    transition: top 0.3s ease-out;
    font-size: 12px;
}
/* Flip Edges Colors Gradient Values */
.scroll-to-top:hover{
    border: 2px solid #2a472e;
    --myColor1: rgba(252,235,73,1);
    --myColor3: rgba(117,245,85,1);;
}
.scroll-to-top:hover span{
    top: 4px;
}
.scroll-to-top:hover img{
    top: 9px;
}
/* Class To Show Hide */
.show-scroll-to-top{
    opacity: 1;
    right: 20px;
}

JavaScript Code (js.js):

// Get the button element
var scrollToTopButton = document.getElementById("scrollToTopButton");
// Add a scroll event listener
window.addEventListener("scroll", function() {
    // If user has scrolled down more than 100px, show the button
    if(document.body.scrollTop >= 100 || document.documentElement.scrollTop >= 100) {
        scrollToTopButton.classList.add("show-scroll-to-top");
    } else {
        scrollToTopButton.classList.remove("show-scroll-to-top");
    }
});
// Add a click event listener to scroll back to the top
scrollToTopButton.addEventListener("click", function(e) {
    e.preventDefault();
    // Scroll to the top smoothly
    window.scrollTo({
        top: 0,
        behavior: "smooth"
    });
});

Conclusion:

Incorporating a “Scroll to Top” functionality using Vanilla JavaScript not only enhances the user experience but also adds a touch of modernity to your website. By implementing a sleek and unobtrusive button that appears when users scroll down, you empower them to navigate effortlessly through lengthy pages, improving accessibility and usability. Moreover, the combination of CSS animations and JavaScript interaction provides a seamless and visually appealing transition, ensuring that your website maintains a polished and professional appearance.


By following the steps outlined in this article, you can easily integrate this essential feature into your website, elevating its overall quality and usability. Embracing simple yet effective solutions like the “Scroll to Top” functionality showcases your commitment to user satisfaction and sets a positive impression for visitors, ultimately contributing to the success and credibility of your online presence. So, don’t hesitate to implement this feature today and enhance the browsing experience for your users.

To learn more about gradients hover visit StackOverflow on this thread

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *