小程序实现左滑删除的效果的实例代码


Posted in Javascript onOctober 19, 2020

前言:实现小程序滑动删除有几种方式,文章会简单列举两种实现,先看效果。

小程序实现左滑删除的效果的实例代码

一、使用movable-view实现滑动

先看官方文档

小程序实现左滑删除的效果的实例代码

简单解读一下movable-area标签的基本概念。movable-area标签就是定义了一个可移动的视图容器,支持在页面中拖拽滑动,跟普通的view容器是一样的,但是也有不同之处,movable-area必须设置width和height属性,不设置默认为10px;movable-view 默认为绝对定位,top和left属性为0px。

<movable-area>
  <movable-view out-of-bounds="true" direction="horizontal" inertia="true">
  </movable-view>
  </movable-area>

我们需要用的一些属性out-of-bounds,给他定义true,让我们的容器超过可移动区域后,movable-view还可以移动,direction属性是定义我们滑动的方向,vertical是垂直滑动,horizontal是水平滑动。

二、使用Touch事件实现滑动

1.bindtouchstart 函数,手指触摸动作开始
2.bindtouchmove 函数,手指触摸后移动
3.bindtouchend 函数,手指触摸动作结束

实现思路:
1.页面上的容器分为上下两层,上面一层显示正常加载无动作的内容,下面一层显示容器触发事件后展示的内容,例如删除、置顶、标为未读等按钮。
2.每个容器上面那一层容器我们通过css使用定位来固定,通过操纵事件来实现向需要移动的方向移动。
3.通过官方文档提供的API来实现容器随着方向移动。

完整代码如下

1.wxml

<view class="">
 <view class="containerTitle">使用movable-view实现左滑</view>
 <view class="list">
 <view class="product-item" wx:for="{{productList}}" wx:for-index="index" wx:key="{{item.id}}" >
  <movable-area>
  <movable-view out-of-bounds="true" direction="horizontal" x="{{item.xmove}}"
   inertia="true"
   data-productIndex="{{index}}"
   bindtouchstart="handleTouchStart"
   bindtouchend="handleTouchEnd"
   bindchange="handleMovableChange">
   <view class="product-item-wrap">
   <view class="product-movable-item">
    <view class="product-movable-item-name">{{item.name}}</view>
    <view class="product-movable-item-code">{{item.code}}</view>
   </view>
   <view class="product-movable-item product-movable-item-amount">
    <text class="amount">{{item.amount}}</text>
    <text class="unit">万</text>
   </view>
   </view>
  </movable-view>
  </movable-area>
  <view class="delete-btn" data-id="{{item.id}}" bindtap="handleDeleteProduct">删除</view>
 </view>
 </view>
 <view class="containerTitle">使用Touch事件实现左滑</view>
 <view class="list">
 <view class="product-item" wx:for="{{slideProductList}}" wx:for-index="index" wx:key="{{item.id}}">
  <slide-delete pid="{{item.id}}" bindaction="handleSlideDelete" wx:key="{{item.id}}">
  <view class="product-item-wrap">
   <view class="product-movable-item">
   <view class="product-movable-item-name">{{item.name}}</view>
   <view class="product-movable-item-code">{{item.code}}</view>
   </view>
   <view class="product-movable-item product-movable-item-amount">
   <text class="amount">{{item.amount}}</text>
   <text class="unit">万</text>
   </view>
  </view>
  </slide-delete>
 </view>
 </view>
</view>

 

2.wxss

.containerTitle {
 margin: 60rpx 0 30rpx;
 font-size: 40rpx;
 text-align: center;
 font-weight: bold;
 color: #383A3D;
}

.list .product-item {
 position: relative;
 width: 100vw;
 border-bottom: 2rpx solid #E9E9E9;
 box-sizing: border-box;
 background: #fff;
 z-index: 999;
}

.slide-product-list .slide-product-item {
 position: relative;
 width: 100vw;
 border-bottom: 2rpx solid #E9E9E9;
 box-sizing: border-box;
 background: #fff;
 z-index: 999;
}

.list .product-item movable-area {
 height: 120rpx;
 width: calc(100vw - 120rpx);
}

.list .product-item movable-view {
 height: 120rpx;
 width: 100vw;
 background: #fff;
 z-index: 999;
}

