微信小程序左右滑動刪除事件詳解
本文實(shí)例為大家分享了微信小程序左右滑動刪除事件,供大家參考,具體內(nèi)容如下
效果圖
上代碼
<scroll-view scroll-y enable-back-to-top style="height:{{ scrollHeight }}px" > ? ? <view> ? ? ? ? <block wx:for="{{ list }}" wx:for-item="item" wx:for-index="index" wx:key="index" > ? ? ? ? ? ? <view class="list {{ item.isTouchMove ? 'touch-move-active' : '' }}" ? ?bindtouchstart="touchStart" bindtouchmove="touchMove" data-index="{{ index }}" > ? ? ? ? ? ? ? ? <view class="txt">{{ item.id }} -- {{ item.title }}</view> ? ? ? ? ? ? ? ? <view class="del" bindtap="delList" data-index="{{ index }}" > 刪除 </view> ? ? ? ? ? ? </view> ? ? ? ? </block> ? ? </view> </scroll-view>
/* 列表 */ .list { ? ? display: flex; ? ? justify-content: space-between; ? ? width: 100%; ? ? height: 100rpx; ? ? line-height: 100rpx; ? ? overflow: hidden; ? ? text-align: center; ? ? border-bottom: 1px solid #cccccc; } /* 列表內(nèi)容 */ .list .txt { ? ? flex-grow: 1; ? ? width: 100%; ? ? margin-left: -150rpx; ? ? background-color: #fff; } /* 刪除按鈕 */ .list .del { ? ? flex-grow: 0; ? ? width: 150rpx; ? ? color: #fff; ? ? background-color: #fe3e2f; } .list .txt, .list .del { ? ? transform: translateX(150rpx); ? ? transition: all 0.4s; } .touch-move-active .txt,.touch-move-active .del { ? ? -webkit-transform: translateX(0); ? ? transform: translateX(0); }
js:
Page({ ? ? /** ? ? ?* 頁面的初始數(shù)據(jù) ? ? ?*/ ? ? data: { ? ? ? ? list: [ ? ? ? ? ? ? { id: "0001", title: "商品1" }, ? ? ? ? ? ? { id: "0002", title: "商品2" }, ? ? ? ? ? ? // .......... ? ? ? ? ], ? ? ? ? scrollHeight: 0, ?// scroll-view高度 ? ? ? ? startX: 0, ? ? ? ?// 開始X坐標(biāo) ? ? ? ? startY: 0, ? ? ? ?// 開始Y坐標(biāo) ? ? }, ? ? // 手指觸摸動作開始 ? ? touchStart: function(e){ ? ? ? ? let that = this; ? ? ? ? //開始觸摸時 重置所有刪除 ? ? ? ? that.data.list.forEach(function (v, i) { ? ? ? ? ? ? if (v.isTouchMove) v.isTouchMove = false; // 只操作為true的 ? ? ? ? }) ? ? ? ? // 記錄手指觸摸開始坐標(biāo) ? ? ? ? that.setData({ ? ? ? ? ? ? startX: e.changedTouches[0].clientX, ?// 開始X坐標(biāo) ? ? ? ? ? ? startY: e.changedTouches[0].clientY, ?// 開始Y坐標(biāo) ? ? ? ? ? ? list: that.data.list ? ? ? ? }) ? ? }, ? ? // 手指觸摸后移動 ? ? touchMove: function(e){ ? ? ? ? let that = this, ? ? ? ? ? ? index = e.currentTarget.dataset.index, ? ?// 當(dāng)前下標(biāo) ? ? ? ? ? ? startX = that.data.startX, ? ? ? ? ? ? ? ?// 開始X坐標(biāo) ? ? ? ? ? ? startY = that.data.startY, ? ? ? ? ? ? ? ?// 開始Y坐標(biāo) ? ? ? ? ? ? touchMoveX = e.changedTouches[0].clientX, // 滑動變化坐標(biāo) ? ? ? ? ? ? touchMoveY = e.changedTouches[0].clientY, // 滑動變化坐標(biāo) ? ? ? ? ? ? // 獲取滑動角度 ? ? ? ? ? ? angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY }); // 判斷滑動角度 ? ? ? ? that.data.list.forEach(function (v, i) { ? ? ? ? ? ? v.isTouchMove = false ? ? ? ? ? ? // 滑動超過30度角 return ? ? ? ? ? ? if (Math.abs(angle) > 30) return; ? ? ? ? ? ? if (i == index) { ? ? ? ? ? ? ? ? // 右滑 ? ? ? ? ? ? ? ? if (touchMoveX > startX)? ? ? ? ? ? ? ? ? ? ? v.isTouchMove = false ? ? ? ? ? ? ? ? // 左滑 ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? ? v.isTouchMove = true ? ? ? ? ? ? } ? ? ? }) ? ? ? // 更新數(shù)據(jù) ? ? ? that.setData({ ? ? ? ? ? list: that.data.list ? ? ? }) ? ? }, ? ? // 計(jì)算滑動角度 ? ? angle: function (start, end) { ? ? ? ? let that = this, ? ? ? ? ? ? _X = end.X - start.X, ? ? ? ? ? ? _Y = end.Y - start.Y; ? ? ? ? // 返回角度 /Math.atan()返回?cái)?shù)字的反正切值 ? ? ? ? return 360 * Math.atan(_Y / _X) / (2 * Math.PI); ? ? }, ? ? // 刪除 ? ? delList: function(e){ ? ? ? ? let that = this, ? ? ? ? ? ? index = e.currentTarget.dataset.index; ?// 當(dāng)前下標(biāo) // 切割當(dāng)前下標(biāo)元素,更新數(shù)據(jù) ? ? ? ? that.data.list.splice(index, 1);? ? ? ? ? that.setData({ ? ? ? ? ? ? list: that.data.list ? ? ? ? }) ? ? }, ? ? /** ? ? ?* 生命周期函數(shù)--監(jiān)聽頁面加載 ? ? ?*/ ? ? onLoad: function (options) { ? ? ? ? let that = this; ? ? ? ? // 動態(tài)獲取屏幕高度 ? ? ? ? that.setData({ ? ? ? ? ? ? scrollHeight: wx.getSystemInfoSync().screenHeight ? ? ? ? }) ? ? }, })
這是左滑動刪除,如需要右滑動刪除在js頁面調(diào)整一下就好。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS數(shù)組方法reverse()用法實(shí)例分析
這篇文章主要介紹了JS數(shù)組方法reverse()用法,結(jié)合實(shí)例形式分析了JS數(shù)組reverse()方法基本功能、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01Javascript節(jié)點(diǎn)關(guān)系實(shí)例分析
這篇文章主要介紹了Javascript節(jié)點(diǎn)關(guān)系,實(shí)例分析了javascript操作父子節(jié)點(diǎn)及兄弟節(jié)點(diǎn)的相關(guān)技巧,需要的朋友可以參考下2015-05-05IE中g(shù)etElementsByName()對有些元素?zé)o效的解決方案
這篇文章主要介紹了IE中g(shù)etElementsByName()對有些元素?zé)o效的解決方案,很簡單,很實(shí)用,需要的朋友可以參考下2014-09-09微信小程序全局配置之tabBar實(shí)戰(zhàn)案例
tabBar相對而言用的還是比較多的,但是用起來并沒有難,下面這篇文章主要給大家介紹了關(guān)于微信小程序全局配置之tabBar的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08webpack拆分壓縮css并以link導(dǎo)入的操作步驟
我們運(yùn)行打包后會發(fā)現(xiàn)less轉(zhuǎn)為了css文件,但css文件確通過js加入style標(biāo)簽,下面我們將css進(jìn)行拆分出來,并以link標(biāo)簽引入,具體實(shí)現(xiàn)步驟一起看看吧2021-10-10JS Testing Properties 判斷屬性是否在對象里的方法
下面小編就為大家?guī)硪黄狫S Testing Properties 判斷屬性是否在對象里的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10