Is it possible to dynamically assign a CSS class to an element when the value of a range slider is changed? For example, consider the following slider:
<input id="slider" type="range" min="10" max="50">
I have a JavaScript function that retrieves the slider value, divides it by 10, and rounds it up:
let sliderValue = Number(slider.value);
sliderValue = Math.ceil(sliderValue / 10);
//output:
3
2
1
2
3
4
5 etc. as the slider moves.
I am aiming to add a class named active
to an element on the page in order to fade it in whenever the slider value changes. Given that this process occurs 10 times per interval, is there a way for JavaScript to detect when the slider value changes and then apply the designated class?
The current application of the "active" class results in stuttering due to it being assigned 10 times per interval. To visualize the issue, here is a link to a CodePen: CodePen