如何通过函数操作 css 值。
一旦单击按钮并将转换添加到.boxdiv。
这是JS代码,需要转换成React
Javascript
var x = 1024; // min value
var y = 9999; // max value
var deg = Math.floor(Math.random() * (x - y)) + y;
document.getElementById("box").style.transform = "rotate(" + deg + "deg)";
反应 16
myfunction=()=>{
var x = 1024; // min value
var y = 9999; // max value
let deg = Math.floor(Math.random() * (x - y)) + y;
let tranf = "rotate(" + deg + "deg)";
var ani = {
transform: tranf,
};
}
<div className="box" style={this.ani}>
<button className="spin" onClick={this.myfunction}>
Spin
</button>
CSS
.box {
width: 100%;
height: 100%;
position: relative;
border-radius: 50%;
overflow: hidden;
border: 10px solid #fff;
transition: all ease 5s;
}
参考 将其构建到 React 中。 https://www.youtube.com/watch?v=Gcz5RM-imJ8