
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to create a scroll indicator with CSS and JavaScript?
To create a scroll indicator with CSS and JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { font-size: 28px; margin:0px; padding:0px; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } .header { position: fixed; top: 0; margin-bottom: 100px; z-index: 1; width: 100%; background-color: #ebfffd; } .progressContainer { width: 100%; height: 20px; background: #ccc; } .progressBar { height: 20px; background: #724caf; width: 0%; } .content { padding: 100px 0; margin: 50px auto 0 auto; width: 80%; } </style> </head> <body> <div class="header"> <h1>Scroll indicator example</h1> <div class="progressContainer"> <div class="progressBar"></div> </div> </div> <div class="content"> <h1>Some headers</h1> <h1>Some headers</h1> <h1>Some headers</h1> <h1>Some headers</h1> <h1>Some headers</h1> </div> <script> window.onscroll = function() {showProgress()}; function showProgress() { var scrollCalculate = document.body.scrollTop || document.documentElement.scrollTop; var height = document.documentElement.scrollHeight - document.documentElement.clientHeight; var scrolled = (scrollCalculate / height) * 100; document.querySelector(".progressBar").style.width = scrolled + "%"; } </script> </body> </html>
Output
The above code will produce the following output −
On scrolling down the page the progress bar will increase as shown −
- Related Articles
- How to create a fixed/sticky header on scroll with CSS and JavaScript?
- How to shrink a header on scroll with CSS and JavaScript?
- How to create a "scroll back to top" button with CSS?
- How to resize a navigation bar on scroll with CSS and JavaScript?
- How to create a gradient background color on scroll with CSS?
- How to hide a navigation menu on scroll down with CSS and JavaScript?
- How to Create an On Scroll Fixed Navigation Bar with CSS?
- How to create tabs with CSS and JavaScript?
- How to create popups with CSS and JavaScript?
- How to create a responsive slideshow with CSS and JavaScript?
- How to create a Modal Box with CSS and JavaScript?
- How to create a collapsible section with CSS and JavaScript?
- How to create a snackbar / toast with CSS and JavaScript?
- How to create a tree view with CSS and JavaScript?
- How to create a quotes slideshow with CSS and JavaScript?

Advertisements