react滑动组件

三种事件:onTouchStart,onTouchMove, onTouchEnd。

handleStart = (e) => {
        e.preventDefault();     
        this.setState({  
            startX : e.targetTouches[0].clientX,
            startY : e.targetTouches[0].clientY,
            x:0,
            y:0,
        });
    }
    handleTouchMove = (e) => {
        const { startX, startY, width, height } = this.state;//取得初始坐标和屏幕可视宽高        
        this.setState({
            ///都可以来设置实时变化的值,不用用到changedTouches;
            x: e.touches[0].clientX - startX, //当前触摸点-初始坐标取得实时变化值
            y: e.touches[0].clientY - startY,
        });
        
    }
handleTouchEnd = (e) => {
  let xCur = this.state.x;
  let width = window.screen.availWidth;   
  let curIndex = this.state.index;        
    if(xCur > width/5 && curIndex !== 0){
      curIndex -- ;
      $(".sliders").css("left",-curIndex*7.5+"rem")         
      this.setState({
        index: curIndex
      })
      console.log(curIndex)  
    }else if(xCur < -width/5 && curIndex !== 2){
      curIndex ++ ;
      $(".sliders").css("left",-curIndex*7.5+"rem")          
      this.setState({
        index: curIndex
      })
      console.log(curIndex)
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容