I’m not sure how to make this work
let progressBar = document.querySelector(".circular-progress");
let valueContainer = document.querySelector(".value-container");
let progressValue = 0;
let progressEndValue = 65;
let speed = 50;
let progress = setInterval(() => {
progressValue++;
valueContainer.textContent = `${progressValue}%`;
progressBar.style.background = `conic-gradient(
#FBDA9D ${progressValue * 3.6}deg,
#683279 ${progressValue * 3.6}deg
)`;
if (progressValue == progressEndValue) {
clearInterval(progress);
}
}, speed);
Updated to be a bit cleaner – this code works on page load, I would like to know how to activate it when it comes to view.
2
Answers
I think it says
addEventListener
requires a js method, not class selector. You can learn more on MDN Web Docs or w3SchoolsThe usage of
addEventListener
is this:I don’t understand exactly what you are trying to do by writing
but you should do it with this syntax that written in MDN Web Docs:
(And listener means a function)
You could use the Intersection Observer API. This is a build in Browser API which can determine if a element is in the viewport / visible.
In your case your code could look like this:
Also you’ll need to update your progress function. The way you define it, the interval starts as soon as the script gets executed. As soon as setInterval() gets called the interval starts.
Just call it in the observer callback function as seen above.
Also this line wont work:
progress
will get a numeric id assigned by thesetInterval
function, so appending the id to the elements classlist will not work as intented, because it is not a valid class. You are using the element provided bydocument.querySelector
, so you are already applying the styles to the object in the DOM. (alsoelement
is not defined – at least in the snippet you provided)