Scroll to Top Using window.scrollY Property Last Updated : 18 Apr, 2025 Comments Improve Suggest changes Like Article Like Report A Scroll to top button is used to Scroll to the top of the page whenever we are at the end or middle of the page and we want to reach the top of the page directly. In this article, we are going to make a Scroll To Top Button.Before starting we need Font Awesome icon for the Up arrow. To get this we will use the font awesome CDN link. The link is given below you can copy and paste it into the head tag inside the HTML file.CDN Link:https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css Functionalities we want:Initially, the scroll button should hide.Whenever we scroll the current page, in the bottom right corner a button should appear.After clicking on the button, the page automatically scrolls to the top of the page.Here is the preview of the feature we are going to make.Preview ImageProject Structure- index.html- style.css- script.jsExample: Add all the below code to the respective files you will see the output in the browser.HTML Code: We have used a container div and inside that we have used random text and used the ArrowUp icon from the fontawesome CDN link.CSS Code: Used normal styling to style the text and attached the ArrowUp icon to the bottom right.JavaScript Code: Initially, the ArrowUp will hide and whenever the user scrolls the screen the icon will be visible, and the scrollToTop() function will trigger whenever the icon will be clicked. HTML <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> <!-- CDN LINK --> <link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <title>Document</title> </head> <body> <div class="container"> <button id="scrollToTopBtn" onclick="scrollToTop()"> <i class="fa-solid fa-arrow-up fa-beat fa-lg" style="color: #ffffff;"> </i> </button> <div class="content-div"> <div class="heading"> Scroll Down to see the effect </div> <div class="description"> GeeksforGeeks is the best Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles. GeeksforGeeks offers Free Tutorials, Millions of Articles, Live, Online and Classroom Courses,Frequent Coding Competitions, Webinars by Industry Experts, Internship opportunities and Job Opportunities. </div> </div> </div> <script src="script.js"></script> </body> </html> CSS * { margin: 0; padding: 0; box-sizing: border-box; } body { background-color: rgb(239, 238, 238); } .heading { background-color: rgb(51, 88, 38); color: white; padding: 30px; font-size: 2rem; font-weight: 900; text-transform: uppercase; } #scrollToTopBtn { position: fixed; width: 2.5rem; height: 2.5rem; bottom: 2rem; right: 2rem; z-index: 10; font-size: 1rem; background-color: rgb(255, 0, 0); border: none; cursor: pointer; border-radius: 50%; opacity: 0.8; transition: opacity 0.3s, transform 0.3s; } #scrollToTopBtn:hover { opacity: 1; transform: scale(1.1); } .description { height: 2500px; margin: 2rem; } JavaScript window.onscroll = function () { scrollFunction(); }; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { document.getElementById("scrollToTopBtn").style.display = "block"; } else { document.getElementById("scrollToTopBtn").style.display = "none"; } } function scrollToTop() { const scrollDuration = 500; // Duration in milliseconds const scrollStep = -window.scrollY / (scrollDuration / 15); const scrollInterval = setInterval(function () { if (window.scrollY !== 0) { window.scrollBy(0, scrollStep); } else { clearInterval(scrollInterval); } }, 15); } Output: Comment More info R rahulkwh Follow Improve Article Tags : JavaScript Web Technologies Explore JavaScript BasicsIntroduction to JavaScript4 min readJavaScript Versions2 min readHow to Add JavaScript in HTML Document?3 min readJavaScript Syntax6 min readJavaScript Output4 min readJavaScript Comments2 min readVariables & DatatypesVariables and Datatypes in JavaScript6 min readGlobal and Local variables in JavaScript4 min readJavaScript Let6 min readJavaScript const5 min readJavaScript Var Statement7 min readOperatorsJavaScript Operators5 min readOperator precedence in JavaScript2 min readJavaScript Arithmetic Operators5 min readJavaScript Assignment Operators5 min readJavaScript Comparison Operators5 min readJavaScript Logical Operators5 min readJavaScript Bitwise Operators5 min readJavaScript Ternary Operator4 min readJavaScript Comma Operator2 min readJavaScript Unary Operators4 min readJavaScript in and instanceof operators3 min readJavaScript String Operators3 min readStatementsJavaScript Statements4 min readJavaScript if-else3 min readJavaScript switch Statement4 min readJavaScript Break Statement2 min readJavaScript Continue Statement1 min readJavaScript Return Statement4 min readLoopsJavaScript Loops3 min readJavaScript For Loop4 min readJavaScript While Loop3 min readJavaScript For In Loop3 min readJavaScript for...of Loop3 min readJavaScript do...while Loop4 min readPerformance & DebuggingJavaScript | Performance4 min readDebugging in JavaScript4 min readJavaScript Errors Throw and Try to Catch2 min readObjectsObjects in Javascript4 min readObject Oriented Programming in JavaScript3 min readJavaScript Objects6 min readCreating objects in JavaScript5 min readJavaScript JSON Objects3 min readJavaScript Object Reference4 min readFunctionsFunctions in JavaScript5 min readHow to write a function in JavaScript ?4 min readJavaScript Function Call2 min readDifferent ways of writing functions in JavaScript3 min readDifference between Methods and Functions in JavaScript3 min readExplain the Different Function States in JavaScript3 min readJavaScript Function Complete Reference3 min readArraysJavaScript Arrays7 min readJavaScript Array Methods7 min readBest-Known JavaScript Array Methods6 min readImportant Array Methods of JavaScript7 min readJavaScript Array Reference4 min readStringJavaScript Strings6 min readJavaScript String Methods9 min readJavaScript String Reference4 min read Like