前言
js原生有dbClick
事件,但是几乎不支持移动端
所以在移动端上要实现双击事件要另谋出路
在移动端单击会依次触发touchstart->touchmove->touchend->click
事件
可以通过touchstart
来模拟双击
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| state = { lastClickTime: 0 }
componentDidMount() { const container = document.getElementsByClassName("dbContainer"); container.addEventListener('touchstart', (e) => { if (e.touches.length === 1) { this.handleTouchStart(e); } }) }
componentWillUnmount() { const container = document.getElementsByClassName("dbContainer"); container.removeEventListener('touchstart'); }
handleTouchStart(e) { e.preventDefault(); e.stopPropagation(); if (currentTime - this.state.lastClickTime < 300) { } this.setState({lastClickTime: currentTime}); }
render() { return (<div className="dbContainer"></div>) }
|
一个简单的移动端双击事件就完成啦!