[英]create “TouchableOpacity” for ReactJS without using react-native-web
I'd like to create a wrapper component that will behave like react- native
s <TouchableOpacity/>
without using react-native-web
. 我想创建一个包装器组件,其行为类似于
react- native
的<TouchableOpacity/>
react- native
<TouchableOpacity/>
而无需使用react-native-web
。
I'm thinking something that manages state and using inline styles that transitions from .5
to 1
for opacity with a setInterval
or should it be a css transition. 我正在考虑管理状态并使用内联样式(使用
setInterval
从.5
转换为1
的不透明性),或者应该是css转换。
Is there anything already out there? 已经有东西了吗?
What's the best approach for this? 最好的方法是什么?
You can achieve this affect with css transitions and using state to track when the user is clicking the button: 您可以通过CSS过渡和使用状态来跟踪用户单击按钮的时间来实现此效果:
class App extends React.Component { state = { touched: false } toggleTouched = () => { this.setState( prevState => ({ touched: !prevState.touched })); } handleMouseUp = () => { // Handle smooth animation when clicking without holding setTimeout( () => { this.setState({ touched: false }); }, 150); } render () { const { touched } = this.state; const className = touched ? 'btn touched' : 'btn'; return ( <button className={className} onMouseDown={this.toggleTouched} onMouseUp={this.handleMouseUp} > Touch Here </button> ) } } ReactDOM.render(<App/>, document.getElementById('root'));
.btn { background: #ededed; padding: 10px 15px; border: none; outline: none; cursor: pointer; font-size: 15px; opacity: 1; transition: opacity 300ms ease; } .touched { opacity: 0.5; }
<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> <div id="root"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.