samo-opusteno Web Razvoj
Pages: 1
Header scroll indikator By: MarkoKg Date: Septembar 07, 2018, 09:26:42
Header scroll indikator

HTML:
Kod: [Izaberi]
<div class="header">
  <h2>Scroll indikator</h2>
  <div class="progress-container">
    <div class="progress-bar" id="myBar"></div>
  </div>
</div>

<div>sadr┼żaj ovde...</div>

CSS:
Kod: [Izaberi]
/* Style the header: fixed position (always stay at the top) */
.header {
  position: fixed;
  top: 0;
  z-index: 1;
  width: 100%;
  background-color: #f1f1f1;
}

/* The progress container (grey background) */
.progress-container {
  width: 100%;
  height: 8px;
  background: #ccc;
}

/* The progress bar (scroll indicator) */
.progress-bar {
  height: 8px;
  background: #4caf50;
  width: 0%;
}

I na kraju, javascript:
Kod: [Izaberi]
// When the user scrolls the page, execute myFunction
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 + "%";
}

Demo:
https://jsfiddle.net/46hd0ry2/1/