Post

HTML

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

 

SCSS

body {
  height: 4000px;
}
.header-bar {
  position: fixed;
  top: 0;
  z-index: 1;
  width: 100%;
}
.progress-bar {
  height: 2px;
  background: black;
  width: 0%;
}

 

JavaScript

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

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

 

'JavaScript related' 카테고리의 다른 글

JavaScript Arrow Function  (0) 2023.01.23
JavaScript Callback Function  (0) 2023.01.23
JavaScript Basic  (0) 2023.01.22
JavaScript preventDefault  (0) 2023.01.22
JavaScript addEventListener Basic  (0) 2023.01.22
▲ top