【发布时间】:2021-05-28 16:16:10
【问题描述】:
我在 Vanilla JS 中创建了一个计数函数。一切正常。当您向下滚动到它时,计数器就会启动。不幸的是,大数字 35 000 000 在 iOS Safari 浏览器中显示为 NaN。我不知道为什么。请帮忙。看起来大数字在 iOS Safari 浏览器中不起作用。
var animationDuration = 3000;
var frameDuration = 1000 / 60;
var totalFrames = Math.round(animationDuration / frameDuration);
var easeOutQuad = t => t * (2 - t);
var animateCountUp = el => {
let frame = 0;
var countTo = parseInt(el.innerHTML.replace(/ /g, ''), 10);
var counter = setInterval(() => {
frame++;
var progress = easeOutQuad(frame / totalFrames);
var currentCount = Math.round(countTo * progress);
if (parseInt(el.innerHTML, 10) !== currentCount) {
el.textContent = currentCount.toLocaleString().replace(/,/g, ' ');
}
if (frame === totalFrames) {
clearInterval(counter);
}
}, frameDuration);
};
var count = document.querySelectorAll('.countup'),
once = 1;
document.addEventListener('scroll', function() {
if (once == 1 && count[0].getBoundingClientRect().top < window.innerHeight) {
once = 0;
count.forEach(animateCountUp);
}
});
<div style="position: fixed">
<span class="countup">12 500</span>
<span class="countup">35 000 000</span>
</div>
<div style="height: 5000px"></div>
【问题讨论】:
-
欢迎来到 Stack Overflow!我已将您的 HTML 和 JavaScript 复制到 Stack Snippet 中(
[<>]工具栏按钮); here's how to do one,但它似乎没有做任何事情。您能否更新它以使其成为minimal reproducible example,并提供有关如何查看问题的说明?我认为它需要一些东西来滚动,但我不想猜测你真正使用的是什么。 -
非常感谢。在我看来,它在这种情况下不起作用,因为它无法滚动。
-
现在,有了 p 标签,它就可以工作了。
-
刚刚在 Safari 14.0.3 中尝试过,它似乎可以按预期工作。
-
我在 ios 14.4 上的移动 safari 上进行了测试,它仅在大数字中显示 NaN
标签: javascript mobile-safari nan