<template>
<view>
<view class="box-slideLeft">
<view v-if="flag" class="touch-item touch-slideLeft " @touchstart="touchS" @touchmove="touchM" @touchend="touchE"
:style="item_show.txtStyle">
<slot />
</view>
<view v-else class="touch-item touch-slideLeft" @touchstart="touchS" @touchmove.stop="touchM" @touchend="touchE"
:style="item_show.txtStyle">
<slot />
</view>
<view class="touch-item del-box-touch-slideLeft cf-shuCenter" @click="delItem(item_show)">
<view class="iconfont icon-shanchu"></view>
</view>
</view>
</view>
</template>
<script>
export default {
components: {
},
props: {
data_transit: {
type: Object,
default() {
return {}
}
},
item: {
type: Object,
default() {
return {}
}
},
},
computed: {
},
data() {
return {
item_show: {},
delBtnWidth: 60,
startX: '',
flag: true,
onoff: false
};
},
created: function () {
let that = this;
let item = that.item;
if (item && !item.hasOwnProperty("txtStyle")) {
this.$set(this.item, 'txtStyle', '');
}
this.item_show = this.item;
},
watch: {
item(e) {
this.item_show = e;
},
},
methods: {
delItem: function (e) {
let that = this;
let data = {
item: e,
data: that.data_transit,
};
this.$emit('delItem', data);
},
touchS: function (e) {
let that = this;
if (e.touches.length == 1) {
this.startX = e.touches[0].clientX;
this.startY = e.touches[0].clientY;
}
},
touchM: function (e) {
let that = this;
if (e.touches.length == 1) {
var moveX = e.touches[0].clientX;
var moveY = e.touches[0].clientY;
var disX = this.startX - moveX;
var delBtnWidth = this.delBtnWidth;
var txtStyle = "";
var angel = this.angel({ X: this.startX, Y: this.startY }, { X: moveX, Y: moveY })
if (this.onoff) return;
if (Math.abs(angel) > 30) {
this.onoff = true
return;
};
this.flag = false;
if (disX == 0 || disX < 0) {
txtStyle = "left:0px";
} else if (disX > 0) {
txtStyle = "left:-" + disX + "px";
if (disX >= delBtnWidth) {
txtStyle = "left:-" + delBtnWidth + "px";
}
}
that.$set(that.item_show, 'txtStyle', txtStyle)
}
},
touchE: function (e) {
let that = this;
if (e.changedTouches.length == 1) {
if (!this.onoff) {
var endX = e.changedTouches[0].clientX;
var disX = this.startX - endX;
var delBtnWidth = this.delBtnWidth;
var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
that.item_show.txtStyle = txtStyle;
}
that.flag = true;
that.onoff = false;
}
},
angel(start, end) {
var _X = end.X - start.X,
_Y = end.Y - start.Y;
return Math.abs(360 * Math.atan(_Y / _X) / (2 * Math.PI))
},
}
}
</script>
<style lang="scss">
@import "./iconfont.css";
.box-slideLeft {
view {
box-sizing: border-box;
}
position: relative;
overflow: hidden;
.touch-item {
position: absolute;
top: 0;
background-color: #ffffff;
overflow: hidden;
}
.touch-slideLeft {
position: relative;
width: 100%;
z-index: 5;
transition: left 0.2s ease-in-out;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.del-box-touch-slideLeft {
right: 0;
float: left;
width: 70px;
height: 100%;
line-height: 101px;
background-color: #ea5863;
border-radius: 0 10px 10px 0;
color: #fff;
font-size: 18px;
font-weight: lighter;
text-align: center;
}
.icon-shanchu {
font-size: 44upx;
}
.cf-shuCenter {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
}
</style>