【问题标题】:How to solve the issue of the appearance of animation when scrolling?如何解决滚动时出现动画的问题?
【发布时间】:2021-09-30 11:32:36
【问题描述】:

请告诉我如何解决滚动时出现动画的问题。当用户滚动并看到这个块时,我需要这个脚本来工作。

$(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>

【问题讨论】:

  • 我看动画没什么问题,能详细点吗?
  • 动画作品。但是当用户将页面滚动到此块时,我需要运行此动画。
  • 试试这个解决方案:stackoverflow.com/a/27462500/12540953

标签: javascript html jquery css


【解决方案1】:

以下代码设置一个水平的progressbar为页面的整个宽度,根据progressbar的垂直滚动显示进度量

我会为你解释脚本行

当前滚动位置

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");

$(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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    相关资源
    最近更新 更多