Controlling Webkit Keyframes with Javascript Variables
This article addresses a query on using JavaScript variables to control values within Webkit keyframes. While direct insertion of variables into CSS is not feasible, there are alternative methods to achieve this.
In the provided scenario, random numbers are generated and intended to be applied as rotation values within the keyframes. However, simply placing the variables in place of rotation values will not work in CSS.
Instead, the solution lies in creating CSS rules programmatically using JavaScript and inserting them into the CSS Object Model (CSSOM). One approach is to overwrite an existing animation with new keyframes based on the generated variables.
A JSFiddle example demonstrates this technique. It initially runs a -10 to 10 degree rotation animation, but allows the user to generate random rotation values between -360 to 360 degrees upon clicking a button.
This example leverages Simon Hausmann's earlier experiments with overwriting keyframes seamlessly in modern browsers like Chrome and Safari. However, the random number generation function is borrowed from Admixweb.
The above is the detailed content of How Can I Control WebKit Keyframes with JavaScript Variables?. For more information, please follow other related articles on the PHP Chinese website!