【微信小程序】-手势左滑功能

本文介绍了如何在微信小程序中实现手势左滑功能,详细讲解了涉及的wxss、wxml和js文件的配置与交互,帮助开发者理解并实现这一常见交互操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.wxss

/* pages/leftSwiperDel/index.wxss */
view{
  box-sizing: border-box;
}
.item-box{
  width: 700rpx;
  margin: 0 auto;
  padding:40rpx 0;
}
.items{
  width: 100%;
}
.item{
  position: relative;
  border-top: 2rpx solid #eee;
  height: 120rpx;
  line-height: 120rpx;
  overflow: hidden;
}
.item:last-child{
  border-bottom: 2rpx solid #eee;
}
.inner{
  position: absolute;
  top:0;
}
.inner.txt{
  background-color: #fff;
  width: 100%;
  z-index: 5;
  padding:0 10rpx;
  transition: left 0.2s ease-in-out;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.inner.returnBook{
  background-color: #aaa;
  width: 120rpx;
  text-align: center;
  z-index: 4;
  right: 120rpx;
  color: #fff
}
.inner.question{
  background-color: blue;
  width: 120rpx;
  text-align: center;
  z-index: 4;
  right: 0;
  color: #fff
}
.item-icon{
  width: 64rpx;
  vertical-align: middle;
  margin-right: 16rpx
}
.thumb{
  width: 200px;
  height: 200px;
  -webkit-overflow-scrolling: touch;
  overflow: scroll;
}

**

2.wxml

**

<!--pages/leftSlip/leftSlip.wxml-->
<view class="item-box">
  <view class="items">
    <view wx:for="{{list}}"  wx:key="{{index}}" bindtouchstart="touchS" bindtouchmove="touchM"  class="item" bindtouchmove="touchE">
      <view   data-index="{{index}}" style="{{item.txtStyle}}" class="inner txt">
      <image class="item-icon" mode="widthFix" src="{{item.icon}}"></image>{{item.txt}}</view>
      <view data-index="{{index}}" bindtap = "delItem"  >
          <view class="inner returnBook">还书</view>
          <view class="inner question">异常</view>
      </view>
    </view>
  </view>
</view>

3.js

// pages/leftSwiperDel/index.js
Page({
  data:{
    statusBtnWidth:'240'//删除按钮宽度单位(rpx)
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.initEleWidth();
    this.tempData();
  },

  touchS:function(e){
    if(e.touches.length==1){
      this.setData({
        //设置触摸起始点水平方向位置
        startX:e.touches[0].clientX
      });
    }
  },
  touchE:function(e){
    // console.log('e-------------',e);
    if(e.changedTouches.length==1){
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束,手指移动的距离
      var disX = this.data.startX - endX;
      var statusBtnWidth = this.data.statusBtnWidth;
      console.log('disX',disX)
      //如果距离小于删除按钮的1/2,不显示删除按钮
      var txtStyle = disX > statusBtnWidth/2 ? "left:-"+statusBtnWidth+"px":"left:0px";
      //获取手指触摸的是哪一项
      var index = e.target.dataset.index;
      var list = this.data.list;
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        list:list
      });
    }
  },
  //获取元素自适应后的实际宽度
  getEleWidth:function(w){
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750/2)/(w/2);//以宽度750px设计稿做宽度的自适应
      // console.log(scale);
      real = Math.floor(res/scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth:function(){
    var statusBtnWidth = this.getEleWidth(this.data.statusBtnWidth);
    // console.log('statusBtnWidth---',statusBtnWidth)
    this.setData({
      statusBtnWidth:statusBtnWidth
    });
  },
  //点击删除按钮事件
  delItem:function(e){
    //获取列表中要删除项的下标
    console.log('--------------')
    // var index = e.target.dataset.index;
    // var list = this.data.list;
    // //移除列表中下标为index的项
    // list.splice(index,1);
    // //更新列表的状态
    // this.setData({
      //   list:list
      // });
    },
    //测试临时数据
    tempData:function(){
      var list = [
        {
          txtStyle:"",
          icon:"/images/icon0.png",
          txt:"向左滑动可以删除"
        },
        {
          txtStyle:"",
          icon:"/images/icon6.png",
          txt:"微信小程序|联盟(wxapp-union.com)"
        },
        {
          txtStyle:"",
          icon:"/images/icon1.png",
          txt:"圣诞老人是爸爸,顺着烟囱往下爬,礼物塞满圣诞袜,平安糖果一大把"
        },
        {
          txtStyle:"",
          icon:"/images/icon2.png",
          txt:"圣诞到来,元旦还会远吗?在圣诞这个日子里"
        },
        {
          txtStyle:"",
          icon:"/images/icon3.png",
          txt:"圣诞节(Christmas或Cristo Messa ),译名为“基督弥撒”。"
        },
        {
          txtStyle:"",
          icon:"/images/icon4.png",
          txt:"一年一度的圣诞节即将到来,姑娘们也纷纷开始跑趴了吧!"
        },
      ];
      this.setData({
        list:list
      });
    }
    // touchM:function(e){
    //   if(e.touches.length==1){
    //     //手指移动时水平方向位置
    //     var moveX = e.touches[0].clientX;
    //     //手指起始点位置与移动期间的差值
    //     var disX = this.data.startX - moveX;
    //     var statusBtnWidth = this.data.statusBtnWidth;
    //     var txtStyle = "";
    //     if(disX == 0 || disX < 0){//如果移动距离小于等于0,文本层位置不变
    //       txtStyle = "left:100px";
    //     }else if(disX > 0 ){//移动距离大于0,文本层left值等于手指移动距离
    //       txtStyle = "left:-"+disX+"px";
    //       if(disX>=statusBtnWidth){
    //         //控制手指移动距离最大值为删除按钮的宽度
    //         txtStyle = "left:-"+statusBtnWidth+"px";
    //       }
    //     }
    //     //获取手指触摸的是哪一项
    //     var index = e.target.dataset.index;
    //     var list = this.data.list;
    //     list[index].txtStyle = txtStyle; 
    //     //更新列表的状态
    //     this.setData({
    //       list:list
    //     });
    //   }
    // },
  })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值