vue滑動刪除元件
阿新 • • 發佈:2018-12-09
export default {
name: 'pa-movedelete',
data() {
return {
startX: 0,
moveX: 0,
disX: 0,
isOpen: false,
btnWidth: 0,
isDisabled: false
};
},
props: {
disabled: {
type: Boolean,
default: void 0
},
data: {}
},
watch: {
disabled(val) {
this .isDisabled = val;
}
},
methods: {
touchStart(ev) {
this.startX = ev.touches[0].clientX;
},
touchMove(ev) {
if (this.isDisabled) {
return;
};
this.isOpen = false;
this.moveX = ev.touches[0].clientX;
this.disX = this.moveX - this.startX;
if (this.disX > 0) {
this.$refs.wrap.style.marginLeft = '-' + 0 + 'px';
} else {
let l = Math.abs(this.disX);
if (l > this.btnWidth / 2) {
l = this.btnWidth;
this.isOpen = true;
}
this.$refs.wrap.style.marginLeft = '-' + l + 'px';
}
},
touchEnd(ev) {
if (!this.isOpen) {
this.$refs.wrap.style.marginLeft = '-' + 0 + 'px';
}
},
close(ev) {
if (this.isOpen) {
this.$refs.wrap.style.marginLeft = '-' + 0 + 'px';
this.isOpen = false;
}
},
deleteHander() {
this.$emit('deleteHander', [this, this.data]);
}
},
mounted() {
this.btnWidth = this.$refs.btn.clientWidth;
this.$on('movedelete.refreshStatus', (disabled) => {
this.isDisabled = disabled;
});
}
};