wepy 滑动删除功能

小程序wepy框架,前段时间做的项目使用到滑动删除功能,然后自己写了个组件,话不多说,直接上代码

<style lang="less">
.swipe-item-wrapper{
overflow: hidden;
position: relative;
.swipe-item-wrapper-action {
position: absolute;
z-index: 2;
background: transparent;
height: 100%;
width: 100%;
top: 0;
display: none;
}
.swipe-item-wrapper-action-popup {
display: block;
}
.swipe-actions{
width: 80rpx;
position: absolute;
top: 0;
bottom: 0;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
visibility: hidden;
white-space: nowrap;
z-index: 10;
}
.actions-right {
right: 0;
}
.actions-visibility {
visibility: visible;
}
.swiper-item-wrapper-content {
width: 100%;
}
.swiper-item-wrapper-content-slide {
transform: translate(-80rpx);
}
.swipe-btn{
flex: 1;
height: 100%;
min-height: 90rpx;
line-height: 90rpx;
text-align: center;
color: white;
display: flex;
justify-content: center;
align-items: center;
&.del{
background-color: rgb(244, 51, 60);
}
&.edit{
background-color: rgb(16, 142, 233);
}
}
}
</style>
<template>
<view class="swipe-item-wrapper"
id="{{swipeData.id}}"
@touchstart="tStart"
@touchmove="tMove"
@touchend="tEnd">
<view class="swipe-item-wrapper-action {{swipeData.id == slideIndex ? 'swipe-item-wrapper-action-popup' : ''}}"></view>
<view class="swipe-actions actions-right {{swipeData.id == slideIndex ? 'actions-visibility' : ''}}">
<view class="swipe-btn del" @tap.stop="handleRightBtnTap({{ swipeData }})">删除</view>
</view>
<view class="swiper-item-wrapper-content {{swipeData.id == slideIndex ? 'swiper-item-wrapper-content-slide' : ''}}">
<slot></slot>
</view>
</view>
</template>
<script>
import wepy from 'wepy'
export default class WepyDelete extends wepy.component {
props = {
swipeData: {
type: Object,
default: {},
twoWay: true
}
}
data = {
startX: 0,
startY: 0,
endX: 0,
endY: 0,
distenceX: 0,
moveX: null,
swiperData: {},
slideIndex: null,
}
onLoad () {}
watch = {
swipeData(newValue, oldValue) {
// console.log(newValue, oldValue)
}
}
// 获得角度
getAngle(angx, angy) {
return Math.atan2(angy, angx) * 180 / Math.PI;
};
// 根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
getDirection(startx, starty, endx, endy) {
let angx = endx - startx;
let angy = endy - starty;
let result = 'no';
// 如果滑动距离太短
if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
return result;
}
let angle = this.getAngle(angx, angy);
if (angle >= -135 && angle <= -45) {
result = 'up';
} else if (angle > 45 && angle < 135) {
result = 'down';
} else if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
result = 'left';
} else if (angle >= -45 && angle <= 45) {
result = 'right';
}
return result;
};
methods = {
tStart (e) {
if (e.touches.length === 1) {
this.startX = e.touches[0].clientX
this.startY = e.touches[0].clientY
this.moveX = e.touches[0].clientX
this.$apply()
}
},
tMove (e) {
if (e.touches.length === 1) {
// 手指起始点位置与移动期间的差值
let distenceX = this.moveX - e.touches[0].clientX
this.distenceX = distenceX;
this.$apply()
}
},
tEnd (e) {
if (e.changedTouches.length === 1){
this.endX = e.changedTouches[0].clientX
this.endY = e.changedTouches[0].clientY
this.$apply()
let result = this.getDirection(this.startX, this.startY, this.endX, this.endY)
if (((this.distenceX) > 50) && (result == 'left')) {
this.slideIndex = e.currentTarget.id
} else if (((this.distenceX) < 0) && (result == 'right')) {
this.slideIndex = null
} else {
this.slideIndex = null
}
}
},
handleRightBtnTap(item) {
item = JSON.parse(JSON.stringify(item))
this.$emit('delItem', item)
},
slideHideBtn(label) {
this.slideIndex = null
}
}
}
</script>

