【发布时间】:2019-11-02 01:45:06
【问题描述】:
我有一个 React 应用程序,它在 Leaflet 地图上有一个图标,一个 0 到 359 之间的值表示它的当前方向。
我想transform: rotate(Xdeg) 图标并使用transition 让它为这个旋转设置动画。
我的问题是当值通过0(或360)时,转换发生在相反的方向。
是否可以将带有transform: rotate(340deg) 的框更改为transform: rotate(10deg) 并使其顺时针旋转?
示例代码笔here
const App = () => {
const [rotation, setRotation] = React.useState(340);
return (
<div id="wrapper">
<div>
<div className="box-wrapper">
{/* Here is where I'm setting the rotation CSS transform dynamically */}
<div className="box" style={{ transform: `rotate(${rotation}deg)` }}>
<span>{ `Rotation: ${rotation}deg` }</span>
</div>
</div>
</div>
<div>
<button onClick={() => setRotation((rotation + 10) % 360)}>+10 deg</button>
<button onClick={() => setRotation(((360 + (rotation - 10)) % 360))}>-10 deg</button>
<button onClick={() => setRotation(340)}>340 deg</button>
<button onClick={() => setRotation(20)}>20 deg</button>
</div>
</div>
)
}
【问题讨论】:
标签: javascript css reactjs animation css-transforms