function countUp(){ if (startNum <= endNum ){ $w(‘#text140’).text = startNum.toString(); startNum++; }}
The problem is that they start the count as soon as the page is loaded, and this element has been placed in the center of the webpage so by the time I scroll there, the count is already over.
Can’t it start its count when it enters the View Port ??
in the Editor create onViewportEnter event from properties panel on this element and add your code below that line so when this element enters the viewport it runs the animation.
I have a column strip at the end of the page (columnStrip6) with four animated numbers that count up.
I’ve tried creating an onViewportEnter event from properties panel and then inserting the Count Up codes below it, so that the animation only begins when columnStrip6 has entered the viewport. This doesn’t seem to work.
Could someone help us out? This is the code I’m using (and doesn’t work):
export function columnStrip6_viewportEnter(event, $w) {
let startNum = 0; let endNum = 200; const duration = 75; $w.onReady(function () { setInterval(()=> { countUp(); }, duration); }); function countUp(){ if (startNum <= endNum ){ $w(’#text19').text = startNum.toString(); startNum++; }} }
Hi,
Instead of using the onReady function, use the onViewportEnter (meaning, the setInterval function should be moved to the onViewEnter event, and not the onReady function.
How can I specify this animation only plays once per page refresh?
To be specific, I would like:
The animation to begin when ViewportEnter, but only play one time.
At the moment it restarts each time I scroll back up the page.
The code I’m using is below:
export function text45_viewportEnter(event) {
//Add your code for this event here: let startNum = 24999900; let endNum = 25000000; const duration = 50; // 1000 milliseconds
$w.onReady( function () {
setInterval(()=> {
countUp();
}, duration);
});
function countUp(){ if (startNum <= endNum ){
$w(‘#text45’).text = startNum.toLocaleString().toString();
startNum++;
}
}
}
The Counter class is a reusable function I can call, passing it a Wix element, taking the text out as the to value, and setting the from value as 0. We then call the run function which uses a timeout to loop the run function, but only if the from value is less than the to value.
in order to stop this being called every time the element enters the viewport, we need to store a record of which elements have been animated. we do this simply but storing the ID’s of the animated elements in an array, and searching the array prior to instantiating the Counter function to make sure the ID doesn’t already exist…
const counterManager = {
arr: [],
add(event) {
if (!this.arr.includes(event.target.id)) {
this.arr.push(event.target.id);
new Counter(event.target);
}
}
}
The only thing left is to add the viewport enter event to all the elements that we want to animate, I did this in the onReady function making use of the multi-element selector.