微信小程序-wepy-侧滑删除组件,支持自定义内容区在最近的微信小程序开发过程中需要用到侧滑删除功能,微信小程序官方是没有提供这样的组件,再加上我们的微信小程序使用的是wepy组件开发框架开发的,wepy也没有提供这样的组件,之前也在github上搜索这方面的组件,没有发现合适的,当时只发现了一个开源的:https://github.com/GeoffZhu/wepy-swipe-delete 只不过该组件功能单一已经被作者废弃了,无奈自己动手撸了一个侧滑删除组件,现在把它开源出来吧。Requirementswepy: "^1.7.3"支持功能和特点自定义内容区域:支持之定义内容区域,组件内使用 slot占位。自定义滚动高度:可以自定义scroll-view的高度,默认为屏幕的高度。自定义menu :如果默认的menu样式不喜欢可以自定义,也可以显示或者隐藏指定的menu。左右滑动:支持左右滑动也可以设置只左右或者右滑。效果如下:       如何使用目前支持两种使用模式:1.page页面模式优点:可定制化高,扩展性强。 缺点:集成复杂,代码复用性差。2.component 组件模式优点:集成简单,代码复用性强,减少包的大小。 缺点:可定制到低。不建议使用page页面模式,下面详细介绍component 组件模式的使用如何使用// 导入组件import SwipeDeleteView from '@/components/wepy-swipe-delete-view'// 声明组件  components = {       swipeDelete: SwipeDeleteView     }       // 引用组件   <template>   <swipeDelete :list.sync="list">       <view class="item">{{item.userName}}</view>     </swipeDelete>   </template>配置如下:<swipeDelete   :list.sync="list"                :scrollHeight="scrollHeight"                @deleteTap.user="deleteTap"                @deleteLongTap.user="deleteLongTap"                @editTap.user="editTap"                @editLongTap.user="editLongTap"                @addTap.user="addTap"                @addLongTap.user="addLongTap"                @markTap.user="markTap"                @markLongTap.user="markLongTap">属性属性说明备注list要显示的列表的原始数据,最好是加上.sync这样可以异步传值必传scrollHeight设置scroll-view的高度,默认为屏幕高度选传deleteTap.user删除menu单击事件回调必要时传deleteLongTap.user删除menu长按事件回调必要时传editTap.user编辑menu单击事件回调必要时传editLongTap.user编辑menu长按事件回调必要时传addTap.user添加menu单击事件回调必要时传addLongTap.user添加menu长按事件回调必要时传markTap.user标记menu单击事件回调必要时传markLongTap.user标记menu长按事件回调必要时传每个点击事件或者长按事件都会返回两个参数methods = {       deleteTap(view, item) {         console.log(item)         view.deleteItem()       },       deleteLongTap(view, item) {         console.log(item)         wx.showModal({           title: '提示',           content: '确定要删除吗?',           success: function (res) {             if (res.confirm) {               view.deleteItem()             } else {               view.closeItem()             }           }         })       }    }view :view 是SwipeDeleteView对象的本身,可以通过view来做一些其他操作,如:删除当前的itemitem : item 就是当前操作的原始数据,可以通过item获取真正需要的数据当前SwipeDeleteView对外暴露的函数有:函数名称函数说明参数deleteItem删除当前操作的item(原始数据 view)都会被删除不需要参数closeItem关闭当前操作的item,原始数据不变不需要参数网络异步加载更新如果使用的网络异步加载完成以后,还需要更新下组件,不然会出现无法侧滑的情况。导致原因:原始数据更新以后组件内部无法监听到,这样就不能获取到左右menu的宽度,所以不能侧滑解决办法:this.$invoke('swipeDelete', 'update'); swipeDelete必须和 components = {swipeDelete: SwipeDeleteView} 同名。代码如下:// 模拟网络加载延迟2秒钟    let that = this      setTimeout(function () {        for (let i = 0; i < 20; i ) {          let msg = {}          msg.userName = ''   '用户'   (i   1)          msg.msgText = '您有新的消息'          msg.color = that.buildRandomColor()          msg.height = that.buildRandomHeight()          msg.headerImg = options.avatarUrl          that.list.push(msg)        }        wx.hideLoading()        // 更新组件        that.$invoke('swipeDelete', 'update')        that.$apply()      }, 2000)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值