Created
July 14, 2015 18:20
-
-
Save chenglou/870bba30490d06c3c4a4 to your computer and use it in GitHub Desktop.
react-motion demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {Spring} from '../src/Spring'; | |
const childVal = 275; | |
const Demo = React.createClass({ | |
getInitialState() { | |
return {open: false}; | |
}, | |
handleMouseDown() { | |
this.setState({open: !this.state.open}); | |
}, | |
getEndValue(currentValue) { | |
const parentVal = this.state.open ? 400 : 0; | |
if (currentValue == null) { | |
return { | |
parent: {val: parentVal}, | |
children: {val: [0, 0, 0]}, | |
}; | |
} | |
const {parent, children: {val: [c1, c2]}} = currentValue; | |
return { | |
parent: {val: parentVal}, | |
children: { | |
val: [ | |
// percentage of parent val | |
parent.val / 400 * childVal, | |
c1 - 100, | |
c2 - 100, | |
], | |
config: [120, 17], | |
}, | |
}; | |
}, | |
render() { | |
return ( | |
<div> | |
<button onMouseDown={this.handleMouseDown}>Toggle</button> | |
<Spring endValue={this.getEndValue}> | |
{({parent, children: {val: [c1, c2, c3]}}) => | |
// children is a callback which should accept the current value of | |
// `endValue` | |
<div className="demo0" style={{height: parent.val}}> | |
<div className="demo0-block" style={{ | |
WebkitTransform: `translate3d(0, ${c1}px, 0)`, | |
transform: `translate3d(0, ${c1}px, 0)`, | |
}} /> | |
<div className="demo0-block" style={{ | |
WebkitTransform: `translate3d(0, ${c2}px, 0)`, | |
transform: `translate3d(0, ${c2}px, 0)`, | |
}} /> | |
<div className="demo0-block" style={{ | |
WebkitTransform: `translate3d(0, ${c3}px, 0)`, | |
transform: `translate3d(0, ${c3}px, 0)`, | |
}} /> | |
</div> | |
} | |
</Spring> | |
</div> | |
); | |
}, | |
}); | |
export default Demo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Toggle</title> | |
<style> | |
.demo0 { | |
border-radius: 4px; | |
background-color: rgb(240, 240, 232); | |
position: relative; | |
margin: 5px 3px 10px; | |
width: 250px; | |
height: 500px; | |
overflow: hidden; | |
} | |
.demo0-block { | |
position: absolute; | |
left: 25px; | |
width: 200px; | |
margin: auto; | |
height: 50px; | |
border-radius: 4px; | |
background-color: rgb(130, 181, 198); | |
} | |
</style> | |
</head> | |
<body> | |
<div id="content"></div> | |
<script src="./all.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment