Hello readers, Today in this blog I will show you how to create a custom page scroll indicator using Javascript. Earlier, you have learned to Create a word and character counter.


Scroll Indicator is such a bar or line that shows the scroll range based on how much you scrolled the page. When you start scrolling page the bar start to fill up from left to right. Generally, scroll indicator is placed at the top of the page.


After going through this blog, you can easily create a custom scroll indicator using Javascript. I have tried to explain it in very simple language and easy way so that you won’t have to face any problem. As you can see in the above image, the whole background is black and there is some dummy content. At first I have created a div having ‘indicator’ ID and give the width of 0% height of 10px and gradient background. Width is given 0% because later we will calculate the height of page and scrolled amount then, according to it we will increase the width.

 

After applying CSS, let’s move towards the Javascript. I am using internal Javascript for now but you can use it as external too. At first, I have added a event listener that observe the scroll activity of whole window and call the function named “scroll”. Then I have created “scroll” function that will read the total height of page and total scrolled height. After that it will covert the total scrolled amount into percentage and store it in a variable named “scrolled”. At last I have assigned the scrolled percentage to the width of “indicator”.

 

 document.getElementById("indicator").style.width = scrolled + "%";

 

If you’re feeling difficult to understand what I am saying. You can watch full video tutorial on this program (Scroll Indicator in Javascript).


 You Might Also Like

All this design is based on CSS only and indicator is based on JavaScript. If you’re a beginner you can also create this type of scroll indicator. If you like this program (Scroll Indicator in Javascript) and want to get source code. You can easily get the source code of this program. To get the source codes you just need to scroll down.

Scroll Indicator in Javascript [Source Codes]

 

To create this program (Scroll Indicator in Javascript). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste the following code in your file. First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Javascript code is also inside the HTML file. Remember, you’ve to create a file with .html extension.

HTML

Second, create a CSS file with the name of style.css and paste the given code in your CSS file. Remember, you’ve to create a file with .css extension.

CSS

That’s all, now you’ve successfully created a scroll indicator using JavaScript. If your code does not work or you’ve faced any errors/problems then please comment down your queries. If you feel this article had helped you, please share it with your friends. Subscribe to our Website by pressing bell icon at the right bottom. We will send you notification regarding our new posts and other tips and tricks. You can unsubscribe it anytime you like. If you have any suggestions for me please mention it in the comment below.

Thank You :)

Post a Comment

Previous Post Next Post