Scroll Indicator with JavaScript

Scroll Indicator with JavaScript
Code Snippet:Scrolling Bar Indicator
Author: mohdhazwan
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 2,124
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a scroll indicator. It renders both horizontal and vertical scrollbars to indicate scrolling position. Besides this, it displays a parallax scrolling effect for the main content area.

How to Create Scroll Indicator with JavaScript

1. First of all, create the HTML structure as follows:

<section class="scroll-effect">
<div class="left"></div>
<div class="right">RightSide</div>
</section>

<section  class="scroll-effect">
<div class="right">LeftSide</div>  <div class="left"></div>
</section>

<section class="scroll-effect">
<div class="left"></div>
<div class="right">RightSide</div>
</section>
  
<section class="scroll-effect">
<div class="right">LeftSide</div>  <div class="left"></div>
</section>

<header class="indicator"></header>
<aside class="indicator"></aside>  

2. After that, add the following CSS styles to your project:

header.indicator, aside.indicator {
  background: black;
}

header.indicator {
  position: fixed;
  height: 15px;
  top: 0;
  left: 0;
  width: 0;
}

aside.indicator {
  width: 15px;
  position: fixed;
  left: 0;
  top: 0;
  height: 0;
}

section.scroll-effect{
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  font-size: 5em;
  font-weight: 700;
  letter-spacing: -10px;
}
section.scroll-effect:nth-child(1) .left {
  background: url(//unsplash.it/800) no-repeat center;
}
section.scroll-effect:nth-child(2) .left {
  background: url(//unsplash.it/801) no-repeat center;
}
section.scroll-effect:nth-child(3) .left {
  background: url(//unsplash.it/802) no-repeat center;
}
section.scroll-effect:nth-child(4) .left {
  background: url(//unsplash.it/803) no-repeat center;
}
section.scroll-effect div {
  transition: all ease-in 1s;
  overflow: hidden;
}
section.scroll-effect div:hover {
  width: 70%;
}
section.scroll-effect div:hover + div {
  width: 30%;
  font-size: 0.75em;
}
section.scroll-effect .left, section .right {
  width: 50%;
  height: 100%;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
section.scroll-effect .left {
  background: blue;
}

3. Finally, add the following JavaScript code and done.

var section = document.querySelectorAll('section.scroll-effect');
var header = document.querySelector('header.indicator');
var aside = document.querySelector('aside.indicator');
var winheight = window.innerHeight;
for (var i = 0; i < section.length; i++) {
  section[i].style.height = winheight + 'px';
  //  console.log(section[i]);
}
var total = winheight * section.length;
console.log(total - winheight);

function scroller(e) {
  var scroll = window.pageYOffset || document.documentElement.scrollTop;

  var percent = scroll / (total - winheight) * 100;

  aside.style.height = percent + '%';
  header.style.width = percent + '%';

  console.log('Scroll :' + scroll + ' Percent :' + percent + '%');
}

window.addEventListener('scroll', scroller, false);

That’s all! hopefully, you have successfully integrated this scroll indicator code snippet into your project. If you have any questions or are facing any issues, please feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X