美文网首页
触屏事件的两种写法

触屏事件的两种写法

作者: 6659a0f02826 | 来源:发表于2017-07-21 11:41 被阅读48次

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>
    )
  }
}

相关文章

网友评论

      本文标题:触屏事件的两种写法

      本文链接:https://www.haomeiwen.com/subject/mfoskxtx.html