Javascript 如何停止计数器动画?

Javascript 如何停止计数器动画?,javascript,animation,scroll,counter,viewport,Javascript,Animation,Scroll,Counter,Viewport,您好,我正在尝试在类位于视口中时启动计数器动画,但这会在每次滚动时继续制作计数器动画 如何停止动画?当我滚动到有数字计数器的部分时,只需使用此计数器一次 谢谢 函数线性(持续时间、范围、当前){ 返回((持续时间*2)/数学功率(范围,2))*当前; } //反动画 //线性缓和 线性函数(持续时间、范围、电流){ 返回((持续时间*2)/数学功率(范围,2))*当前; } 函数animateValue(id、开始、持续时间、缓和){ var end=parseInt(document.get

您好,我正在尝试在类位于视口中时启动计数器动画,但这会在每次滚动时继续制作计数器动画

如何停止动画?当我滚动到有数字计数器的部分时,只需使用此计数器一次

谢谢

函数线性(持续时间、范围、当前){
返回((持续时间*2)/数学功率(范围,2))*当前;
}
//反动画
//线性缓和
线性函数(持续时间、范围、电流){
返回((持续时间*2)/数学功率(范围,2))*当前;
}
函数animateValue(id、开始、持续时间、缓和){
var end=parseInt(document.getElementById.textContent,10);
var范围=结束-开始;
无功电流=启动;
var增量=结束>开始?1:-1;
var obj=document.getElementById(id);
var startTime=新日期();
var步骤=函数(){
电流+=增量;
obj.innerHTML=当前;
如果(当前!==结束){
设置超时(步长、缓和(持续时间、范围、电流));
}否则{
console.log(“放松:”,放松);
log(“运行时间:”,new Date()-startTime);
控制台日志(“”);
}
};
设置超时(步长、缓和(持续时间、范围、开始));
}
常量计数器视口=函数(){
让艾伦斯;
让窗高;
函数init(){
elems=document.queryselectoral(“.numbers stats”);
windowHeight=window.innerHeight;
addEventHandlers();
检查位置();
}
函数addEventHandlers(){
window.addEventListener(“滚动”,选中位置);
addEventListener(“resize”,init);
}
函数checkPosition(){
对于(变量i=0;i如果(positionFromTop-windowHeight在计数器运行一次之后,您只需调用另一个函数即可删除事件侦听器。
更新代码如下:

<html>
<body>
<section>
    <div class="content-max-width our-solution flex flex-wrap padding-mobile justify-between">


        <div class="w-full md:w-6c md:pt-4 ">
            <h4>Our Solution</h4>
            <p>
                Genius Sports’ partnership with FIBA began back in 2004 with the development of FIBA Organizer,
                a digital system for the management of any basketball competition format.
            </p>
            <p>
                After four years of successful cooperation, FIBA requested the development of a tool that would enable it to
                centralise its data through the real-time collection of statistics. Genius Sports subsequently launched FIBA
                LiveStats, giving leagues
                and federations of all sizes an official data feed that could underpin their relationships with fans, commercial
                partners and coaches.
            </p>
        </div>
    </div>
</section>
<section>
    <div class="content-max-width our-solution flex flex-wrap padding-mobile justify-between">


        <div class="w-full md:w-6c md:pt-4 ">
            <h4>Our Solution</h4>
            <p>
                Genius Sports’ partnership with FIBA began back in 2004 with the development of FIBA Organizer,
                a digital system for the management of any basketball competition format.
            </p>
            <p>
                After four years of successful cooperation, FIBA requested the development of a tool that would enable it to
                centralise its data through the real-time collection of statistics. Genius Sports subsequently launched FIBA
                LiveStats, giving leagues
                and federations of all sizes an official data feed that could underpin their relationships with fans, commercial
                partners and coaches.
            </p>
        </div>
    </div>
</section>
<section>
    <div class="content-max-width our-solution flex flex-wrap padding-mobile justify-between">


        <div class="w-full md:w-6c md:pt-4 ">
            <h4>Our Solution</h4>
            <p>
                Genius Sports’ partnership with FIBA began back in 2004 with the development of FIBA Organizer,
                a digital system for the management of any basketball competition format.
            </p>
            <p>
                After four years of successful cooperation, FIBA requested the development of a tool that would enable it to
                centralise its data through the real-time collection of statistics. Genius Sports subsequently launched FIBA
                LiveStats, giving leagues
                and federations of all sizes an official data feed that could underpin their relationships with fans, commercial
                partners and coaches.
            </p>
        </div>
    </div>
</section>
<div class="numbers-stats">
    <div class="content-max-width mx-auto mt-4 flex flex-wrap justify-between">
        <div class="w-6c md:w-full">
            <h2 class="counter-number" id="counterNumberFiba">80</h2><span class="million-text">+</span>
            <h5>MATCHES PER YEAR POWERED BY FIBA LIVESTATS</h5>
        </div>
        <div class="w-6c md:w-full">
            <h2 class="counter-number" id="counterNumberFiba2">25</h2><span class="million-text">m+</span>
            <h5>BASKETBALL FANS GLOBALLY USINHG LIVESTATS' GAMECENTRES</h5>
        </div>
        <div class="w-6c mt-2 md:w-full">
            <h2 class="counter-number" id="counterNumberFiba3">200</h2><span class="million-text">+</span>
            <h5>LEAGUES AND FEDERATIONS BENEFITTING FROM THIS LANDMARK PARTNERSHIP</h5>
        </div>
    </div>
</div>
<style>
.numbers-stats {
  h2.counter-number, .million-text {
    font-size: 5rem;
    color: $GREEN;
    font-weight: 300;
    display: inline;

    @screen md {
      font-size: 9.1rem;
    }
  }

  h5 {
    color: $TEXT-GRAY;
    text-transform: uppercase;
  }
}
</style>
<script>
//counter animation
//Linear easing
function linear(duration, range, current) {
  return ((duration * 2) / Math.pow(range, 2)) * current;
}

//Quadratic easing
function animateValue(id, start, duration, easing) {
  var end = parseInt(document.getElementById(id).textContent, 10);
  var range = end - start;
  var current = start;
  var increment = end > start ? 1 : -1;
  var obj = document.getElementById(id);
  var startTime = new Date();

  var step = function() {
    current += increment;
    obj.innerHTML = current;

    if (current !== end) {
      setTimeout(step, easing(duration, range, current));
    } else {
      console.log("Easing: ", easing);
      console.log("Elapsed time: ", new Date() - startTime);
      console.log("");
    }
  };

  setTimeout(step, easing(duration, range, start));
}

const counterViewport = function() {
  let elems;
  let windowHeight;
  function init() {
    elems = document.querySelectorAll(".numbers-stats");
    windowHeight = window.innerHeight;
    addEventHandlers();
    checkPosition();
  }
  function addEventHandlers() {
    window.addEventListener("scroll", checkPosition);
    window.addEventListener("resize", init);
  }
  function checkPosition() {
    for (var i = 0; i < elems.length; i++) {
      var positionFromTop = elems[i].getBoundingClientRect().top;
      if (positionFromTop - windowHeight <= 0) {
        animateValue("counterNumberFiba", 0, 2000, linear);
        animateValue("counterNumberFiba2", 0, 1400, linear);
        animateValue("counterNumberFiba3", 0, 700, linear);
        removeHandlers();
      }
    }
  }
  function removeHandlers(){
    window.removeEventListener("scroll", checkPosition);
    window.removeEventListener("resize", init);

}

  return {
    init: init
  };
};
counterViewport().init();

</script>
</body>
</html>

我们的解决方案

Genius Sports与国际篮联的合作始于2004年国际篮联组织者的发展,
用于管理任何篮球比赛格式的数字系统。

经过四年的成功合作,国际篮联要求开发一种工具,使其能够 通过实时收集统计数据来集中数据。Genius Sports随后推出了FIBA LiveStats,给予联盟 而各种规模的联合会则是一个官方数据源,可以巩固他们与粉丝、商业伙伴的关系 合作伙伴和教练。

我们的解决方案 Genius Sports与国际篮联的合作始于2004年国际篮联组织者的发展, 用于管理任何篮球比赛格式的数字系统。

经过四年的成功合作,国际篮联要求开发一种工具,使其能够 通过实时收集统计数据来集中数据。Genius Sports随后推出了FIBA LiveStats,给予联盟 而各种规模的联合会则是一个官方数据源,可以巩固他们与粉丝、商业伙伴的关系 合作伙伴和教练。

我们的解决方案 Genius Sports与国际篮联的合作始于2004年国际篮联组织者的发展, 用于管理任何篮球比赛格式的数字系统。

经过四年的成功合作,国际篮联要求开发一种工具,使其能够 通过实时收集统计数据来集中数据。Genius Sports随后推出了FIBA LiveStats,给予联盟 而各种规模的联合会则是一个官方数据源,可以巩固他们与粉丝、商业伙伴的关系 合作伙伴和教练。

80+ 由FIBA LIVESTATS支持的每年比赛 25米+ 使用LIVESTATS游戏中心的全球篮球迷 200+ 联盟和联合会受益于这一里程碑式的合作关系 .数字统计{ h2.计数器编号,.百万文本{ 字体大小:5rem; 颜色:$绿色; 字体大小:300; 显示:内联; @屏幕md{ 字号:9.1rem; } } h5{ 颜色:$TEXT-GRAY; 文本转换:大写; } } //反动画 //线性缓和 线性函数(持续时间、范围、电流){ 返回((持续时间*2)/数学功率(范围,2))*当前; } //二次缓和 函数animateValue(id、开始、持续时间、缓和){ var end=parseInt(document.getElementById.textContent,10); var范围=结束-开始; 无功电流=启动; var增量=结束>开始?1:-1; var obj=document.getElementById(id); var startTime=新日期(); var步骤=函数(){ 电流+=增量; obj.innerHTML=当前; 如果(当前!==结束){ 设置超时(步长、缓和(持续时间、范围、电流)); }否则{ console.log(“放松:”,放松); log(“运行时间:”,new Date()-startTime); 控制台日志(“”); } }; 设置超时(步长、缓和(持续时间、范围、开始)); } 常量计数器视口=函数(){ 让艾伦斯; 让窗高; 函数init(){ elems=document.queryselectoral(“.numbers stats”); windowHeight=window.innerHeight; addEventHandlers(); 检查位置(); } 函数addEventHandlers(){ window.addEventListener(“滚动”,选中位置);
<html>
<body>
<section>
    <div class="content-max-width our-solution flex flex-wrap padding-mobile justify-between">


        <div class="w-full md:w-6c md:pt-4 ">
            <h4>Our Solution</h4>
            <p>
                Genius Sports’ partnership with FIBA began back in 2004 with the development of FIBA Organizer,
                a digital system for the management of any basketball competition format.
            </p>
            <p>
                After four years of successful cooperation, FIBA requested the development of a tool that would enable it to
                centralise its data through the real-time collection of statistics. Genius Sports subsequently launched FIBA
                LiveStats, giving leagues
                and federations of all sizes an official data feed that could underpin their relationships with fans, commercial
                partners and coaches.
            </p>
        </div>
    </div>
</section>
<section>
    <div class="content-max-width our-solution flex flex-wrap padding-mobile justify-between">


        <div class="w-full md:w-6c md:pt-4 ">
            <h4>Our Solution</h4>
            <p>
                Genius Sports’ partnership with FIBA began back in 2004 with the development of FIBA Organizer,
                a digital system for the management of any basketball competition format.
            </p>
            <p>
                After four years of successful cooperation, FIBA requested the development of a tool that would enable it to
                centralise its data through the real-time collection of statistics. Genius Sports subsequently launched FIBA
                LiveStats, giving leagues
                and federations of all sizes an official data feed that could underpin their relationships with fans, commercial
                partners and coaches.
            </p>
        </div>
    </div>
</section>
<section>
    <div class="content-max-width our-solution flex flex-wrap padding-mobile justify-between">


        <div class="w-full md:w-6c md:pt-4 ">
            <h4>Our Solution</h4>
            <p>
                Genius Sports’ partnership with FIBA began back in 2004 with the development of FIBA Organizer,
                a digital system for the management of any basketball competition format.
            </p>
            <p>
                After four years of successful cooperation, FIBA requested the development of a tool that would enable it to
                centralise its data through the real-time collection of statistics. Genius Sports subsequently launched FIBA
                LiveStats, giving leagues
                and federations of all sizes an official data feed that could underpin their relationships with fans, commercial
                partners and coaches.
            </p>
        </div>
    </div>
</section>
<div class="numbers-stats">
    <div class="content-max-width mx-auto mt-4 flex flex-wrap justify-between">
        <div class="w-6c md:w-full">
            <h2 class="counter-number" id="counterNumberFiba">80</h2><span class="million-text">+</span>
            <h5>MATCHES PER YEAR POWERED BY FIBA LIVESTATS</h5>
        </div>
        <div class="w-6c md:w-full">
            <h2 class="counter-number" id="counterNumberFiba2">25</h2><span class="million-text">m+</span>
            <h5>BASKETBALL FANS GLOBALLY USINHG LIVESTATS' GAMECENTRES</h5>
        </div>
        <div class="w-6c mt-2 md:w-full">
            <h2 class="counter-number" id="counterNumberFiba3">200</h2><span class="million-text">+</span>
            <h5>LEAGUES AND FEDERATIONS BENEFITTING FROM THIS LANDMARK PARTNERSHIP</h5>
        </div>
    </div>
</div>
<style>
.numbers-stats {
  h2.counter-number, .million-text {
    font-size: 5rem;
    color: $GREEN;
    font-weight: 300;
    display: inline;

    @screen md {
      font-size: 9.1rem;
    }
  }

  h5 {
    color: $TEXT-GRAY;
    text-transform: uppercase;
  }
}
</style>
<script>
//counter animation
//Linear easing
function linear(duration, range, current) {
  return ((duration * 2) / Math.pow(range, 2)) * current;
}

//Quadratic easing
function animateValue(id, start, duration, easing) {
  var end = parseInt(document.getElementById(id).textContent, 10);
  var range = end - start;
  var current = start;
  var increment = end > start ? 1 : -1;
  var obj = document.getElementById(id);
  var startTime = new Date();

  var step = function() {
    current += increment;
    obj.innerHTML = current;

    if (current !== end) {
      setTimeout(step, easing(duration, range, current));
    } else {
      console.log("Easing: ", easing);
      console.log("Elapsed time: ", new Date() - startTime);
      console.log("");
    }
  };

  setTimeout(step, easing(duration, range, start));
}

const counterViewport = function() {
  let elems;
  let windowHeight;
  function init() {
    elems = document.querySelectorAll(".numbers-stats");
    windowHeight = window.innerHeight;
    addEventHandlers();
    checkPosition();
  }
  function addEventHandlers() {
    window.addEventListener("scroll", checkPosition);
    window.addEventListener("resize", init);
  }
  function checkPosition() {
    for (var i = 0; i < elems.length; i++) {
      var positionFromTop = elems[i].getBoundingClientRect().top;
      if (positionFromTop - windowHeight <= 0) {
        animateValue("counterNumberFiba", 0, 2000, linear);
        animateValue("counterNumberFiba2", 0, 1400, linear);
        animateValue("counterNumberFiba3", 0, 700, linear);
        removeHandlers();
      }
    }
  }
  function removeHandlers(){
    window.removeEventListener("scroll", checkPosition);
    window.removeEventListener("resize", init);

}

  return {
    init: init
  };
};
counterViewport().init();

</script>
</body>
</html>