我正嘗試使用React創建滑動事件。我不想使用任何外部組件或jQuery。反應js中的滑動效果
的CSS是這樣的:
.outter{
position:relative;
width: 100%;
height: 150px;
background-color: blue;
}
.inner{
position: absolute;
width: 1000%;
left: 50px;
}
.child{
float: left;
margin-right: 15px;
}
在反應成分,我試圖做一些事情,如:
class Test extends React.Component {
constructor(props){
super(props);
this.state = {
left: 0
}
}
handleSwipe(){
this.setState({left: -350})
}
render(){
return(
<div className="outter">
<div className="inner" style={{left: this.state.left}} onSwipe={this.handleSwipe.bind(this)}>
<div className="child"><img src="http://placehold.it/350x150" /></div>
<div className="child"><img src="http://placehold.it/350x150" /></div>
<div className="child"><img src="http://placehold.it/350x150" /></div>
<div className="child"><img src="http://placehold.it/350x150" /></div>
</div>
</div>
)
}
}
React.render(<Test />, document.getElementById('container'));
我如何能識別刷卡事件?
如果我在我的例子中,而不是onSwipe
添加onClick
它的工作原理,但我怎樣才能使滑動效果?
這裏是jsfiddle。
如果你真的想自己做,那麼你需要傾聽和處理鼠標按下+鼠標移動+鼠標事件。 – dfsq