css 我想在向下滚动到网站的特定部分时触发进度条的动画

yzxexxkh  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(109)

我已经添加了动画,把进度条在网站的一节,但它得到与页面加载触发。
我希望每当用户访问该特定部分时都能触发动画。

body {
  margin: 0;
  background-color: #101214;
}

.title {
  text-align: center;
  color: #fff;
}

.right {
  color: white;
}

.progress-bar {
  background-color: #959595;
  width: 600px;
  height: 10px;
  border-radius: 5px;
}

.progress-bar div {
  height: 1rem;
  border-radius: 5px;
  width: 0%;
}

.progress-bar div span {
  height: 40px;
  width: 40px;
  float: right;
  margin-top: -12px;
  color: #d1d8e0;
  display: flex;
  align-items: center;
  justify-self: center;
  font-size: 0.625rem;
}

.bar {
  background-color: #374850;
  animation: bar 1s linear forwards;
}

@keyframes bar {
  100% {
    width: 100%;
  }
}
<h1 class="title">My Skills</h1>
<div class="right">
  <h2>Builder Reliability</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
  <h2>Construction Quality</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
  <h2>Connectivity</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
  <h2>Competitive pricing</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
</div>
dced5bon

dced5bon1#

如果你想激活动画,我们可以使用mouseleave事件来保存它。

$( ".progress-bar" ).mouseleave(function() {
  $( this ).find(".bar").removeClass('bar').addClass('animClass');
});
body {
  margin: 0;
  background-color: #101214;
}

.title {
  text-align: center;
  color: #fff;
}

.right {
  color: white;
}

.progress-bar {
  background-color: #959595;
  width: 600px;
  height: 10px;
  border-radius: 5px;
}

.progress-bar div {
  height: 1rem;
  border-radius: 5px;
  width: 0%;
}

.progress-bar div span {
  height: 40px;
  width: 40px;
  float: right;
  margin-top: -12px;
  color: #d1d8e0;
  display: flex;
  align-items: center;
  justify-self: center;
  font-size: 0.625rem;
}

.progress-bar:hover .bar {
  background-color: #374850;
  animation: bar 1s linear forwards;
}

.animClass {
  background-color: #374850;
  animation: bar 1s linear forwards;
}

@keyframes bar {
  100% {
    width: 100%;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="title">My Skills</h1>
<div class="right">
  <h2>Builder Reliability</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
  <h2>Construction Quality</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
  <h2>Connectivity</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
  <h2>Competitive pricing</h2>
  <div class="progress-bar">
    <div class="bar"><span>100%</span></div>
  </div>
</div>
wmomyfyw

wmomyfyw2#

这是你想要的吗
悬停将执行以下操作:

.progress-bar:hover .bar {
  background-color: #374850;
  animation: bar 1s linear forwards;
}

x一个一个一个一个x一个一个二个x

相关问题