微信小程序?qū)崿F(xiàn)左滑動刪除效果
最近做微信小程序項目遇到左滑動效果,比如在我的收藏頁面,我的歷史瀏覽記錄頁面,我的購物車頁面,大多數(shù)都會用到這種效果,我把代碼復(fù)制出來,供大家參考,用的時候直接用模板,再此基礎(chǔ)上修改就行。
wxml中的代碼:
<view class="touch-item {{item.isTouchMove ? 'touch-move-active' : ''}}" data-index=" {{index}}" bindtouchstart="touchstart" bindtouchmove="touchmove" wx:for="{{items}}" wx:key="">
<view class="content">
<view class='com'>
<view class='tp'>
<image src="{{item.image_src}}" class='img' />
</view>
<view class='txt'>
<view class='tit'>{{item.goods_name}}</view>
<view class='bot'>
<block wx:if="{{item.marketable=='true'}}">
<view class="pri">
<text class="new-price">¥{{item.goods_price}}</text>
<text class="old-price">¥{{item.mktprice}}</text>
</view>
<navigator class='a'>
<label class='ti1'>可使用優(yōu)惠券</label>
</navigator>
</block>
<block wx:else>
<navigator class='a'>
<label class='ti'>對不起該商品已下架</label>
</navigator>
</block>
</view>
</view>
</view>
</view>
<view class="del" catchtap="del" data-index="{{index}}" data-productid="{{item.product_id}}" data-goodsid="{{item.goods_id}}">刪除</view>
</view>
我這是對完接口之后的代碼,循環(huán)items,然后用{{item.}}取到下面的值,并且用了一個判斷,如果后臺返回來的字段值marketable==‘true',讓其顯示商品的銷售價和原價,否則顯示該商品已下架。
js中的代碼:
data: {
startX: 0, //開始坐標(biāo)
startY: 0
},
touchstart: function(e) {
//開始觸摸時 重置所有刪除
this.data.items.forEach(function(v, i) {
if (v.isTouchMove) //只操作為true的
v.isTouchMove = false;
})
this.setData({
startX: e.changedTouches[0].clientX,
startY: e.changedTouches[0].clientY,
items: this.data.items
})
},
//滑動事件處理
touchmove: function(e) {
var that = this,
index = e.currentTarget.dataset.index, //當(dāng)前索引
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.items.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({
items: that.data.items
})
},
/**
* 計算滑動角度
* @param {Object} start 起點坐標(biāo)
* @param {Object} end 終點坐標(biāo)
*/
angle: function(start, end) {
var _X = end.X - start.X,
_Y = end.Y - start.Y
//返回角度 /Math.atan()返回數(shù)字的反正切值
return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
},
wxml中的代碼:
.touch-item {
background-color: #fff;
margin-top: 20rpx;
display: flex;
justify-content: space-between;
width: 100%;
overflow: hidden;
box-sizing: border-box;
}
.content {
width: 100%;
-webkit-transition: all 0.4s;
transition: all 0.4s;
-webkit-transform: translateX(180rpx);
transform: translateX(180rpx);
margin-left: -180rpx;
}
.touch-move-active .content, .touch-move-active .del {
-webkit-transform: translateX(0);
transform: translateX(0);
}
.com {
padding: 25rpx;
height: 210rpx;
}
.tp {
background-color: white;
float: left;
}
.img {
width: 210rpx;
height: 210rpx;
display: inline-block;
vertical-align: middle;
border-radius: 15rpx;
}
.txt {
width: 420rpx;
margin-left: 270rpx;
position: relative;
}
.txt .tit {
font-size: 28rpx;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
.txt .bot {
width: 100%;
font-size: 24rpx;
margin-top: 20rpx;
}
.ti1 {
margin-top: 15rpx;
font-size: 23rpx;
background-color: #fce64c;
padding: 10rpx;
display: inline-block;
}
.ti {
margin-top: 35rpx;
font-size: 28rpx;
display: inline-block;
color: #a2a2a2;
}
.del {
background-color: red;
width: 180rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #fff;
-webkit-transform: translateX(180rpx);
transform: translateX(180rpx);
-webkit-transition: all 0.4s;
transition: all 0.4s;
}
.new-price {
font-weight: 600;
color: #ff503c;
font-size: 35rpx;
}
.old-price {
margin-left: 30rpx;
text-decoration: line-through;
font-size: 28rpx;
color: #b1b1b1;
}
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java通過WebSocket實現(xiàn)異步導(dǎo)出解決思路
這篇文章主要介紹了通過WebSocket實現(xiàn)異步導(dǎo)出,本篇文章記錄大批量數(shù)據(jù)導(dǎo)出時間過長,導(dǎo)致接口請求超時問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01
JavaScript find()方法及返回數(shù)據(jù)實例
這篇文章主要介紹了JavaScript中的find()方法和返回數(shù)據(jù)的內(nèi)存指向,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
js中offset,client , scroll 三大元素知識點總結(jié)
在本篇文章里小編給大家整理了關(guān)于js 元素offset,client , scroll 三大系列總結(jié),有需要的朋友們可以學(xué)習(xí)下。2019-09-09
JavaScript 判斷iPhone X Series機型的方法
這篇文章主要介紹了JavaScript 判斷iPhone X Series機型的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

