首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript如何循环数字计数器

JavaScript如何循环数字计数器
EN

Stack Overflow用户
提问于 2021-12-20 07:17:03
回答 1查看 207关注 0票数 0

我试图让下面的数字计数器只启动一次滚动到,然后让它循环5秒或10秒,请协助。我试过用:

代码语言:javascript
复制
$( '#yourdiv').scroll(function() {
    if ( $(this)[0].scrollHeight - $(this).scrollTop() <= $(this).outerHeight()){

因为卷轴的功能,但没有运气。请查看下面的代码。

代码语言:javascript
复制
const counterAnim = (qSelector, start = 0, end, duration = 8000) => {
 const target = document.querySelector(qSelector);
 let startTimestamp = null;
 const step = (timestamp) => {
  if (!startTimestamp) startTimestamp = timestamp;
  const progress = Math.min((timestamp - startTimestamp) / duration, 1);
  target.innerText = Math.floor(progress * (end - start) + start);
  if (progress < 1) {
   window.requestAnimationFrame(step);
  }
 };
 window.requestAnimationFrame(step);
};
//#endregion - end of - number counter animation

document.addEventListener("DOMContentLoaded", () => {
 counterAnim("#count1", 0, 10000,8000);
 counterAnim("#count2", 0, 40, 8000);
 counterAnim("#count3", 0, 5, 8000);
});
代码语言:javascript
复制
<div class="ohio-heading-sc heading text-left" id="ohio-custom-61aa36a52809a">
<h2 class="title"><span id="count1" class="display-4"></span>+  </h2>
<p class="subtitle">lorem ipsum</p>
</div>

<div class="ohio-heading-sc heading text-left" id="ohio-custom-61aa36a52809a">
<h2 class="title"><span id="count2" class="display-4"></span>+  YEARS</h2>
<p class="subtitle">lorem ipsum</p>
</div>

<div class="ohio-heading-sc heading text-left" id="ohio-custom-61aa36a52809a">
<h2 class="title"><span id="count3" class="display-4"></span>   YEAR</h2>
<p class="subtitle">lorem ipsum</p>
</div>

EN

回答 1

Stack Overflow用户

发布于 2021-12-20 10:35:07

mentioned.

  • Create

  • 将3种动画移动到一个函数中,以便您可以一次启动它们(对于好代码,很好,但可以复制代码),在DOMContentLoaded中调用

  • 而不是调用counterAnim,根据您的if条件调用它一个布尔变量,以确定计数器是否已经到达视图端口一次,因为用户可以一直滚动页面,但您只希望启动间隔一次。

代码语言:javascript
复制
const counterAnim = (qSelector, start = 0, end, duration = 8000) => {
  const target = document.querySelector(qSelector);
  let startTimestamp = null;
  const step = (timestamp) => {
    if (!startTimestamp) startTimestamp = timestamp;
    const progress = Math.min((timestamp - startTimestamp) / duration, 1);
    target.innerText = Math.floor(progress * (end - start) + start);
    if (progress < 1) {
      window.requestAnimationFrame(step);
    }
  };
  window.requestAnimationFrame(step);
};

const animateTexts = () => {
  counterAnim("#count1", 0, 10000, 8000);
  counterAnim("#count2", 0, 40, 8000);
  counterAnim("#count3", 0, 5, 8000);
}

let initiatedInterval = false;
$('#yourdiv').scroll(function() {
  if ($(this)[0].scrollHeight - $(this).scrollTop() <= $(this).outerHeight()) {
    animateTexts();

    if (!initiatedInterval) {
      initiatedInterval = true;
      // 8,000 - animation time + 10,000 - delay
      setInterval(animateTexts, 18_000);
    }
  }
});
代码语言:javascript
复制
body {
  margin: 0;
}

#yourdiv {
  height: 100vh;
  background: red;
  overflow: auto;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="yourdiv">
  <div style="height: 500px;">Placeholder with 500px height, please scroll</div>
  <div class="ohio-heading-sc heading text-left" id="ohio-custom-61aa36a52809a">
    <h2 class="title"><span id="count1" class="display-4"></span>+ </h2>
    <p class="subtitle">lorem ipsum</p>
  </div>

  <div class="ohio-heading-sc heading text-left" id="ohio-custom-61aa36a52809a">
    <h2 class="title"><span id="count2" class="display-4"></span>+ YEARS</h2>
    <p class="subtitle">lorem ipsum</p>
  </div>

  <div class="ohio-heading-sc heading text-left" id="ohio-custom-61aa36a52809a">
    <h2 class="title"><span id="count3" class="display-4"></span> YEAR</h2>
    <p class="subtitle">lorem ipsum</p>
  </div>
</div>

我鼓励您深入阅读,了解我发布的代码片段,而不仅仅是复制/粘贴它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70418509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档