.list .product-item .delete-btn {
 position: absolute;
 top: 0;
 bottom: 0;
 right: 0; 
 width: 120rpx;
 font-family: PingFangSC-Regular;
 font-size: 24rpx;
 color: #FFFFFF;
 line-height: 120rpx;
 z-index: 1;
 background: red;
 text-align: center;
}

.list .product-item-wrap {
 position: relative;
 display: flex;
 align-items: center;
 padding: 8rpx 0 20rpx 20rpx;
 box-sizing: border-box;
}

.list .product-item-wrap .product-movable-item {
 flex: 1;
 overflow: hidden;
}

.list .product-item-wrap .product-movable-item-name {
 font-family: PingFangSC-Regular;
 font-size: 28rpx;
 color: #71747A;
 line-height: 60rpx;
 margin-right: 10rpx;
 overflow: hidden;
 white-space: nowrap;
 text-overflow: ellipsis;
}

.list .product-item-wrap .product-movable-item-code {
 font-family: PingFangSC-Regular;
 font-size: 24rpx;
 color: #969AA3;
}

.list .product-item-wrap .product-movable-item-amount {
 flex: 0 0 auto;
 padding-right: 80rpx;
 position: relative;
}

.list .product-item-wrap .product-movable-item-amount .amount {
 width: 120rpx;
 font-size: 28rpx;
 color: #383A3D;
 line-height: 60rpx;
}

.list .product-item-wrap .product-movable-item-amount .unit {
 position: absolute;
 top: 0;
 right: 30rpx;
 font-size: 28rpx;
 color: #969AA3;
 line-height: 60rpx;
}

 

3.js代码

//获取应用实例
const app = getApp()

Page({
 data: {
 productList: [
  {
  id: 1,
  name: '31省市区新增境外输入13例',
  code: 'Jin日头条',
  amount: 5
  },
  {
  id: 2,
  name: '饲养员遭熊攻击身亡',
  code: 'bai度新闻',
  amount: 4
  },
  {
  id: 3,
  name: '安倍晋三参拜靖国神社',
  code: '日媒',
  amount: 10
  }
 ],
 slideProductList: [
  {
  id: 4,
  name: '老兵回忆参加抗美援朝说今生无悔',
  code: 'xin微博',
  amount: 101
  },
  {
  id: 5,
  name: '女子下楼时玩手机踩空摔伤',
  code: 'zz资讯',
  amount: 500
  },
  {
  id: 6,
  name: '杨紫为离线庆生',
  code: 'xx新闻',
  amount: 110
  }
 ]
 },

 onLoad: function () {

 },

 /**
 * 显示删除按钮
 */
 showDeleteButton: function (e) {
 let productIndex = e.currentTarget.dataset.productindex
 this.setXmove(productIndex, -65)
 },

 /**
 * 隐藏删除按钮
 */
 hideDeleteButton: function (e) {
 let productIndex = e.currentTarget.dataset.productindex

 this.setXmove(productIndex, 0)
 },

 /**
 * 设置movable-view位移
 */
 setXmove: function (productIndex, xmove) {
 let productList = this.data.productList
 productList[productIndex].xmove = xmove

 this.setData({
  productList: productList
 })
 },

 /**
 * 处理movable-view移动事件
 */
 handleMovableChange: function (e) {
 if (e.detail.source === 'friction') {
  if (e.detail.x < -30) {
  this.showDeleteButton(e)
  } else {
  this.hideDeleteButton(e)
  }
 } else if (e.detail.source === 'out-of-bounds' && e.detail.x === 0) {
  this.hideDeleteButton(e)
 }
 },

 /**
 * 处理touchstart事件
 */
 handleTouchStart(e) {
 this.startX = e.touches[0].pageX
 },

 /**
 * 处理touchend事件
 */
 handleTouchEnd(e) {
 if(e.changedTouches[0].pageX < this.startX && e.changedTouches[0].pageX - this.startX <= -30) {
  this.showDeleteButton(e)
 } else if(e.changedTouches[0].pageX > this.startX && e.changedTouches[0].pageX - this.startX < 30) {
  this.showDeleteButton(e)
 } else {
  this.hideDeleteButton(e)
 }
 },

 /**
 * 删除产品
 */
 handleDeleteProduct: function ({ currentTarget: { dataset: { id } } }) {
 let productList = this.data.productList
 let productIndex = productList.findIndex(item => item.id === id)
 productList.splice(productIndex, 1)
 this.setData({
  productList
 })
 if (productList[productIndex]) {
  this.setXmove(productIndex, 0)
 }
 },

 /**
 * slide-delete 删除产品
 */
 handleSlideDelete({ detail: { id } }) {
 let slideProductList = this.data.slideProductList
 let productIndex = slideProductList.findIndex(item => item.id === id)
 slideProductList.splice(productIndex, 1)
 this.setData({
  slideProductList
 })
 }
})

