-
-
Save liuyanghejerry/b4b8f214b1273c4ca71f to your computer and use it in GitHub Desktop.
Component switch animation with React
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> | |
<head> | |
<script src="http://fb.me/react-with-addons-0.13.1.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<style id="jsbin-css"> | |
.parent { | |
position: relative; | |
} | |
.child { | |
position: absolute; | |
top: 0; | |
left: 0; | |
} | |
.child.a { | |
z-index: 2; | |
} | |
.child.b { | |
z-index: 1; | |
} | |
.sample-enter { | |
opacity: 0; | |
transition: opacity .5s ease-in; | |
} | |
.sample-enter.sample-enter-active { | |
opacity: 1; | |
} | |
.sample-leave { | |
opacity: 1; | |
transition: opacity .5s ease-in; | |
} | |
.sample-leave.sample-leave-active { | |
opacity: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup; | |
var Sample = React.createClass({displayName: 'Sample', | |
getInitialState: function() { | |
return { | |
time: (+new Date()) / 1000 | |
} | |
}, | |
componentDidMount: function() { | |
var self = this; | |
setInterval(function() { | |
self.setState({ | |
time: parseInt((+new Date()) / 1000, 10) | |
}); | |
}, 1000); | |
}, | |
renderElement: function() { | |
console.log(this.state.time); | |
if(this.state.time % 2 === 0) { | |
return (React.createElement("div", {className: "child a", key: "a"}, "aaaaaa")); | |
} else { | |
return (React.createElement("div", {className: "child b", key: "b"}, "bbbbbb")); | |
} | |
}, | |
render: function() { | |
return ( | |
React.createElement("div", null, | |
React.createElement(ReactCSSTransitionGroup, {transitionName: "sample", transitionAppear: false, className: "parent"}, | |
this.renderElement() | |
) | |
) | |
); | |
} | |
}); | |
React.render(React.createElement(Sample, null), document.body); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//fb.me/react-with-addons-0.13.1.js"><\/script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html></script> | |
<script id="jsbin-source-css" type="text/css">.parent { | |
position: relative; | |
} | |
.child { | |
position: absolute; | |
top: 0; | |
left: 0; | |
&.a { | |
z-index: 2; | |
} | |
&.b { | |
z-index: 1; | |
} | |
} | |
.sample-enter { | |
opacity: 0; | |
transition: opacity .5s ease-in; | |
} | |
.sample-enter.sample-enter-active { | |
opacity: 1; | |
} | |
.sample-leave { | |
opacity: 1; | |
transition: opacity .5s ease-in; | |
} | |
.sample-leave.sample-leave-active { | |
opacity: 0; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup; | |
var Sample = React.createClass({ | |
getInitialState: function() { | |
return { | |
time: (+new Date()) / 1000 | |
} | |
}, | |
componentDidMount: function() { | |
var self = this; | |
setInterval(function() { | |
self.setState({ | |
time: parseInt((+new Date()) / 1000, 10) | |
}); | |
}, 1000); | |
}, | |
renderElement: function() { | |
console.log(this.state.time); | |
if(this.state.time % 2 === 0) { | |
return (<div className="child a" key="a">aaaaaa</div>); | |
} else { | |
return (<div className="child b" key="b">bbbbbb</div>); | |
} | |
}, | |
render: function() { | |
return ( | |
<div> | |
<ReactCSSTransitionGroup transitionName="sample" transitionAppear={false} className="parent"> | |
{this.renderElement()} | |
</ReactCSSTransitionGroup> | |
</div> | |
); | |
} | |
}); | |
React.render(<Sample/>, document.body);</script></body> | |
</html> |
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
.parent { | |
position: relative; | |
} | |
.child { | |
position: absolute; | |
top: 0; | |
left: 0; | |
} | |
.child.a { | |
z-index: 2; | |
} | |
.child.b { | |
z-index: 1; | |
} | |
.sample-enter { | |
opacity: 0; | |
transition: opacity .5s ease-in; | |
} | |
.sample-enter.sample-enter-active { | |
opacity: 1; | |
} | |
.sample-leave { | |
opacity: 1; | |
transition: opacity .5s ease-in; | |
} | |
.sample-leave.sample-leave-active { | |
opacity: 0; | |
} |
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
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup; | |
var Sample = React.createClass({displayName: 'Sample', | |
getInitialState: function() { | |
return { | |
time: (+new Date()) / 1000 | |
} | |
}, | |
componentDidMount: function() { | |
var self = this; | |
setInterval(function() { | |
self.setState({ | |
time: parseInt((+new Date()) / 1000, 10) | |
}); | |
}, 1000); | |
}, | |
renderElement: function() { | |
console.log(this.state.time); | |
if(this.state.time % 2 === 0) { | |
return (React.createElement("div", {className: "child a", key: "a"}, "aaaaaa")); | |
} else { | |
return (React.createElement("div", {className: "child b", key: "b"}, "bbbbbb")); | |
} | |
}, | |
render: function() { | |
return ( | |
React.createElement("div", null, | |
React.createElement(ReactCSSTransitionGroup, {transitionName: "sample", transitionAppear: false, className: "parent"}, | |
this.renderElement() | |
) | |
) | |
); | |
} | |
}); | |
React.render(React.createElement(Sample, null), document.body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment