Skip to content

Commit

Permalink
add swipe left/right gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvisniu committed Jun 15, 2019
1 parent ad1d7e3 commit 2e8b506
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-zoomer",
"version": "0.3.0",
"version": "0.3.1",
"description": "Zoom the image or other thing with mouse or touch",
"main": "dist/vue-zoomer.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion src/vue-zoomer-gallery.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class="vue-zoomer-gallery"
@mousemove="onMouseMove"
@mousedown="onMouseDown"
@mouseout="isPointerDown = false"
@mouseout="onMouseUp"
@mouseup="onMouseUp"
@touchstart="onTouchStart"
@touchend="onTouchEnd"
Expand All @@ -29,6 +29,7 @@
:aspect-ratio="imageAspectRatios[selIndex + i - 1] || 1"
:pivot="pivot"
:limit-translation="limitTranslation"
@swipe="onImageSwipe"
>
<img
v-if="selIndex + i - 1 > -1 && selIndex + i - 1 < list.length"
Expand Down Expand Up @@ -192,6 +193,9 @@ export default {
let aspectRatio = ev.target.naturalWidth / ev.target.naturalHeight
this.$set(this.imageAspectRatios, index, aspectRatio)
},
onImageSwipe (direction) {
this.paginate(direction == 'right' ? -1 : 1)
},
},
}
</script>
Expand Down
18 changes: 16 additions & 2 deletions src/vue-zoomer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export default {
animScale: 1,
// Mouse states
lastFullWheelTime: 0,
lastWheelTime: 0,
lastWheelDirection: 'x',
isPointerDown: false,
pointerPosX: -1,
pointerPosY: -1,
Expand Down Expand Up @@ -234,16 +236,25 @@ export default {
// Mouse Events ------------------------------------------------------------
// Mouse wheel scroll, TrackPad pinch or TrackPad scroll
onMouseWheel (ev) {
let currTime = Date.now()
if (Math.abs(ev.wheelDelta) === 120) {
// Throttle the TouchPad pinch on Mac, or it will be too sensitive
let currTime = Date.now()
if (currTime - this.lastFullWheelTime > 50) {
this.onMouseWheelDo(ev)
this.lastFullWheelTime = currTime
}
} else {
this.onMouseWheelDo(ev)
if (currTime - this.lastWheelTime > 50 && typeof ev.deltaX === 'number') {
this.lastWheelDirection = Math.abs(ev.deltaX) > Math.abs(ev.deltaY) ? 'x' : 'y'
if (this.lastWheelDirection === 'x') {
this.$emit('swipe', ev.deltaX > 0 ? 'left' : 'right')
}
}
if (this.lastWheelDirection === 'y') {
this.onMouseWheelDo(ev)
}
}
this.lastWheelTime = currTime
},
onMouseWheelDo (ev) {
// Value basis: One mouse wheel (wheelDelta=+-120) means 1.25/0.8 scale.
Expand All @@ -252,6 +263,9 @@ export default {
this.tryToScale(scaleDelta)
this.onInteractionEnd()
},
onScroll (ev) {
console.log(2)
},
onMouseDown (ev) {
this.refreshContainerPos()
this.isPointerDown = true
Expand Down

0 comments on commit 2e8b506

Please sign in to comment.