gpt4 book ai didi

用于大小数字的 Javascript 计数器

转载 作者:行者123 更新时间:2023-12-05 00:32:46 28 4
gpt4 key购买 nike

我一直在为网页设计一个计数器,但我一直被这个我无法解决的功能所困扰。
我有一个 4 个 div 的计数器,因为其中两个是小数字,另外两个是大数字,所以第一个运行得很快,我看不到它们的功能。
有人知道如何设置 js 函数,以便它们可以以不同的速度运行,同时完成吗?

let section_counter = document.querySelector('#section_counter');
let counters = document.querySelectorAll('.counter-item .counter');

// Scroll Animation

let CounterObserver = new IntersectionObserver(
(entries, observer) => {
let [entry] = entries;
if (!entry.isIntersecting) return;

let speed = 1000;
counters.forEach((counter, index) => {
function UpdateCounter() {
const targetNumber = +counter.dataset.target;
const initialNumber = +counter.innerText;
const incPerCount = targetNumber / speed;
if (initialNumber < targetNumber) {
counter.innerText = Math.ceil(initialNumber + incPerCount);
setTimeout(UpdateCounter, 50);
}
}

UpdateCounter();

if (counter.parentElement.style.animation) {
counter.parentElement.style.animation = '';
} else {
counter.parentElement.style.animation = `slide-up 0.3s ease forwards ${
index / counters.length + 0.5
}s`;
}

});
observer.unobserve(section_counter);
},
{
root: null,
threshold: window.innerWidth > 768 ? 0.4 : 0.3,
}
);

CounterObserver.observe(section_counter);
    <section id="section_counter">
<div class="container">
<h1 class="section-heading">For every set, you purchase of our sustainable wear, you are helping the world save: </h1>
</div>
<div class="container">
<div class="counter-grid">

<div class="counter-item">
<h1 class="counter" data-target="15">0</h1>
<h2 class="counter-heading">Plastic bottles</h2>
<div class="counter-text">Which takes up to 150 years to decompose</div>
</div>
<div class="counter-item">
<h1 class="counter" data-target="22">0</h1>
<h2 class="counter-heading">KW of energy</h2>
<div class="counter-text">Equivalent to 1-day household consumption</div>
</div>
<div class="counter-item">
<h1 class="counter" data-target="5900">0</h1>
<h2 class="counter-heading">Liters of water</h2>
<div class="counter-text">Equivalent to the daily consumption of 16 persons (Mexico average)</div>

</div>
<div class="counter-item">
<h1 class="counter" data-target="9000">0</h1>
<h2 class="counter-heading">Grams of Co2 emissions</h2>
<div class="counter-text">Equivalent to a 90 km car consumption</div>

</div>
</div>
</div>
</section>

最佳答案

使用附加属性 尝试此解决方案数据计数 .逻辑很简单:我们将十进制值的计算存储在 中。数据计数里面只放一个整数,去掉数据计数在最后。

let section_counter = document.querySelector('#section_counter');
let counters = document.querySelectorAll('.counter-item .counter');

// Scroll Animation

let CounterObserver = new IntersectionObserver(
(entries, observer) => {
let [entry] = entries;
if (!entry.isIntersecting) return;

let speed = 1000;
counters.forEach((counter, index) => {
const targetNumber = +counter.dataset.target;
// Find the unit for one counter step
const count = targetNumber / speed;
// Set data attribute for accurate calculation with decimal
counter.setAttribute('data-count', 0);

function UpdateCounter() {
const initialNumber = +counter.innerText;
// Get decimal number to calculate
const countNumber = +counter.dataset.count;
// Getting an integer value with Math.floor()
const integer = Math.floor(countNumber);
if (initialNumber < targetNumber) {
// Set decimal number / toFixed() with speed length, to increase accuracy
counter.setAttribute('data-count', (countNumber + count).toFixed(`${speed}`.length));
// Set integer number
counter.innerText = integer;
setTimeout(UpdateCounter, 50);
} else {
// remove additional data attribute
counter.removeAttribute('data-count');
}
}

UpdateCounter();

if (counter.parentElement.style.animation) {
counter.parentElement.style.animation = '';
} else {
counter.parentElement.style.animation = `slide-up 0.3s ease forwards ${
index / counters.length + 0.5
}s`;
}
});
observer.unobserve(section_counter);
}, {
root: null,
threshold: window.innerWidth > 768 ? 0.4 : 0.3,
}
);

CounterObserver.observe(section_counter);
<section id="section_counter">
<div class="container">
<h1 class="section-heading">
For every set, you purchase of our sustainable wear, you are helping the world save:
</h1>
</div>
<div class="container">
<div class="counter-grid">
<div class="counter-item">
<h1 class="counter" data-target="15">0</h1>
<h2 class="counter-heading">Plastic bottles</h2>
<div class="counter-text">Which takes up to 150 years to decompose</div>
</div>
<div class="counter-item">
<h1 class="counter" data-target="22">0</h1>
<h2 class="counter-heading">KW of energy</h2>
<div class="counter-text">Equivalent to 1-day household consumption</div>
</div>
<div class="counter-item">
<h1 class="counter" data-target="5900">0</h1>
<h2 class="counter-heading">Liters of water</h2>
<div class="counter-text">
Equivalent to the daily consumption of 16 persons (Mexico average)
</div>
</div>
<div class="counter-item">
<h1 class="counter" data-target="9000">0</h1>
<h2 class="counter-heading">Grams of Co2 emissions</h2>
<div class="counter-text">Equivalent to a 90 km car consumption</div>
</div>
</div>
</div>
</section>

关于用于大小数字的 Javascript 计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72648427/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com