总结

到此这篇关于小程序实现左滑删除的效果的文章就介绍到这了,更多相关小程序左滑删除内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
jquery 无限级下拉菜单的简单实现代码
Feb 21 Javascript
JS实现鼠标经过好友列表中的好友头像时显示资料卡的效果
Jul 02 Javascript
javascript跨域的方法汇总
Oct 23 Javascript
详解JavaScript中localStorage使用要点
Jan 13 Javascript
jQuery实例—选项卡的简单实现(js源码和jQuery)
Jun 14 Javascript
漫谈JS引擎的运行机制 你应该知道什么
Jun 15 Javascript
自动化测试读写64位操作系统的注册表
Aug 15 Javascript
详解Angular 4.x Injector
May 04 Javascript
微信小程序实现拖拽 image 触摸事件监听的实例
Aug 17 Javascript
详解VUE中常用的几种import(模块、文件)引入方式
Jul 03 Javascript
在React项目中使用Eslint代码检查工具及常见问题
Oct 10 Javascript
vue实现页面切换滑动效果
Jun 29 Javascript
jQuery实现图片切换效果
Oct 19 #jQuery
jQuery实现回到顶部效果
Oct 19 #jQuery
jQuery实现放大镜案例
Oct 19 #jQuery
vue v-model的用法解析
Oct 19 #Javascript
jQuery插件实现图片轮播效果
Oct 19 #jQuery
jquery插件实现轮播图效果
Oct 19 #jQuery
SpringBoot+Vue开发之Login校验规则、实现登录和重置事件
Oct 19 #Javascript
You might like
一个简单的php实现的MySQL数据浏览器
2007/03/11 PHP
php实现无限级分类实现代码(递归方法)
2011/01/01 PHP
探讨PHP中this,self,parent的区别详解
2013/06/08 PHP
PHP调用MySQL存储过程并返回值的方法
2014/12/26 PHP
XHProf报告字段含义的解析
2016/05/17 PHP
php生成条形码的图片的实例详解
2017/09/13 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
详解php中生成标准uuid(guid)的方法
2019/04/28 PHP
YII2.0框架行为(Behavior)深入详解
2019/07/26 PHP
javascript 自定义事件初探
2009/08/21 Javascript
如何在node的express中使用socket.io
2014/12/15 Javascript
基于jQuery+PHP+Mysql实现在线拍照和在线浏览照片
2015/09/06 Javascript
基于JS代码实现当鼠标悬停表格上显示这一格的全部内容
2016/06/12 Javascript
利用jquery给指定的table动态添加一行、删除一行的方法
2016/10/12 Javascript
Node.js编写CLI的实例详解
2017/05/17 Javascript
js的函数的按值传递参数(实例讲解)
2017/11/16 Javascript
react实现换肤功能的示例代码
2018/08/14 Javascript
在 Vue-CLI 中引入 simple-mock实现简易的 API Mock 接口数据模拟
2018/11/28 Javascript
[00:18]天涯墨客三技能展示
2018/08/25 DOTA
Python中还原JavaScript的escape函数编码后字符串的方法
2014/08/22 Python
使用python3.5仿微软记事本notepad
2016/06/15 Python
Python实现计算两个时间之间相差天数的方法
2017/05/10 Python
Python实现按当前日期(年、月、日)创建多级目录的方法
2018/04/26 Python
使用python读取csv文件快速插入数据库的实例
2018/06/21 Python
keras 使用Lambda 快速新建层 添加多个参数操作
2020/06/10 Python
Python自动发送和收取邮件的方法
2020/08/12 Python
社团成立邀请函
2014/01/08 职场文书
文化建设工作方案
2014/05/12 职场文书
导师工作推荐信范文
2014/05/17 职场文书
学校食堂食品安全责任书
2014/07/28 职场文书
基层党员对照检查材料
2014/08/25 职场文书
十佳家长事迹材料
2014/08/26 职场文书
婚内房产协议书范本
2014/10/02 职场文书
家长会后的感想
2015/08/11 职场文书
六年级数学教学反思
2016/02/16 职场文书
导游词之长城八达岭
2019/09/24 职场文书