对于rotate
&scale
您可以使用标记的模板文字 ${ expresion }
。
我来给你展示
<Motion
defaultStyle={{ rotate: 0, scale: 1}}
style={{ rotate: spring(180), scale: spring(3)}}
>
{style =>
(
<div style={{
transform: `rotate( ${style.rotate}deg )`,
transform: `scale( ${style.scale}, ${style.scale} )`
}}> </div>
)
}
</Motion>
注意反引号的使用
对于交互式动画,React 非常擅长访问 DOM 并使用 SyntheticEvents 包括鼠标事件。
onClick onContextMenu onDoubleClick onDrag onDragEnd onDragEnter
onDragExit onDragLeave onDragOver onDragStart onDrop onMouseDown
onMouseEnter onMouseLeave onMouseMove onMouseOut onMouseOver onMouseUp
这是一个使用鼠标悬停的示例setState
import React, { Component } from 'react'
import { Motion, spring } from 'react-motion'
class App extends Component {
state = {
rotate: 0
}
enter(){
this.setState({rotate: 180})
}
leave(){
this.setState({rotate: 0})
}
render(){
return (
<Motion
defaultStyle={{ rotate: 0}}
style={{ rotate: spring(this.state.rotate)}}
>
{style =>
(
<div
style={{
transform: `rotate( ${style.rotate}deg )`,
}}
onMouseEnter={this.enter.bind(this)}
onMouseLeave={this.leave.bind(this)}
> </div>
)
}
</Motion>
)
}
}
export default App
现在这不会显示任何内容,因为div
. 为此,让我们声明styles
对象。
import
线下
const styles = {
div: {
height: 100,
width: 100,
backgroundColor: 'papayawhip'
}
}
那么你可以像这样使用它:
style={ Object.assign( {},
styles.div,
{
transform: `rotate( ${style.rotate}deg )`,
// more styles here...
})
}