原生js实现下滑到当前模块进度条填充

<div style="height: 1500px;">

</div>
<div class="progress-container">
    <div class="progress-bar" data-progress="90%"><p class="progress-text">Google Ads在Google搜索引擎上覆盖超过90%的互联网用户。</p></div>
    <p class="progress-tag">90%</p>
    <div class="progress-bar" data-progress="60%"><p class="progress-text">超过2/3的潜在用户会被精准定位的Google Ads付费广告所吸引,点进进入。</p></div>
    <p class="progress-tag">2/3</p>
    <div class="progress-bar" data-progress="90%"><p class="progress-text">覆盖全球超过200万个网站和应用,可触及到网上90%的互联网用户。</p></div>
    <p class="progress-tag">200w+</p>
  </div>
  
  <style>
    .progress-container {
  margin: 50px 0;
}

.progress-bar {
  width: 80%;
  height: 80px;
  background-color: #ffffff;
  border-radius: 5px;
  /* overflow: hidden; */
  position: relative;
  margin-bottom: 20px;
  display: inline-block;
  margin-right: 30px;
}

.progress-bar::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: var(--progress-width, 0); /* 使用CSS变量 */
  background-color: #000000;
  transition: width 2s ease;
  border-radius: 5px 0 0 5px;
}
.progress-text{
    position: relative;
    z-index: 1;
    margin: 0;
    line-height: 30px;
    font-size: 16px;
    white-space: normal;
    color: #ffffff;
    width: 55%;
    text-align: left;
    margin-top: 10px;
    margin-left: 15px;
}
.progress-tag{
    width: 80px;
    height: 80px;
    padding: 20px;
    font-weight: 700;
    display: inline-block;
    margin: 0;
    border-radius: 50%;
    color: #ffffff;
    background-color: #000000;
    text-align: center;
    line-height: 40px;
    margin-bottom: 20px;
}

@media screen and (max-width:768px) {
    .progress-container {
        margin: 50px 10px;
    }
    .progress-bar {
        width: 68%;
        margin-right: 20px;
    }
    .progress-text {
        line-height: 18px;
        font-size: 12px;
        width: 55%;
    }
    .google-six-content ul li h3 {
        width: 150px;
        height: 150px;
        line-height: 130px;
    }
}


  </style>

  <script>
document.addEventListener('DOMContentLoaded', function() {
  var progressBars = document.querySelectorAll('.progress-bar');

  document.addEventListener('scroll', function() {
    var windowHeight = window.innerHeight;
  
    progressBars.forEach(function(bar) {
      var rect = bar.getBoundingClientRect();
      
      if (rect.top < windowHeight) {
        var progress = bar.getAttribute('data-progress');
        bar.style.setProperty('--progress-width', progress);
      }
    });
  });
});



  </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值