微信小程序 實(shí)現(xiàn)列表項(xiàng)滑動顯示刪除按鈕的功能
微信小程序 實(shí)現(xiàn)列表項(xiàng)滑動顯示刪除按鈕的功能
微信小程序并沒有提供列表控件,所以也沒有iOS上慣用的列表項(xiàng)左滑刪除的功能,SO只能自己干了。
原理很簡單,用2個層,上面的層顯示正常的內(nèi)容,下面的層顯示一個刪除按鈕,就是記錄手指滑動的距離,動態(tài)的來移動上層元素,當(dāng)然上層用絕對定位。
wxml:
<view class="container"> <view class="record-box" data-datetime="{{record.datetime}}" wx:for="{{detailList}}" wx:for-item="record"> <view class="record" style="left:{{record.offsetX}}px" bindtouchstart="recordStart" catchtouchmove="recordMove" bindtouchend="recordEnd"> <view class="left"> <view>{{record.type}} {{record.count+record.unit}}</view> <view class="summary">{{record.remark}}</view> </view> <view class="right"> {{record.datetime}} </view> </view> <view class="delete-box"> <view>刪除</view> </view> </view> </view>
wxss:
@import "../common/weui.wxss"; .container { box-sizing: border-box; padding: 0 0 0 0; } .record { display: flex; flex-direction: row; align-items: center; width:100%; height: 120rpx; position: absolute; justify-content: space-between; background-color: #fff; } .record .right{ margin-right: 30rpx; color: #888888; } .record .left{ margin-left: 30rpx; display: flex; flex-direction: column; justify-content: space-between; } .record .left .summary{ color: #aaa; font-size: 30rpx; line-height: 30rpx; } .record-box{ height: 120rpx; width: 100%; border-bottom: 1rpx solid #ddd; background-color: #fff; } .delete-box{ background-color: #e64340; color: #ffffff; float: right; height: 100%; width: 80px; display: flex; align-items: center; justify-content: center; } .record-box:last-child { border-bottom: 0; } .record .r-item { }
JS代碼:
var detailList = [ { datetime: '2017-01-01 17:00', count: 100, unit: 'ml', type: '開水', remark: '哈哈哈哈' }, { datetime: '2017-01-01 17:01', count: 100, unit: 'ml', type: '開水' }, { datetime: '2017-01-01 17:02', count: 100, unit: 'ml', type: '開水' } ]; var recordStartX = 0; var currentOffsetX = 0; Page( { data: { detailList: detailList } , recordStart: function (e) { recordStartX = e.touches[0].clientX; currentOffsetX = this.data.detailList[0].offsetX; console.log('start x ', recordStartX); } , recordMove: function (e) { var detailList = this.data.detailList; var item = detailList[0]; var x = e.touches[0].clientX; var mx = recordStartX - x; console.log('move x ', mx); var result = currentOffsetX - mx; if (result >= -80 && result <= 0) { item.offsetX = result; } this.setData({ detailList: detailList }); } , recordEnd: function (e) { var detailList = this.data.detailList; var item = detailList[0]; console.log('end x ', item.offsetX); if (item.offsetX < -40) { item.offsetX = -80; } else { item.offsetX = 0; } this.setData({ detailList: detailList }); } } );
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
微信小程序 基礎(chǔ)知識css樣式media標(biāo)簽
這篇文章主要介紹了微信小程序 基礎(chǔ)知識css樣式media標(biāo)簽的相關(guān)資料,需要的朋友可以參考下2017-02-02ECharts transform數(shù)據(jù)轉(zhuǎn)換和dataZoom在項(xiàng)目中使用
這篇文章主要為大家介紹了ECharts transform數(shù)據(jù)轉(zhuǎn)換和dataZoom在項(xiàng)目中使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12微信小程序 ecshop地址三級聯(lián)動實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了微信小程序 ecshop地址3級聯(lián)動實(shí)現(xiàn)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02微信小程序中input標(biāo)簽詳解及簡單實(shí)例
這篇文章主要介紹了微信小程序中input標(biāo)簽詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Javascript實(shí)現(xiàn)的分頁函數(shù)
Javascript實(shí)現(xiàn)的分頁函數(shù)...2006-12-12