触屏事件的两种写法

https://developer.mozilla.org/en-US/docs/Web/API/Touch_events

第一种写法

import React,{Component} extends 'react'

export default class App from Component {
  style={
      'width':'200px',
      'height':'200px',
      'background':'#bada55'  
  }
  
  handleTouchStart = (e)=>{
        console.log('touchstart',e)
  }
  handleTouchEnd = (e)=>{
        console.log('touchEnd',e)
  }
  handleTouchMove = (e)=>{
        console.log('touchMove',e)
  }

  render(){
    return(
        <div style={this.style}
            onTouchStart={this.handleTouchStart}
            onTouchEnd={this.handleTouchEnd}
            onTouchMove={this.handleTouchMove}
        >
              
        </div>
    )
  }
}

第二种写法:原生JS

import React,{Component} extends 'react'

export default class App from Component {
  style={
      'width':'200px',
      'height':'200px',
      'background':'#bada55'  
  }
  componentDidMount(){
        let obj = document.getElementById('stage')
        obj.addEventListener('touchstart',()=>{
            console.log('touchstart')
            obj.addEventListener('touchmove',()=>{
                 console.log('touchmove') 
            })
        })
        obj.addEventListener('touchend',()=>{
            console.log('touchend') 
        })
  }
  handleTouchStart = (e)=>{
        console.log('touchstart',e)
  }
  handleTouchEnd = (e)=>{
        console.log('touchEnd',e)
  }
  handleTouchMove = (e)=>{
        console.log('touchMove',e)
  }

  render(){
    return(
        <div style={this.style} id='stage'>
              
        </div>
    )
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前端知识体系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋风喵阅读 12,566评论 7 163
  • 房子不外乎大,豪华,温馨舒适即可。
    水月兰阅读 153评论 0 0
  • 在动漫片《千与千寻》里,有个无脸怪在一座红色的桥上首次遇到小女主千寻后,便开始了默然关注。 千寻在工作时,他就立在...
    龙少之说阅读 542评论 2 3
  • 一直想用文字记录一个长长的故事 因为 我怕时光的棱角会改变它的样子 却不知道该用怎样的心情去起笔 怎样的语调去结局
    pan朵阅读 217评论 0 2