page scroll indicator research

I was wondering the other day how they do those page scroll indicators, i saw one when viewing this site:

https://blog.wolfram.com/2021/03/24/the-solution-of-the-zodiac-killers-340-character-cipher/

I still have to finish reading that article as i got distracted by the scroll indicator...

Anyway this is one method of adding a scroll indicator, which is that  bar on the top that expands as you are reading, you also need a fixed element to show this indicator, otherwise there is no point showing it. So you can just fix it to the top of your page, or if you have a fixed nav you could add it under there. 

I guess its more of a gimmic, but I think it looks cool.

 

HTML

<div class="progress-fixed">
  <div class="progress-bar-container">
    <div class="progress-bar" id="myBar"></div>
  </div>
</div>

CSS

.progress-fixed {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  background-color: #f1f1f1;
}

.progress-bar-container {
  width: 100%;
  height: 5px;
  background: #ccc;
}

.progress-bar {
  height: 5px;
  background: #f04b4b;
  width: 0%;
}

Javascript

window.onscroll = function() {myFunction()};

function myFunction() {
  var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
  var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
  var scrolled = (winScroll / height) * 100;
  document.getElementById("myBar").style.width = scrolled + "%";
}

Other Items in javascript

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 191 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹

Random Quote
You want to be the best, you MUST put the long yards in! Nothing comes easy in life so stop wishing and start DOING! So many people would rather bitch and moan than help themselves. Dont be one of those negative drainers, start today, make a small change and keep going forwards with this attitude!
Unknown