兼容性
经测试,兼容 ie9+ 主流浏览器
手机单击触发事件
Chrome
onTouchStart
-> onTouchEnd
-> onMouseDown
-> onMouseUp
Firefox
onTouchStart
-> onTouchEnd
-> onMouseDown
-> onMouseMove
-> onMouseUp
手机触摸移动触发事件
Chrome
onTouchStart
-> onTouchMove
-> onTouchEnd
Firefox
onTouchStart
-> onTouchMove
-> onTouchEnd
-> onMouseDown
-> onMouseMove
-> onMouseUp
获取坐标方式
事件 |
方法 |
onMouseDown |
const {pageX, pageY} = e; |
onMouseMove |
const {pageX, pageY} = e; |
onMouseUp |
const {pageX, pageY} = e; |
onTouchStart |
const {pageX,pageY} = e.touches[0]; |
onTouchMove |
const {pageX, pageY} = e.touches[0]; |
onTouchEnd |
const {clientX, clientY} = e.changedTouches[0]; |
示例
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| import React,{ Component } from 'react'; import styles from './App.css';
class App extends Component { constructor(props) { super(props); this.checked = false;
this.handleMouseDown = this.handleMouseDown.bind(this); this.handleMouseMove = this.handleMouseMove.bind(this); this.handleMouseUp = this.handleMouseUp.bind(this);
this.handleTouchStart = this.handleTouchStart.bind(this); this.handleTouchMove = this.handleTouchMove.bind(this); this.handleTouchEnd = this.handleTouchEnd.bind(this); }
handleTouchStart(e){ const {pageX,pageY} = e.touches[0]; console.log("handleTouchStart", pageX, pageY); }
handleTouchMove(e){ const {pageX, pageY} = e.touches[0]; console.log("handleTouchMove", pageX, pageY); }
handleTouchEnd(e){ const {clientX, clientY} = e.changedTouches[0]; console.log("handleTouchEnd", clientX, clientY); }
handleMouseDown(e){ this.checked = true; console.log("handleMouseDown", e.pageX, e.pageY); }
handleMouseMove(e){ if(this.checked) console.log("handleMouseMove", e.pageX, e.pageY); }
handleMouseUp(e){ this.checked = false; console.log("handleMouseUp", e.pageX, e.pageY); }
render(){ return ( <div className={styles.app}> <h2>Hello, Nice</h2> <div className={styles.touchBox} onMouseDown={this.handleMouseDown} onMouseMove={this.handleMouseMove} onMouseUp={this.handleMouseUp}
onTouchStart={this.handleTouchStart} onTouchMove={this.handleTouchMove} onTouchEnd={this.handleTouchEnd} /> </div> ) } }
export default App;
|