微信小程序?qū)崿F(xiàn)左滑修改、刪除功能
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)左滑修改、刪除的具體代碼,供大家參考,具體內(nèi)容如下
wxml:
<view class="offer-item" wx:for-items='{{offerList}}'>
<!--這里綁定了剛才說的3個函數(shù)分別為 touchS,touchM touchE-->
<!--這里注意這個 style="{{item.txtStyle}}" ,這是我們一會再js中 將要設(shè)置的樣式 -->
<view style="{{item.txtStyle}}">
<view class="offer-item-top fl clearfix" bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" data-index="{{index}}">
<navigator bindtap='navigatorTo' data-index="{{item.id}}">
<view class='content'>
<view class='title clearfix'>
<view class='fl'>
{{item.title}}黨建項目報價表
</view>
<image src='../../images/right.png' class='fr'></image>
</view>
<view class='note clearfix'>
<view class='price fl'>
{{item.create_time}}
</view>
</view>
</view>
</navigator>
</view>
<!--這里是左滑按鈕部分----start-->
<view bindtap="delItem" class='posit fr isMove' hidden='{{!item.isMove}}'>
<view class="ref" data-offerid="{{item.id}}" data-index="{{item.id}}" catchtap="ref">
<image src='../../images/ref.png'></image>
</view>
<view class="del" data-offerid="{{item.id}}" data-index="{{item.id}}" catchtap="del">
<image src='../../images/default.png'></image>
</view>
</view>
<!--這里是左滑按鈕部分----end-->
</view>
</view>
wxss:
.offer-item {
height: 150rpx;
width: 100vw;
overflow-x: hidden;
border-bottom: 1px solid #f0f0f0;
}
.offer-item>view {
position: absolute;
/* width: calc(100% + 200rpx); */
height: 150rpx;
}
.offer-item .offer-item-top {
height: 100%;
}
.offer-item .offer-item-top navigator {
display: inline-block;
width: 100vw;
height: 100%;
}
.offer-item .content {
padding: 35rpx 30rpx;
position: relative;
height: calc(100% - 70rpx);
}
.offer-item .title .fl {
display: inline-block;
width: 78%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 30rpx;
color: #333;
}
.offer-item .title .fr {
display: inline-block;
width: 20rpx;
height: 26rpx;
margin-top: 5rpx;
}
.offer-item .note {
position: absolute;
left: 30rpx;
bottom: 35rpx;
width: calc(100vw - 60rpx);
font-size: 24rpx;
color: #999;
}
.offer-item .posit {
width: 200rpx;
height: 150rpx;
line-height: 150rpx;
text-align: center;
display: none
}
.posit.isMove {
display: inline-block;
position: absolute;
}
.posit.isMove[hidden] {
display: none
}
.offer-item .posit>view {
display: inline-block;
width: 100rpx;
}
.offer-item .posit>view:first-of-type {
background-color: #FED847;
}
.offer-item .posit>view:last-of-type {
background-color: #C71B1B;
}
.offer-item .posit image {
display: inline-block;
width: 36rpx;
height: 36rpx;
}
js:
let len = 0; // 初次加載長度
let hadLastPage = false; // 判斷是否到最后一頁
var initdata = function (that) {
var list = that.data.offerList
for (var i = 0; i < list.length; i++) {
list[i].txtStyle = "";
list[i].isMove = false;
}
that.setData({
offerList: list
})
}
Page({
data: {
offerList: [
],
delBtnWidth: 100, // 刪除按鈕寬度單位(rpx)
},
// 手指剛放到屏幕觸發(fā)
touchS: function (e) {
console.log("touchS" + e);
// initdata(this);
// 判斷是否只有一個觸摸點(diǎn)
if (e.touches.length == 1) {
this.setData({
// 記錄觸摸起始位置的X坐標(biāo)
startX: e.touches[0].clientX
});
};
return false;
},
// 觸摸時觸發(fā),手指在屏幕上每移動一次,觸發(fā)一次
touchM: function (e) {
var that = this;
initdata(that);
if (e.touches.length == 1) {
// 記錄觸摸點(diǎn)位置的X坐標(biāo)
var moveX = e.touches[0].clientX;
// 計算手指起始點(diǎn)的X坐標(biāo)與當(dāng)前觸摸點(diǎn)的X坐標(biāo)的差值
var disX = that.data.startX - moveX;
// delBtnWidth 為右側(cè)按鈕區(qū)域的寬度
var delBtnWidth = that.data.delBtnWidth;
var txtStyle = "";
if (disX == 0 || disX < 0) { // 如果移動距離小于等于0,文本層位置不變
txtStyle = "left:0px";
} else if (disX > 0) { // 移動距離大于0,文本層left值等于手指移動距離
txtStyle = "left:-" + disX + "px";
if (disX >= delBtnWidth) {
// 控制手指移動距離最大值為刪除按鈕的寬度
txtStyle = "left:-" + delBtnWidth + "px";
}
}
// 獲取手指觸摸的是哪一個item
var index = e.currentTarget.dataset.index;
var list = that.data.offerList;
// 將拼接好的樣式設(shè)置到當(dāng)前item中
list[index].txtStyle = txtStyle;
list[index].isMove = true;
// 更新列表的狀態(tài)
this.setData({
offerList: list
});
}
},
touchE: function (e) {
console.log( e);
var that = this
if (e.changedTouches.length == 1) {
// 手指移動結(jié)束后觸摸點(diǎn)位置的X坐標(biāo)
var endX = e.changedTouches[0].clientX;
// 觸摸開始與結(jié)束,手指移動的距離
var disX = that.data.startX - endX;
var delBtnWidth = that.data.delBtnWidth;
// 如果距離小于刪除按鈕的1/2,不顯示刪除按鈕
var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
// 獲取手指觸摸的是哪一項
var index = e.currentTarget.dataset.index;
var list = that.data.offerList;
list[index].txtStyle = txtStyle;
// 更新列表的狀態(tài)
that.setData({
offerList: list
});
}
},
/**
*
*/
navigatorTo: function (event) {
},
/**
* 刪除操作
*/
del: function (e) {
var that = this;
var data = {
id: e.currentTarget.dataset.index
};
wx.showModal({
title: '',
content: '確定選擇刪除么?',
confirmColor: '#C71B1B',
cancelColor: '#666666',
success: function (res) {
if (res.confirm) {
utils.requestFun("接口url", data, 'POST', function (msg) {
console.log(msg)
wx.showToast({
title: '刪除成功',
icon: 'success',
duration: 1500
})
var lists = that.data.offerList;
var list1 = [];
for (let i = 0; i < lists.length; i++) {
if (lists[i].id != e.currentTarget.dataset.index) {
list1.push(lists[i])
}
}
len--;
that.setData({
offerList: list1
})
})
} else if (res.cancel) {
}
}
})
},
/**
* 修改操作
*/
ref: function (e) {
wx.navigateTo({
url: '修改頁面路徑?id=' + e.currentTarget.dataset.index,
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
page = 0;
this.loadList();
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload: function () {
hadLastPage = false;
len = 0;
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function (event) {
console.log("上拉事件")
this.loadList();
},
/**
* 數(shù)據(jù)請求封裝
*/
loadList: function (event) {
if (hadLastPage != false) {
wx.showToast({
title: '到底啦',
});
return;
}
var that = this;
// 顯示加載圖標(biāo)
wx.showLoading({
title: '玩命加載中',
})
let data = {
length: len,
openId: 'openid'
};
utils.requestFun("接口url", data, 'POST', function (msg) {
if (msg.data.length != 0) {
var lists = that.data.offerList;
for (let i = 0; i < msg.data.length; i++) {
msg.data[i].isMove = false;
lists.push(msg.data[i]);
}
// len
len = len + msg.data.length;
// 設(shè)置數(shù)據(jù)
that.setData({
offerList: lists
})
} else {
hadLastPage = true;
}
wx.hideLoading();
})
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)的車標(biāo)圖片提示效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)的車標(biāo)圖片提示效果代碼,涉及JavaScript鼠標(biāo)事件觸發(fā)頁面元素遍歷修改的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
mpvue實(shí)現(xiàn)微信小程序快遞單號查詢代碼
這篇文章主要介紹了mpvue實(shí)現(xiàn)微信小程序快遞單號查詢,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
JS實(shí)現(xiàn)勻速運(yùn)動的代碼實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)勻速運(yùn)動的代碼實(shí)例,有需要的朋友可以參考一下2013-11-11
3種js實(shí)現(xiàn)string的substring方法
這篇文章主要介紹了3種javascript實(shí)現(xiàn)string的substring方法,需要的朋友可以參考下2015-11-11
詳解ES6 Promise對象then方法鏈?zhǔn)秸{(diào)用
這篇文章主要介紹了詳解ES6 Promise對象then方法鏈?zhǔn)秸{(diào)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
JS監(jiān)聽組合按鍵思路及實(shí)現(xiàn)過程
這篇文章主要介紹了JS監(jiān)聽組合按鍵思路及實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
js獲取UserControl內(nèi)容為拼html時提供方便
js獲取UserControl內(nèi)容時無法測試通過,原來是繼承了Page 然后使用VerifyRenderingInServerForm驗(yàn)證2014-11-11

