uniapp左滑删除
欢迎使用ay-operate插件
最近有需求为:左滑删除,整理插件代码如下:
1.ay-operate插件
可去uniapp插件市场的操作组件:左滑删除,自定义任意内容插件
页面下载.
属性
属性 | 类型 | 说明 |
---|
data_transit | Object | 需要传递数据的对象 |
item | Object | 显示对象,可不传参 |
下面是插件的 代码片
.
<template>
<view>
<view class="box-slideLeft" >
<view class="touch-item touch-slideLeft " @touchstart="touchS" @touchmove="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: {
item: {
type: Object,
default () {
return {}
}
},
data_transit: {
type: Object,
default () {
return {}
}
},
},
computed: {
},
data() {
return {
item_show : {},
delBtnWidth: 60,
startX: '',
};
},
created:function(){
let that = this ;
let item = that.item ;
if(!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
}
},
touchM: function(e) {
let that = this;
if (e.touches.length == 1) {
var moveX = e.touches[0].clientX;
var disX = this.startX - moveX;
var delBtnWidth = this.delBtnWidth;
var txtStyle = "";
if (disX == 0 || disX < 0) {
txtStyle = "left:0px";
} else if (disX > 0) {
txtStyle = "left:-" + disX + "px";
if (disX >= delBtnWidth) {
txtStyle = "left:-" + delBtnWidth + "px";
}
}
that.item_show.txtStyle = txtStyle;
}
},
touchE: function(e) {
let that = this;
if (e.changedTouches.length == 1) {
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;
}
},
}
}
</script>
<style lang="scss">
@import './iconfont.css';
.box-slideLeft {
view {
box-sizing: border-box;
}
position: relative;
overflow: hidden;
.touch-item {
position: absolute;
top: 0;
padding: 10px 10px 10px;
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>
2.引用插件
可去uniapp插件市场的操作组件:左滑删除,自定义任意内容插件
页面,下载项目
项目结构如下:
下面index.vue
是引用插件的 代码片
.
<template>
<view>
<view v-for="(item,index) in result" :key="index" >
<delSlideLeft :item="item" :data_transit="{index:index,item:item}" @delItem="delItem">
<view style="color: #000000;">
<view >
<view >{{item.name}}</view>
<view >{{item.name}}</view>
</view>
<view >
<view >{{item.name}}</view>
</view>
</view>
</delSlideLeft>
</view>
</view>
</template>
<script>
import delSlideLeft from '@/components/ay-operate/del_slideLeft.vue'
export default {
components: {
delSlideLeft,
},
data() {
return {
result: [{
name: '左滑删除11111111111',
}, {
name: '左滑删除22222222222',
},
{
name: '左滑删除333333333',
}, ],
};
},
methods: {
delItem: function(e) {
let that = this;
that.result.splice(e.data.index,1)
},
}
}
</script>
<style lang="scss">
</style>
效果图