亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 JavaScript 中制作修改后的計數器動畫?

如何在 JavaScript 中制作修改后的計數器動畫?

森林海 2023-08-24 21:02:19
我需要創建一個顯示兩個按鈕的網頁:“開始”和“停止”。單擊“開始”時,我需要每秒顯示一個方程。例如:假設起始數字為100,那么在動畫中,網頁首先會顯示:100 + 1 = 101然后每隔一秒,它應該顯示:100 + 2 = 102;  100 + 3 = 103; 100 + 4 = 104;等等...每1秒一次。我已經能夠創建計數器動畫,但是,我對在此之后如何進展感到困惑。到目前為止,這是我的代碼<html><head>  <script>  var counter = 100;  var counterSchedule;  function startCounterAnimation(){    counterSchedule = setInterval(showCounter, 1000);  }  function showCounter(){    counter = counter + 1;    var counterSpan = document.getElementById("counter");    counterSpan.innerHTML = counter;  }  function stopCounterAnimation(){    clearInterval(counterSchedule);  }  </script></head><body>  <button onClick="startCounterAnimation()">Start Animation</button>  <button onClick="stopCounterAnimation()">Stop Animation</button>  <br /><br />  <span id="counter"></span></body></html>任何幫助將不勝感激!
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

使用下面的代碼嘗試一下。這就是您要找的嗎?


  var counter = 100;

  var counterSchedule;

  let i = 1;


  function startCounterAnimation(){


    counterSchedule = setInterval(showCounter, 1000);

  }


  function showCounter(){


    counter = counter + 1;

    var counterSpan = document.getElementById("counter");

    counterSpan.innerHTML = `100 + ${i} = ${counter}`;

    i++;

  }


  function stopCounterAnimation(){


    clearInterval(counterSchedule);

  }

<html>

<head>

</head>

<body>

  <button onClick="startCounterAnimation()">Start Animation</button>

  <button onClick="stopCounterAnimation()">Stop Animation</button>


  <br /><br />


  <span id="counter"></span>

</body>

</html>


查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號