I am making a simple animation with JavaScript. I used Animation class from Web Animation API. I just want to create few more instance of that class and want to animate different element with different value and replace transform value of translateX. I have setup my constructor I just want to dynamically pass the value to translateX;
Here is my full project
JavaScript File
// https://codepen.io/rachelnabors/pen/eJyWzm/?editors=0010
const car = document.getElementById("car");
// CAR ANIMATION
class CarAnimate {
constructor(tFormXStart , tFormYStart, tFormXStop, tFormYStop, startOpacity, stopOpacity) {
this.tFormXStart = tFormXStart;
this.tFormYStart = tFormYStart;
this.tFormXStop = tFormXStop;
this.tFormYStop = tFormYStop;
this.startOpacity = startOpacity;
this.stopOpacity = stopOpacity;
}
carRightKeyframes = new KeyframeEffect(
car,
[
{
// WANT TO SET THE VALUE OF TRANSLATE X DYNAMICALLY
//HELP IF YOU CAN
transform:'translateX('+this.tFormXStart+'%)', // HERE IT SHOWING VALUE IS UNDEFINED
},
{
transform: 'translateX('+this.tFormXStop+'%)',
}
], {
duration: 3000,
fill: 'forwards',
iterations: Infinity
},
);
carRightAnimation = new Animation(this.carRightKeyframes, document.timeline);
start() {
this.carRightAnimation.play();
}
}
//FROM HERE I WANT TO SET THE FIRST AND THIRD PARAMETERS TO BE WORK
const carAnimate =new CarAnimate(0, 0, 100, 0,0, 1);
carAnimate.start();
I had to put the declarations into the click listener on my project, here is the code on codepen.