有人可以告诉我如何在机车滚动中查看元素时触发功能,我是菜鸟,请详细说明,提前谢谢
const counterFunc = () => {
counters.forEach((counter) => {
const updateCount = () => {
const target = +counter.getAttribute("data-target");
const count = +counter.innerText;
// Lower inc to slow and higher to slow
const inc = target / speed;
// console.log(inc);
// console.log(count);
// Check if target is reached
if (count < target) {
// Add inc to count and output in counter
counter.innerText = count + inc;
// Call function every ms
setTimeout(updateCount, 1);
} else {
counter.innerText = target;
}
};
updateCount();
});
};
//HTML
<div id="counter-SUBSCRIBERS">
<h1 class="counter" data-scroll data-target="60000">0</h1>
<h5>Subscribers</h5>
</div>
<div id="counter-VIDEOS">
<h1 class="counter" data-target="15000">0</h1>
<h5>Comments</h5>
</div>
<div id="counter-ENGAGEMENT">
<h1 class="counter" data-target="9000">0</h1>
<h5>Share</h5>