微信小程序 左右滑动方法

在这里插入图片描述
实现小程序左右滑动操作

wxml,在当前view层滑动操作

<view bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" animation="{{ani}}"> 

data数据

 status:"0" //状态显示参数
 startX:0,
 endX:0,
 moveFlag:true,
 ani:"",
/**
   * 触摸开始事件
   * */ 
  touchStart(e:any) {
    this.data.startX = e.touches[0].pageX; // 获取触摸时的原点
    this.data.moveFlag = true;
  },
 
  /**
   * 触摸移动事件
   * */ 
  touchMove(e:any) {
    this.data.endX = e.touches[0].pageX; // 获取触摸时的原点
    if ( this.data.moveFlag) {
      if ( this.data.endX -  this.data.startX > 50) {
        console.log("move right");
        this.move2right();
        this.data.moveFlag = false;
      }
      if ( this.data.startX -  this.data.endX > 50) {
        console.log("move left");
        this.move2left();
        this.data.moveFlag = false;
      }
    }
  },
 
  /**
   * 触摸结束事件
   * */ 
  touchEnd () {
    this.data.moveFlag = true; // 回复滑动事件
  },

  /**
   * 右移,向左滑动操作
   * */
  move2left(){
    let status = Number(this.data.status); 
    if (status === 2 ) { //最右,不移动
      return
    }
    //....移动成功,执行方法
  },
  /**
   * 左移,向右滑动操作
   * */
  move2right(){
    let status = Number(this.data.status); 
    if (status === 0 ) {//最左,不移动
      return
    }
     //....移动成功,执行方法
  },
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值