February 8, 2022

Прогресс пролистывания странички

Пример работы: https://tap.one-way.dev/p/8d8b50/

Код:

<div class="bar-container">

  <div class="meter">
    <span class="mariam animate"></span>
    <p class="progress-counter">0%</p>
  </div>



</div>
<style>
  

:root {
  /* Цвет прогрессбара */
  --progress-color: green;
  /* Высота прогрессбара */
  --progress-height: 30px;
  /* расстояние до верха экрана */
  --progress-top: 0px;
  /* Отступы прогрессбара с боков */
  --progress-margins: 3px;
  /* Видимость процентов относительно полосы. Если надо убрать проценты под полосу, поставьте вместо 0. Если надо сделать видимой, поставьте 1 */
  --progress-counter-index: 10;
}
.bar-container {
  z-index: 30;
  position: fixed;
  top: var(--progress-top);
  left: 50%;
  transform: translate(-50%, 0px);
  width: calc(100% - var(--progress-margins));
  margin-left: auto;
  margin-right: auto;
}


/* Usage examples start here */
.stripes {
  content: "";
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
  -webkit-animation: none;
  -moz-animation: none;
  -ms-animation: none;
  animation: none;
  background: -webkit-linear-gradient(-45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent);
  background: -moz-linear-gradient(-45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent);
  background: linear-gradient(-45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent);
  -webkit-background-size: 50px 50px;
  -moz-background-size: 50px 50px;
  background-size: 50px 50px;
}

.meter {
  overflow: hidden;
  -webkit-box-shadow: rgba(0, 0, 0, 0.1) 0 1px 2px inset;
  -moz-box-shadow: rgba(0, 0, 0, 0.1) 0 1px 2px inset;
  box-shadow: rgba(0, 0, 0, 0.1) 0 1px 2px inset;
  position: relative;
  border-radius: 3px;
 background-image: linear-gradient(to bottom,#ebebeb 0,#f5f5f5 100%);
  height: var(--progress-height);


}
.meter .animate {
  content: "";
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
  -webkit-animation: big 2s linear infinite;
  -moz-animation: big 2s linear infinite;
  -ms-animation: big 2s linear infinite;
  animation: big 2s linear infinite;
  background: -webkit-linear-gradient(-45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent);
  background: -moz-linear-gradient(-45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent);
  background: linear-gradient(-45deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent);
  -webkit-background-size: 50px 50px;
  -moz-background-size: 50px 50px;
  background-size: 50px 50px;
}
.meter > span {
  -webkit-box-shadow: rgba(255, 255, 255, 0.3) 0 2px 9px inset, rgba(0, 0, 0, 0.4) 0 -2px 6px inset;
  -moz-box-shadow: rgba(255, 255, 255, 0.3) 0 2px 9px inset, rgba(0, 0, 0, 0.4) 0 -2px 6px inset;
  box-shadow: rgba(255, 255, 255, 0.3) 0 2px 9px inset, rgba(0, 0, 0, 0.4) 0 -2px 6px inset;
  display: block;
  height: 100%;
  overflow: hidden;
  position: relative;
  background: #2ac052, -webkit-gradient(linear, 50% 100%, 50% 0%, color-stop(37%, #2ac052), color-stop(69%, #56f056));
  background: #2ac052, -webkit-linear-gradient(center bottom, #2ac052 37%, #56f056 69%);
  background: #2ac052, -moz-linear-gradient(center bottom, #2ac052 37%, #56f056 69%);
  background: #2ac052, linear-gradient(center bottom, #2ac052 37%, #56f056 69%);
}
.meter .mariam {
  width: 0%;
  -moz-border-radius-topleft: 3px;
  -webkit-border-top-left-radius: 3px;
  border-top-left-radius: 3px;
  -moz-border-radius-bottomleft: 3px;
  -webkit-border-bottom-left-radius: 3px;
  border-bottom-left-radius: 3px;
  -moz-border-radius-topright: 3px;
  -webkit-border-top-right-radius: 3px;
  border-top-right-radius: 3px;
  -moz-border-radius-bottomright: 3px;
  -webkit-border-bottom-right-radius: 3px;
  border-bottom-right-radius: 3px;
  background-color: var(--progress-color);
  position: relative;
}
.meter .mariam:after {
  background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, rgba(255, 255, 255, 0.5)), color-stop(100%, rgba(255, 255, 255, 0.1)));
  background: -webkit-linear-gradient(top, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.1));
  background: -moz-linear-gradient(top, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.1));
  background: linear-gradient(top, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.1));
  -moz-border-radius-topleft: 3px;
  -webkit-border-top-left-radius: 3px;
  border-top-left-radius: 3px;
  -moz-border-radius-bottomleft: 3px;
  -webkit-border-bottom-left-radius: 3px;
  border-bottom-left-radius: 3px;
  -moz-border-radius-topright: 3px;
  -webkit-border-top-right-radius: 3px;
  border-top-right-radius: 3px;
  -moz-border-radius-bottomright: 3px;
  -webkit-border-bottom-right-radius: 3px;
  border-bottom-right-radius: 3px;
  content: "";
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
}
/* СЧЕТЧИК ПРОГРЕССА */
.progress-counter {
  margin: 0px;
position: absolute;
  left:50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: transparent;
  text-shadow: 0px 0px 1px rgba(0,0,0,0.3);
  z-index: var(--progress-counter-index);
}
@-moz-keyframes big {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 50px 50px;
  }
}

@-webkit-keyframes big {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 50px 50px;
  }
}

@-ms-keyframes big {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 50px 50px;
  }
}

@keyframes big {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 50px 50px;
  }
}
</style>

<script>
document.querySelector('.page.vue').addEventListener("scroll", () => {
var totalscroll = document.querySelector('.page.vue').scrollHeight - window.innerHeight;
var currentscroll = document.querySelector('.page.vue').scrollTop;
  console.log(totalscroll)
  console.log(currentscroll)
document.querySelector('.mariam').style.width = Math.ceil((currentscroll / totalscroll) * 100) + '%';
  var result = (currentscroll / totalscroll);
  if (result > 0.99) {result = 1}
  document.querySelector('.progress-counter').textContent = Math.ceil(Math.trunc(result * 100)) + '%';
});
</script>