html
<template>
<view class="content-1">
<view class="container-1" @touchstart="touchS" @touchmove="touchM" @touchend="touchE"
:style="{'left':act_touch==index?leftStyle + 'upx':0}" :data-index="index" v-for="(i, index) in 3">
<view>
左滑删除左滑删除左滑删除左滑删除
</view>
<view class="delete-1" @click="delData">
删除
</view>
</view>
</view>
</template>
JavaScript
<script>
export default {
data() {
return {
leftStyle: 0,
startX: 0,
hiddenFlag: true,
delBtnWidth: 220,
act_touch: null,
}
},
onLoad() {
},
onShow() {
},
methods: {
touchS({
touches
}) {
if (touches.length === 1) {
this.startX = touches[0].clientX;
}
if (this.leftStyle === 0) {
this.hiddenFlag = true;
} else {
this.hiddenFlag = false;
}
},
touchM(e) {
if (e.touches.length === 1) {
const moveX = e.touches[0].clientX;
this.moveFunc(moveX);
console.log(e.currentTarget.dataset.index)
var index = e.currentTarget.dataset.index;
this.act_touch = index;
}
},
touchE({
touches
}) {
const {
leftStyle
} = this;
const {
delBtnWidth
} = this;
if (-leftStyle > delBtnWidth / 2) {
this.leftStyle = -delBtnWidth;
} else {
this.leftStyle = 0;
}
},
moveFunc(moveX) {
const disX = moveX - this.startX;
const delBtnWidth = this.delBtnWidth;
let offsetNum = 0;
if (-disX >= delBtnWidth && this.leftStyle === -delBtnWidth) {
return;
}
console.log(disX, this.hiddenFlag);
if (this.hiddenFlag) {
if (disX == 0 || disX > 0) {
offsetNum = 0;
} else {
offsetNum = disX;
if (disX <= -delBtnWidth) {
offsetNum = -delBtnWidth;
}
}
} else {
if (disX < 0) {
offsetNum = -this.delBtnWidth;
} else {
offsetNum = -this.delBtnWidth + disX;
if (offsetNum > 0) {
offsetNum = 0;
}
}
}
this.leftStyle = offsetNum;
},
delData() {
console.log("删除")
uni.showModal({
title: '提示',
content: "确认删除该职位?",
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
},
}
}
</script>
css
<style lang="scss">
.content-1 {
width: 100%;
overflow-x: hidden;
background-color: #f4f4f4;
border-radius: none;
}
.container-1 {
position: relative;
margin-top: 26rpx;
margin-bottom: 20upx;
background-color: #fff;
// padding: 20upx;
height: 120rpx;
line-height: 120rpx;
}
.delete-1 {
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
right: -107px;
width: 65px;
height: 120rpx;
line-height: 120rpx;
font-weight: 500;
font-size: 16px;
text-align: center;
color: #FFFFFF;
background: #FF1C1C;
width: 106px;
}
</style>