0

Please tell me how to solve the issue of the appearance of animation when scrolling. I need this script to work at the moment when the user scrolls and sees this block.

$(function() {
  $(".skill_per").each(function() {
    $this = $(this);
    var per = $(this).attr("per");
    $this.css("width", per + "%");
    $this.find(".value").text(per + "%").css("opacity", "1");
  });
});
.skill {
  margin-bottom: 20px;
}

.skill_bar {
  height: 20px;
  background-color: #cacaca;
  border-radius: 231px;
  width: 120px;
}

.skill_per {
  position: relative;
  height: 20px;
  background: url(progress_bar.png)#fc0000;
  border-radius: 231px;
  width: 0;
  -webkit-transition: 2s linear;
  -moz-transition: 2s linear;
  -ms-transition: 2s linear;
  -o-transition: 2s linear;
  transition: 2s linear;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="skill">
  <div class="skill_bar">
    <div class="skill_per" per="43"></div>
  </div>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

0

The following code sets a horizontal progressbar to the entire width of the page, which shows the amount of progress according to the vertical scrolling of the progressbar

I will interpret the script lines for you

Current scroll position

var scroll = $(document).scrollTop();

Scrollable page height

 var height = document.getElementsByTagName("BODY")[0].scrollHeight;

Adjust the actual height according to the scroll bar

height = height - ($(window).height());

Calculate the ratio of height to current position

var ratio = height / scroll;

The width of the pug is gray, which is the size of a full screen

 $parent = $('.skill_bar');

Red Progressber width

$this = $('.skill_per-progress');

Divide the width of the gray processor by the ratio

var width = $parent.width() / ratio;
$this.css("width", width + "px");

$(window).scroll(function(event) {
  var scroll = $(document).scrollTop();
  var height = document.getElementsByTagName("BODY")[0].scrollHeight;
  height = height - ($(window).height());
  var ratio = height / scroll;

  $parent = $('.skill_bar');
  $this = $('.skill_per_progress');
  var width = $parent.width() / ratio;
  $this.css("width", width + "px");

  var per = Math.round($this.width() / $parent.width() * 100);
  if (scroll == 0)
    per = 0;
  else if (scroll >= height)
    per = 100;
  else if (per >= 100)
    per = 100;
  else if (per < 0)
    per = 0;
  $this.find("#value").text(per + "%").css("opacity", "1");
});
body {
  height: 5000px;
}

.skill {
  margin-bottom: 20px;
}

.skill_bar {
  position: fixed;
  height: 20px;
  background-color: #cacaca;
  border-radius: 231px;
  width: 100%;
  top: 0;
  left: 0;
}

.skill_per_progress {
  margin-left: 1px;
  position: relative;
  height: 20px;
  background: url(progress_bar.png)#fc0000;
  border-radius: 231px;
  width: 0px;
  max-width: 100%;
  -webkit-transition: .01s linear;
  -moz-transition: .01s linear;
  -ms-transition: .01s linear;
  -o-transition: .01s linear;
  transition: .01s linear;
  text-align: center;
}

.skill_per_progress span {
  color: white;
  font-weight: bold;
  top;
  0;
  z-index: 10;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="skill">
  <div class="skill_bar">
    <div class="skill_per_progress"><span id="value"></span></div>
  </div>
</div>
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17