Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Create an Infinite Scroll Page using HTML CSS & JavaScript
Next article icon

How to Create Scroll Indicator using HTML CSS and JavaScript ?

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Scroll Indicator is a progress bar that represents how much a page has been scrolled. When we scroll down the bar fills up and when we scroll up the bar amount reduces.

Approach:

Now, we will create a basic webpage with text to enable scrolling and then use JavaScript to make the scroll indicator work.

HTML Code: In this section, we will create a basic structure of the body. html

<!DOCTYPE html> <html lang="en">  <head>     <meta name="viewport" content=         "width=device-width, initial-scale=1.0">     <title>GFG : Scroll Indicator</title> </head>  <body>     <!--The scroll indicator line          at the top of page-->      <div class="line" id="scrollIndicator"></div>      <!--Content to fill the page          to enable scrolling-->     <div class="text">         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>     </div> </body>  </html> 

CSS code: In this section, we will add some CSS property to set the style to create scroll indicator. CSS

<style>     body {         margin: 0;     }      /* Formatting text to      fill the page */     .text {         font-size: 80px;         color: green;         text-align: center;         line-height: 3em;     }      /* The progress bar -      scroll indicator */     .line {         background: green;         height: 8px;         border-radius: 4px;         width: 0%;         position: fixed;         top: 0;     } </style> 
Approach:

window.innerHeight – The height of the viewable portion of the browser.

document.body.scrollHeight – The height of webpage.

window.scrollY – Number of pixels the user has scrolled down so far.

maxHeight – Calculate number of pixels user can scroll.

percentage – The width of scrollIndicator element.

JavaScript Code for Scroll Indicator: javascript

<script>      // Added event listener to the scroll     window.addEventListener('scroll',             moveScrollIndicator);      // Getting scrollIndicator element by ID     const scrollIndicatorElt =          document.getElementById('scrollIndicator');      // Height of entire web page - height     // of viewable portion of browser     const maxHeight =          window.document.body.scrollHeight          - window.innerHeight;      function moveScrollIndicator(e) {          // Calculating width of the          // scrollIndicator element         const percentage =              ((window.scrollY) / maxHeight) * 100;          // Pixels scrolled by the user          // to total scrollable Pixels         scrollIndicatorElt.style.width                  = percentage + '%';     } </script> 
Complete code: html
<!DOCTYPE html> <html>  <head>     <title>GFG : Scroll Indicator</title>     <style>         body {             margin: 0;         }          .text {             font-size: 80px;             color: green;             text-align: center;             line-height: 3em;         }          .line {             background: green;             height: 8px;             border-radius: 4px;             width: 0%;             position: fixed;             top: 0;         }     </style> </head>  <body>     <div class="line" id="scrollIndicator"></div>      <div class="text">         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>         <div>GeeksforGeeks</div>     </div>      <script type="text/javascript">         window.addEventListener('scroll',                 moveScrollIndicator);          const scrollIndicatorElt =             document.getElementById('scrollIndicator');          const maxHeight =             window.document.body.scrollHeight             - window.innerHeight;          function moveScrollIndicator(e) {             const percentage =                  ((window.scrollY) / maxHeight) * 100;              scrollIndicatorElt.style.width                 = percentage + '%';         }     </script> </body>  </html> 
Output:

https://media.geeksforgeeks.org/wp-content/uploads/20240725201747/file.mp4


Next Article
Create an Infinite Scroll Page using HTML CSS & JavaScript
author
tarundhamor
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Projects

Similar Reads

  • Create an Infinite Scroll Page using HTML CSS & JavaScript
    In this article, we will create an infinite scroll page using HTML, CSS, and JavaScript. Infinite scrolling allows you to load and display content as the user scrolls down the page, providing a seamless browsing experience. We'll fetch and append new content dynamically as the user reaches the end o
    2 min read
  • How to Create Stopwatch using HTML CSS and JavaScript ?
    In this article, we will learn How to create a Stopwatch using HTML CSS, and JavaScript. The StopWatch will have the Start, Stop, and Reset functionality. Prerequisites:HTML CSS JavaScriptApproach:Create one container in which all the elements are present.Inside this container add 2 divs that contai
    3 min read
  • How to Create Shrink Header on Scroll using HTML, CSS and JavaScript ?
    The Shrink Navigation bar works when the user scrolls down the page. In this article, we will use HTML, CSS, and JavaScript to design a shrink navigation bar. HTML is used to create the structure, and CSS is used to set the style of the HTML structure to make it looks good. This kind of shrinking na
    3 min read
  • How to create a Snackbar using HTML, CSS & JavaScript?
    Snackbar or toast notifications are brief messages that pop up on a webpage to convey information or feedback to users. They are versatile tools for signaling different states, like success, error, or invalid input, and can appear at various positions on the screen. Approach:First, create a basic HT
    3 min read
  • How to Create Scroll Indicator using ReactJS ?
    Scroll Indicator in React JS refers to a representation of the length of the page visited or present on the screen. It shows the amount the pages that have been scrolled. PrerequisiteNode.js & npm React JSstyled-componentsReact JS useState() hooks.Approach to create Scroll IndicatorTo create a S
    2 min read
  • How to Create a Change Background on Scroll using HTML CSS and JavaScript ?
    In this article, we will try to change the background color of the container once the scroll occurs. If a user scrolls down the page the background color will be changed. We will use the below approach to accomplish this task. Using the scroll event on the window objectIn this approach, we will use
    6 min read
  • How to create a revealing sidebar using HTML CSS and JavaScript?
    A revealing sidebar is a hidden UI element that becomes visible upon user interaction, such as clicking or swiping, providing navigation links. The content of the page will rotate and the navigation bar will reveal itself when the menu button is clicked. ApproachCreate an HTML file with headings and
    3 min read
  • Scrollspy using HTML CSS and JavaScript
    In this article, we will learn about Scrollspy which is a popular feature used in modern web applications. It is used to highlight and allow to navigate through different sections of long web pages as the user scrolls. It increases the interaction between the user and the web application by providin
    5 min read
  • Create a Health Tracker using HTML CSS & JavaScript
    In this article, we will see how we can create a health tracker using HTML, CSS, and JavaScript. Here, we have provided the user with three inputs i.e. input water intake, exercise duration, and blood sugar levels. The user can input these daily and then the provided data is stored in the localStora
    5 min read
  • How to Scroll to an Element Inside a Div using JavaScript?
    To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript: Table of Content Using scrollIntoV
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences