微信小程序实现滑动操作代码

前言

本文使用动画组件wx.createAnimation来实现滑动操作:

1. 左滑动显示操作项区域;

2. 点击操作项触发相应方法;

3. 右滑动和点击行收起操作项;

4. 点击“删除”并确定则删除该条数据。

最终效果如下:

实现过程

1. 文件index.wxml和index.wxss代码如下,这一块比较简单,可自行查看,不做过多分析;

Tips:“详情”、“取号”和“删除”点击触发使用catchtap,阻止冒泡事件向上冒泡;

<view class="wrapper">
 <view class="container">
  <view class="list">
   <view class="line" bindtouchstart="touchstartX" bindtap="resetX" bindtouchmove="touchmoveX" bindtouchend="touchendX" animation="{{currentIndex === index ?animation : ''}}" data-index="{{index}}" wx:for="{{recordList}}" wx:key="id">
    <image class="icon-title" src="../../images/start_icon.png"></image>
    <view class="mes">
     <view class="title">{{item.title}}</view>
     <view class="date">预约时间:{{item.date}}</view>
     <view class="status">状态:<text>{{item.status}}</text></view>
    </view>
    <view class="operation">
     <view class="detail" catchtap="openDetail">详情</view>
     <view class="number" catchtap="getNumber">取号</view>
     <view class="delete" catchtap="deleteItem">删除</view>
    </view>
   </view>
  </view>
 </view>
</view>
.container .line {
 display: flex;
 padding: 20rpx 30rpx;
 border-bottom: 2rpx solid #ebebeb;
 position: relative;
 cursor: pointer;
}

.container .line .icon-title {
 margin-top: 28rpx;
 width: 30rpx;
 height: 30rpx;
}

.container .line .mes {
 flex: 1;
 margin-left: 10rpx;
}

.container .line .mes .date, .container .line .mes .status {
 color: #9d9d9d;
 font-size: 24rpx;
 margin-top: 4rpx;
}

.status text {
 color: #fba500;
}

.operation {
 position: absolute;
 top: 0;
 right: -300rpx;
 height: 152rpx;
 text-align: center;
 color: #fff;
 line-height: 152rpx;
 display: flex;
}

.operation view {
 width: 100rpx;
}

.operation .detail {
 background-color: #018efb;
}

.operation .number {
 background-color: #fba500;
}

.operation .delete {
 background-color: #cfcfcf;
}

2. 文件index.js存放所有功能的逻辑代码,下面主要分析几个重点方法:

1)方法touchmoveX用于手指触摸后移动时获取移动距离,并根据移动距离动画显示操作项相应区域,使移动有即时效果;

2)方法touchendX用于手指触摸动作结束时,如果移动距离达到100,则动画显示全部操作项区域;如果移动距离未达到100,则收起操作项区域;

3)方法deleteItem用于删除该条数据。

let movedistance = 0;
Page({
 data: {
  currentIndex: 0, // 列表操作项的index
  recordList: [{ // 列表数据
   id: 1,
   title: '业务办理01',
   date: '2020-04-21 10:30-12:00',
   status: '未取号'
  }, {
   id: 2,
   title: '业务办理02',
   date: '2020-04-21 10:30-12:00',
   status: '未取号'
  }, {
   id: 3,
   title: '业务办理03',
   date: '2020-04-21 10:30-12:00',
   status: '取号'
  }]
 },
 // 打开详情页
 openDetail() {
  console.log(this.data.currentIndex, '点击详情');
 },
 // 取号
 getNumber() {
  console.log(this.data.currentIndex, '点击取号');
 },
 // 删除数据
 deleteItem() {
  let that = this;
  let recordList = this.data.recordList;
  wx.showModal({
   title: '提示',
   content: '是否删除该条数据?',
   success(res) {
    if (res.confirm) {
     that.slideAnimation(0, 500);
     recordList.splice(that.data.currentIndex, 1);
     that.setData({
      recordList: recordList
     });
    } else if (res.cancel) {
     console.log('用户点击取消')
    }
   }
  });
 },
 // 手指触摸动作开始
 touchstartX(e) {
  this.setData({
   currentIndex: e.currentTarget.dataset.index
  });
  // 获取触摸X坐标
  this.recordX = e.touches[0].clientX;
 },
 // 点击操作
 resetX() {
  this.slideAnimation(0, 500);
 },
 // 手指触摸后移动
 touchmoveX(e) {
  let currentX = e.touches[0].clientX;
  movedistance = currentX - this.recordX; // 获取移动距离
  this.slideAnimation(movedistance, 500);
 },
 // 手指触摸动作结束
 touchendX() {
  let recordX;
  if (movedistance <= -100) { // 移动达到距离就动画显示全部操作项
   recordX = -300;
  } else if (movedistance >= -100) { // 移动未达到距离即还原
   recordX = 0;
  }
  this.slideAnimation(recordX, 500);
 },
 // 滑动动画
 slideAnimation(recordX, time) {
  let animation = wx.createAnimation({
   duration: time,
   timingFunction: 'ease'
  });
  animation.translate(recordX + 'rpx', 0).step()
  this.setData({
   animation: animation.export()
  })
 },
 onLoad: function(options) {
  wx.setNavigationBarTitle({
   title: '银行业务',
  });
  movedistance = 0; // 解决切换到其它页面再返回该页面动画失效的问题
 }
})

到此这篇关于微信小程序实现滑动操作代码的文章就介绍到这了,更多相关微信小程序滑动内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 微信小程序实现左侧滑动导航栏

    本文实例为大家分享了微信小程序实现左侧滑动导航栏的具体代码,供大家参考,具体内容如下 左侧滑动导航栏如图 wxml <!-- 左侧滚动栏 --> <view class='under_line'></view> <view style='float: left' class='left'> <scroll-view scroll-y scroll-with-animation scroll-left="{{scrollLength}}&quo

  • 微信小程序实现卡片层叠滑动效果

    本文实例为大家分享了微信小程序之卡片层叠滑动效果的具体代码,供大家参考,具体内容如下 代码: js: // index/gun/jsSwiper2/jsSwiper2.js Page({ /** * 页面的初始数据 */ data: { startX: 0, endX: 0, iCenter: 3, datas: [{ id: 1, zIndex: 2, opacity: 0.2, left: 40, iamge: "../../images/1.jpg", animation: nu

  • 微信小程序 页面滑动事件的实例详解

    微信小程序--页面滑动事件 wxml: <view id="id" class = "ball" bindtap = "handletap" bindtouchstart = "handletouchtart" bindtouchmove="handletouchmove" bindtouchend="handletouchend" style = "width : 10

  • 微信小程序图片横向左右滑动案例

    本文实例为大家分享了微信小程序图片左右滑动的具体代码,供大家参考,具体内容如下 图片左右滑动效果图: wxml代码: <scroll-view scroll-x="true"> <view class="uploadWrap" scroll-x="true"> <view class="upload_Item"> <image class="upload_Item_img&q

  • 微信小程序左滑动显示菜单功能的实现

    效果图如下所示: view <view class="page"> <!--下层左侧导航--> <view class="page-bottom"> <view class="page-content"> <view class="userinfo"> <view class="userImg"> <image src='/ima

  • 微信小程序左右滑动的实现代码

    左滑 右滑 你不再是一个人 无论你是一个程序猿还是一个程序媛,每天干的事除了coding还是coding,代码不能解决的问题(什么问题自己心里还没点abcd数嘛),探探能帮你解决.最近网上特流行一款交友软件叫探探(据说是yp软件).作为探探曾经的一名从来只浏览图片但是没有yue过的资深玩家同时又是一位热爱前端的妹子,我决定要仿一下这个app.既然是寄几开发,那还不是寄几说了算,毫无疑问整款APP的主题风格被我改成我最爱的终极少女粉了hhh,下面让我们一起来感受下探探的魅力吧~ 项目整体效果 项目

  • 微信小程序导航栏跟随滑动效果的实现代码

    效果图 .wxml <view class='tabBar'> <view class='tabItem wx:if="{{tabClick===0?"click":""}}"' bindtap='clickTab' data-index='0'>tab1</view> <view class='tabItem wx:if="{{tabClick===1?"click":&q

  • 微信小程序实现滑动操作代码

    前言 本文使用动画组件wx.createAnimation来实现滑动操作: 1. 左滑动显示操作项区域: 2. 点击操作项触发相应方法: 3. 右滑动和点击行收起操作项: 4. 点击"删除"并确定则删除该条数据. 最终效果如下: 实现过程 1. 文件index.wxml和index.wxss代码如下,这一块比较简单,可自行查看,不做过多分析: Tips:"详情"."取号"和"删除"点击触发使用catchtap,阻止冒泡事件向上

  • 微信小程序之滑动页面隐藏和显示组件功能的实现代码

    用csdnAPP的用户都知道,在发布blink动态时,那个红色按钮会随着你滚动页面消失或者出现.往上滑动时,按钮消失.往下滑动时,按钮出现. 今天我们就模仿一下这个功能,只不过我们换中样式,让它逐渐滑出页面,或逐渐从页面之外滑到固定位置. 效果图: 滑动前: 滑动后: 此功能是往上滑动消失,往下滑动出现. 实现步骤: 编写页面代码与样式 编写逻辑代码 wxml: <view class="mask-con {{!hidden ? 'mask-con-show' : '' } } sendD

  • 微信小程序左右滑动切换页面详解及实例代码

    微信小程序--左右滑动切换页面事件 微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是pageX和pageY,表示X,Y坐标. touchstart在触摸开始时触发事件; touchend在触摸结束时触发事件; touchmove触摸的过程中不断激发这个事件; 这三个事件都有一个timeStamp的属性,查看timeStamp属性,可以看到顺序是touchstart => touchmove=> touchmov

  • 微信小程序实现滑动翻页效果(完整代码)

    微信小程序实现滑动翻页效果,效果图如下所示: 源码: <view class="mainFrame"> <swiper class="container" indicator-dots="{{indicatorDots}}" indicator-dots="{{indicatordots}}" autoplay="{{autoplay}}" interval="{{interva

  • 微信小程序实现滑动验证拼图

    本文实例为大家分享了微信小程序实现滑动验证拼图的具体代码,供大家参考,具体内容如下 效果图 .wxml <button bindtap="visidlisd">滑动验证</button> <!-- 滑动验证弹窗 --> <view class="slide_model" wx:if="{{slidebel}}">   <view>       <view class="c

  • 微信小程序 按钮滑动的实现方法

    微信小程序 按钮滑动的实现方法 一.先看东西 滑动前 滑动后 二.再上代码 index.wxml <view class="content"> <view class="sliderContent"> <input placeholder="验证码" placeholder-class="input-placeholder" disabled="{{disabled}}" /&

  • 微信小程序实现滑动切换自定义页码的方法分析

    本文实例讲述了微信小程序实现滑动切换自定义页码的方法.分享给大家供大家参考,具体如下: 效果如下: 这里三个图片使用了swiper组件进行轮播,下方的页码数字1.2.3会随着图片的切换变动位置 在微信小程序中我们是无法操作dom的,那么 var div = document.getElementById('id'); div.setAttribute("class", "className"); 这种方式实现. 然后我们可以考虑使用hidden或者wx:if的方式,

  • 操作按钮悬浮固定在微信小程序底部的实现代码

    本章节主要介绍了如何将操作按钮悬浮固定在微信小程序底部?操作起来也比较简单,还不会的朋友一起跟着小编学习一下吧,希望对你们有所帮助. 常见的有加入购物车按钮.结算按钮.收货列表添加地址按钮. 以收货地址为例,将添加地址按钮悬浮于最底部,这样再多的地址,也不会被遮挡而看不见. 核心代码如下: 添加 /*添加地址按钮*/ .address-add { position: fixed; bottom: 0; width: 100%; } 改用position: fixed之后,其中width需要设置为

  • 微信小程序进入广告实现代码实例

    这篇文章主要介绍了微信小程序进入广告实现代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 <view class="container"> <image src="../../imgs/swiper1.jpg"></image> <text bindtap="cliadv">跳过广告 {{miao}}</text> &l

随机推荐