[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

View File

@@ -23,10 +23,13 @@
<script>
import create from '../utils/create';
import Touch from '../mixins/touch';
export default create({
name: 'swipe',
mixins: [Touch],
props: {
autoplay: Number,
loop: {
@@ -121,11 +124,8 @@ export default create({
onTouchStart(event) {
clearTimeout(this.timer);
this.deltaX = 0;
this.direction = '';
this.currentDuration = 0;
this.startX = event.touches[0].clientX;
this.startY = event.touches[0].clientY;
this.touchStart(event);
if (this.active <= -1) {
this.move(this.count);
@@ -136,19 +136,18 @@ export default create({
},
onTouchMove(event) {
this.direction = this.direction || this.getDirection(event.touches[0]);
this.touchMove(event);
if (this.direction === 'horizontal') {
event.preventDefault();
event.stopPropagation();
this.deltaX = event.touches[0].clientX - this.startX;
this.move(0, this.range(this.deltaX, [-this.width, this.width]));
}
},
onTouchEnd() {
if (this.deltaX) {
this.move(Math.abs(this.deltaX) > 50 ? (this.deltaX > 0 ? -1 : 1) : 0);
this.move(this.offsetX > 50 ? (this.deltaX > 0 ? -1 : 1) : 0);
this.currentDuration = this.duration;
}
this.autoPlay();
@@ -202,12 +201,6 @@ export default create({
}
},
getDirection(touch) {
const offsetX = Math.abs(touch.clientX - this.startX);
const offsetY = Math.abs(touch.clientY - this.startY);
return offsetX > offsetY ? 'horizontal' : offsetX < offsetY ? 'vertical' : '';
},
range(num, arr) {
return Math.min(Math.max(num, arr[0]), arr[1]);
}