How to Create Scroll Indicator using ReactJS ?
Last Updated :
02 Nov, 2023
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.
Prerequisite
Approach to create Scroll Indicator
To create a Scroll Indicator using ReactJS we will use a scroll event with window.addEventListener to capture the scroll and update the scroll indicator accordingly. Styled components are used to define the styles and CSS for reusable components.
Steps to create React Application
Step 1: You will start a new project using create-react-app command.
npx create-react-app react-scroll-indicator
Step 2: Now go to your react-scroll-indicator folder by typing the given command in the terminal.
cd react-scroll-indicator
Step 3: Install the dependencies required in this project by typing the given command in the terminal:
npm install --save styled-components
Project Structure:Â

Dependencies list after installing packages
{
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^6.1.0",
"web-vitals": "^2.1.4"
}
}
Example: This example shows Scroll Indicator with the help of eventlisterners and styled-components.
JavaScript
// Filename - App.js
import ScrollIndicator from "./components/ScrollIndicator";
function App() {
return <ScrollIndicator />;
}
export default App;
JavaScript
// Filename - components/ScrollIndicator.js
import React, { useState, Fragment } from "react";
import {
Container,
ProgressBar,
ScrollContent,
Heading,
Heading2,
} from "./Styles";
const ScrollIndicator = () => {
const [scroll, setScroll] = useState(0);
const onScroll = () => {
const Scrolled = document.documentElement.scrollTop;
const MaxHeight =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
const ScrollPercent = (Scrolled / MaxHeight) * 100;
setScroll(ScrollPercent);
};
window.addEventListener("scroll", onScroll);
return (
<Fragment>
<Container>
<ProgressBar
style={{ width: `${scroll}%` }}
></ProgressBar>
</Container>
<ScrollContent>
<Heading>GeeksforGeeks</Heading>
<Heading2>
React Example for Scroll Indicator
</Heading2>
</ScrollContent>
</Fragment>
);
};
export default ScrollIndicator;
JavaScript
// Filename - components/Styles.js
import styled from "styled-components";
export const Container = styled.div`
background-color: black;
height: 10px;
position: sticky;
top: 0;
left: 0;
z-index: 1;
width: 100%;
border-radius: 10px;
`;
export const ProgressBar = styled.div`
height: 10px;
background-color: green;
border-radius: 10px;
`;
export const ScrollContent = styled.div`
overflowy: scroll;
height: 2000px;
`;
export const Heading = styled.h1`
text-align: center;
color: green;
font-size: 3rem;
`;
export const Heading2 = styled.h1`
text-align: center;
font-size: 2rem;
`;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output

Similar Reads
How to Create Scroll Indicator using HTML CSS and JavaScript ? 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 wo
3 min read
Create a scroll status in ReactJS using Material-UI Progress bars at the top, showing scroll status are very common nowadays in webpages. Also, ReactJS and Material-UI go pretty well along with each other, React-JS being the most popular JavaScript framework for building UI components and Material-UI a library that provides various useful and reusabl
3 min read
How to create Loading Screen in ReactJS ? Loading screen plays a major role in enhancing UX development. In this article, we are going to learn how we can create a Loading Screen in ReactJs. A loading screen is a picture shown by a computer program, often a video game, while the program is loading or initializing.PrerequisitesJavaScript and
2 min read
How to create a simple counter Using ReactJS? React counter app is a program that allows users to interact with a numerical counter value. It demonstrates basic state management and user interaction within a user interface.Prerequisites:NPM & Node.js React JSReact useState hookApproach:Building a simple counter application is a great way to
2 min read
How to use Slide Component in ReactJS ? Slide Component adds a Slide animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Slide Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReactJSMaterial UISteps to Create the
3 min read
How to use SnackBar Component in ReactJS ? ReactJS provides developers with a wide range of components to create interactive and dynamic web applications. One such component is the SnackBar, which is commonly used to display temporary messages or notifications to users. Creating a SnackBar component allows for the presentation of these messa
2 min read
How to Create a Scroll To Top Button in React JS ? You will see there are lots of websites, that are using a useful feature like if you're scrolling the webpage, and now you are at the bottom of that page then you can use this button to scroll up automatically like skip to the content. The following example covers create a Scroll To Top button in Re
3 min read
How to use List Component in ReactJS? Lists are continuous, vertical indexes of text or images. Material UI for React has this component available for us, and it is very easy to integrate. We can use the List Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReactJSSteps to Create the React Application And In
2 min read
How to use Pagination Component in ReactJS ? Pagination is a feature through which users can easily switch between pages across a website or app. With the help of this component, a user can select a specific page from a range of pages. Material UI for React has this component available for us, and it is very easy to integrate. We can use the f
2 min read
How to create a Scroll To Bottom button in React JS ? Learn to implement a "Scroll to Bottom" button in React using the useState() hook, a feature commonly seen in chat applications like WhatsApp and Telegram for effortless navigation to the end of a chat window. Prerequisites:NodeJS or NPMReact JSstyled-componentReact useState()Steps to Create the Rea
2 min read