如何制作数字计数器来计算 Wix Velo 中的多个元素

问题描述 投票:0回答:2

我正在 Wix 上制作一个网站,它有 Velo,它的工作方式类似于 javascript(我对编码不太了解)

我试图做一个从 0 计数到给定数字的数字计数器,我做到了,但我需要 4 个不同的计数器,不知道如何做,也许有人可以帮忙。

所以我的代码看起来像这样

$w.onReady(function() {});

let startNum = 0;
let endNum = 145;
const duration = 20;

$w.onReady(function() {
  setInterval(() => {
    countUp();
  }, duration);
});


function countUp() {
  if (startNum <= endNum) {
    $w('#StartNumber').text = startNum.toString();
    startNum++;
  }
}

#startnumber
是一个从 0 到 145 的文本元素 我想对另外 3 个元素
#startnumber2
、3 和 4 执行同样的操作。

this is what I'm trying to do

counter velo
2个回答
1
投票

计数逻辑可以提取到一个函数中,以便您可以为所有文本组件调用它。

$w.onReady(function () {
  count($w('#text1'), 0, 150, 1000);
  count($w('#text2'), 0, 250, 1000);
  count($w('#text3'), 0, 500, 1000);
  count($w('#text4'), 0, 1000, 1000);
});

function count(obj, start, end, duration) {
  let startTimestamp = null;
  const step = (timestamp) => {
    if (!startTimestamp) startTimestamp = timestamp;
    const progress = Math.min((timestamp - startTimestamp) / duration, 1);
    obj.text = `${Math.floor(progress * (end - start) + start)}`;
    if (progress < 1) {
      requestAnimationFrame(step);
    }
  };
  requestAnimationFrame(step);
}

演示:https://moshef9.wixsite.com/count-numbers

计数器函数取自:https://stackoverflow.com/a/60291224/863110


0
投票

如何将数字计数器连接到具有唯一数字的数据集并使其重复运行?

这是从名单中选出获胜者

© www.soinside.com 2019 - 2024. All rights reserved.