我有一个表示按钮的react组件,该按钮根据某些道具更改内容和颜色.如果表单组件包含此按钮并提交,则该按钮将更改其内容和颜色.这很好.
现在,我想要一个动画(CSS过渡),用于在按钮内容更改时进行更改.不能为不同的状态设置显式宽度,因为该应用程序是多语言的.使用最大宽度时,动画仅在按钮扩展时起作用,而在按钮减小时不起作用.
这是一个带有按钮组件的代码段,它每秒都会更改状态.就像在实际组件中一样,此人使用引导程序btn类:
class Button extends React.Component { constructor(props) { super(props); this.state = {status: 0}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } tick() { let status = this.state.status; status ; if(status >= 2) { status = 0; } this.setState({ status: status }); } render() { const btnClass = this.state.status ? 'primary' : 'secondary'; return ( <button className={`btn btn-${btnClass}`} type='button'> {this.state.status ? this.props.large : this.props.short} </button> ); }}// Render itReactDOM.render( <Button large='This is a long button you can click on!' short='short'/>, document.getElementById('react'));
.btn { -webkit-transition: width 1s; /* Safari */ transition: max-width 1s; width: auto;}.btn-primary { max-width: 300px;}.btn-secondary { max-width: 50px;}
<div id="react"></div><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
如何获得宽度变化的动画(当按钮再次变小时)?
请考虑一个答案,并不仅限于CSS.我想到了react组件计算它的宽度,然后将其设置在componentWillMount函数中,但是那也不起作用…
解决方法:
我有解决办法.将最小宽度添加到您的CSS类并按如下所示进行转换.
在向上动画.在下降的过程中进行动画处理.
class Button extends React.Component { constructor(props) { super(props); this.state = {status: 0}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } tick() { let status = this.state.status; status ; if(status >= 2) { status = 0; } this.setState({ status: status }); } render() { const btnClass = this.state.status ? 'primary' : 'secondary'; return ( <button className={`btn btn-${btnClass}`} type='button'> {this.state.status ? this.props.large : this.props.short} </button> ); }}// Render itReactDOM.render( <Button large='This is a long button you can click on!' short='short'/>, document.getElementById('react'));
.btn { -webkit-transition: width 1s; /* Safari */ transition: max-width 1s, min-width 1s; width: auto;}.btn-primary { max-width: 330px; min-width: 230px;}.btn-secondary { max-width: 60px; min-width: 40px; text-align: left;}
<div id="react"></div><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
来源:https://www.icode9.com/content-1-519151.html
联系客服