小程式wepy踩坑之旅(三)----- 微信小程式wepy左滑刪除特效原始碼
阿新 • • 發佈:2018-11-12
我寫在了shop_cart.wepy裡,原始碼就在下面註釋很詳細,直接拷貝到新建的.wpy就可以使用
<template>
<view class="item-box">
<view class="items">
<repeat for="{{list}}" key="item">
<view class="item">
<view @touchstart="ts" @touchmove="tm" @touchend="te" data-index="{{index }}" class="inner txt" style="{{item.txtStyle}}">{{item.txt}}</view>
<view class="inner del" data-index="{{index}}" @tap="del">刪除</view>
</view>
</repeat>
</view>
</view>
</template>
<script>
import wepy from 'wepy'
export default class ShopCart extends wepy.page {
config = {
navigationBarTitleText: '購物車'
};
components = {
};
mixins = [];
data = {
list: [
{
txtStyle:"",
txt:"向左滑動可以刪除"
},
{
txtStyle:"",
txt:"微信小程式|聯盟(wxapp-union.com)"
},
{
txtStyle:"" ,
txt:"聖誕老人是爸爸,順著煙囪往下爬,禮物塞滿聖誕襪,平安糖果一大把"
},
{
txtStyle:"",
txt:"聖誕到來,元旦還會遠嗎?在聖誕這個日子裡"
},
{
txtStyle:"",
txt:"聖誕節(Christmas或Cristo Messa ),譯名為“基督彌撒”。"
},
{
txtStyle:"",
txt:"一年一度的聖誕節即將到來,姑娘們也紛紛開始跑趴了吧!"
},
{
txtStyle:"",
txt:"聖誕節(Christmas或Cristo Messa ),譯名為“基督彌撒”。"
},
{
txtStyle:"",
txt:"你的聖誕節禮物準備好了嗎?"
},
{
txtStyle:"",
txt:"一年一度的聖誕節即將到來,姑娘們也紛紛開始跑趴了吧!"
},
{
txtStyle:"",
txt:"聖誕到來,元旦還會遠嗎?"
},
{
txtStyle:"",
txt:"記下這一刻的心情"
},
],
delBtnWidth: 180 //單位rpx
}
computed = {}
methods = {
ts (e) { // 觸控開始
let that = this
if (e.touches.length === 1) {
that.startX = e.touches[0].clientX
console.log('startX---'+that.startX)
}
},
tm (e) { // 觸控過程
let that = this
if (e.touches.length === 1) {
//手指移動方向水平
let moveX = e.touches[0].clientX // 這裡的clientX獲取的是螢幕可視區的座標,其實是邏輯畫素px,所以要用getEleWidth方法進行換算
console.log('moveX---'+moveX)
//手指起始點位置與移動期間的產值
let disX = that.startX - moveX
let txtStyle = ""
if (disX === 0 || disX < 0) { // 往右移動或者沒移動
txtStyle = "left: 0px"
}else if (disX > 0) { // 移動距離大於0
txtStyle = "left:-" + disX + "px"
if (disX >= that.delBtnWidth) { // 移動超過刪除按鈕的寬度
txtStyle = "left:-" + that.delBtnWidth + "px"
}
}
//獲取手指觸控的是哪一項
let index = e.target.dataset.index
that.list[index].txtStyle = txtStyle
}
},
te (e) { // 觸控結束
let that = this
if (e.changedTouches.length === 1) {
//手指移動結束後水平位置
let endX = e.changedTouches[0].clientX
//觸控開始與結束,是指移動的距離
let disX = that.startX - endX
let delBtnWidth = that.delBtnWidth
//如果距離小於刪除按鈕的1/2,不顯示刪除按鈕
let txtStyle = disX > delBtnWidth / 2 ? 'left:-'+ delBtnWidth + 'px': 'left:0px'
//手指觸控的是哪一項
let index = e.target.dataset.index
that.list[index].txtStyle = txtStyle
}
},
del () { // 刪除
}
}
events = {}
initEleWidth () {
let that = this
that.delBtnWidth = that.getEleWidth(that.delBtnWidth)
console.log(that.delBtnWidth)
}
getEleWidth (w) { //獲取元素自適應後的實際寬度(也就是根據設計稿寬度換算成px畫素)
let real = 0
try {
let resWidth = wx.getSystemInfoSync().windowWidth
let scale = 750 / w
real = Math.floor(resWidth / scale)
return real
}catch (e) {
return false
}
}
onLoad() {
this.initEleWidth()
}
}
</script>
<style>
view {
box-sizing: border-box;
}
.item-box {
margin: 0 auto;
padding: 40rpx 0;
}
.items {
width: 100%;
}
.item {
position: relative;
border-top: 2rpx solid#eee;
height: 120rpx;
line-height: 120rpx;
overflow: hidden;
}
.item:last-child {
border-bottom: 2rpx solid#eee;
}
.inner {
position: absolute;
top: 0;
}
.inner.txt {
background-color: #fff;
width: 100%;
z-index: 5;
padding: 0 10rpx;
transition: left 0.2s ease-in-out;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.inner.del {
background-color: #e64340;
width: 180rpx;
text-align: center;
z-index: 4;
right: 0;
color: #fff;
}
.item-icon {
width: 64rpx;
vertical-align: middle;
margin-right: 16rpx;
}
</style>