[Improvement] add touch mixin (#869)

This commit is contained in:
neverland
2018-04-13 15:26:41 +08:00
committed by GitHub
parent 7285632c35
commit bd80fe4e05
7 changed files with 66 additions and 100 deletions

25
packages/mixins/touch.js Normal file
View File

@@ -0,0 +1,25 @@
export default {
methods: {
touchStart(event) {
this.direction = '';
this.deltaX = 0;
this.deltaY = 0;
this.offsetX = 0;
this.offsetY = 0;
this.startX = event.touches[0].clientX;
this.startY = event.touches[0].clientY;
},
touchMove(event) {
const touch = event.touches[0];
this.deltaX = touch.clientX - this.startX;
this.deltaY = touch.clientY - this.startY;
this.offsetX = Math.abs(this.deltaX);
this.offsetY = Math.abs(this.deltaY);
if (!this.direction) {
this.direction = this.offsetX > this.offsetY ? 'horizontal' : this.offsetX < this.offsetY ? 'vertical' : '';
}
}
}
};