admin管理员组文章数量:1392007
In my React based application I'm trying to perform a simple css3 width transition like:
.foo {
transition: width 1s ease-in-out;
}
what I want to do is set a style inside an element in the react ponent which is "width: xx%" and the animate it from 0% to xx%. Since the element when rendered already has this property the animation is not working. I've looked into the "ReactCSSTransitionGroup" but did not e closer to a solution. I started messing around with setTimeOut to set the style attribute after the ponent was rendered but it felt really messy and "hackish". Could someone point me in the right direction?
In my React based application I'm trying to perform a simple css3 width transition like:
.foo {
transition: width 1s ease-in-out;
}
what I want to do is set a style inside an element in the react ponent which is "width: xx%" and the animate it from 0% to xx%. Since the element when rendered already has this property the animation is not working. I've looked into the "ReactCSSTransitionGroup" but did not e closer to a solution. I started messing around with setTimeOut to set the style attribute after the ponent was rendered but it felt really messy and "hackish". Could someone point me in the right direction?
Share Improve this question asked Mar 30, 2017 at 18:01 martenolofssonmartenolofsson 5311 gold badge5 silver badges21 bronze badges1 Answer
Reset to default 6If you are trying to animate the ponent after it has been rendered (from 0 to n%) you can do it by calling setState
in ponentDidMount
. As browsers are not rerendering stuff that has changed in the same animation frame but merge changes and render the end result, you'll need to wrap it in requestAnimationFrame
I explained it throughly in this blog post.
Code will look like this:
export default class AnimateMe extends Component {
state = {
width: 0
};
ponentDidMount() {
requestAnimationFrame(() => {
this.setState({ width: "75%" });
});
}
render() {
return (
<div style={{ width: this.state.width }}>
Animate my width
</div>
);
}
}
I made a working example: https://codesandbox.io/s/7z1j794oy1
Hope that helps!
本文标签: javascriptReact element width animationStack Overflow
版权声明:本文标题:javascript - React element width animation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744765161a2623995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论