微信小程式實現滑動操作程式碼
阿新 • • 發佈:2020-04-26
前言
本文使用動畫元件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',{ id: 3,title: '業務辦理03',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,// 手指觸控動作結束 touchendX() { let recordX; if (movedistance <= -100) { // 移動達到距離就動畫顯示全部操作項 recordX = -300; } else if (movedistance >= -100) { // 移動未達到距離即還原 recordX = 0; } this.slideAnimation(recordX,// 滑動動畫 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; // 解決切換到其它頁面再返回該頁面動畫失效的問題 } })
到此這篇關於微信小程式實現滑動操作程式碼的文章就介紹到這了,更多相關微信小程式滑動內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!