Browse code

Remove old version 5

Benjamin Roth authored on14/03/2021 15:27:00
Showing1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,561 +0,0 @@
1
-import $ from '../../utils/dom';
2
-import Utils from '../../utils/utils';
3
-import Device from '../../utils/device';
4
-import Support from '../../utils/support';
5
-
6
-const Zoom = {
7
-  // Calc Scale From Multi-touches
8
-  getDistanceBetweenTouches(e) {
9
-    if (e.targetTouches.length < 2) return 1;
10
-    const x1 = e.targetTouches[0].pageX;
11
-    const y1 = e.targetTouches[0].pageY;
12
-    const x2 = e.targetTouches[1].pageX;
13
-    const y2 = e.targetTouches[1].pageY;
14
-    const distance = Math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2));
15
-    return distance;
16
-  },
17
-  // Events
18
-  onGestureStart(e) {
19
-    const swiper = this;
20
-    const params = swiper.params.zoom;
21
-    const zoom = swiper.zoom;
22
-    const { gesture } = zoom;
23
-    zoom.fakeGestureTouched = false;
24
-    zoom.fakeGestureMoved = false;
25
-    if (!Support.gestures) {
26
-      if (e.type !== 'touchstart' || (e.type === 'touchstart' && e.targetTouches.length < 2)) {
27
-        return;
28
-      }
29
-      zoom.fakeGestureTouched = true;
30
-      gesture.scaleStart = Zoom.getDistanceBetweenTouches(e);
31
-    }
32
-    if (!gesture.$slideEl || !gesture.$slideEl.length) {
33
-      gesture.$slideEl = $(e.target).closest(`.${swiper.params.slideClass}`);
34
-      if (gesture.$slideEl.length === 0) gesture.$slideEl = swiper.slides.eq(swiper.activeIndex);
35
-      gesture.$imageEl = gesture.$slideEl.find('img, svg, canvas, picture, .swiper-zoom-target');
36
-      gesture.$imageWrapEl = gesture.$imageEl.parent(`.${params.containerClass}`);
37
-      gesture.maxRatio = gesture.$imageWrapEl.attr('data-swiper-zoom') || params.maxRatio;
38
-      if (gesture.$imageWrapEl.length === 0) {
39
-        gesture.$imageEl = undefined;
40
-        return;
41
-      }
42
-    }
43
-    if (gesture.$imageEl) {
44
-      gesture.$imageEl.transition(0);
45
-    }
46
-    swiper.zoom.isScaling = true;
47
-  },
48
-  onGestureChange(e) {
49
-    const swiper = this;
50
-    const params = swiper.params.zoom;
51
-    const zoom = swiper.zoom;
52
-    const { gesture } = zoom;
53
-    if (!Support.gestures) {
54
-      if (e.type !== 'touchmove' || (e.type === 'touchmove' && e.targetTouches.length < 2)) {
55
-        return;
56
-      }
57
-      zoom.fakeGestureMoved = true;
58
-      gesture.scaleMove = Zoom.getDistanceBetweenTouches(e);
59
-    }
60
-    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
61
-    if (Support.gestures) {
62
-      zoom.scale = e.scale * zoom.currentScale;
63
-    } else {
64
-      zoom.scale = (gesture.scaleMove / gesture.scaleStart) * zoom.currentScale;
65
-    }
66
-    if (zoom.scale > gesture.maxRatio) {
67
-      zoom.scale = (gesture.maxRatio - 1) + (((zoom.scale - gesture.maxRatio) + 1) ** 0.5);
68
-    }
69
-    if (zoom.scale < params.minRatio) {
70
-      zoom.scale = (params.minRatio + 1) - (((params.minRatio - zoom.scale) + 1) ** 0.5);
71
-    }
72
-    gesture.$imageEl.transform(`translate3d(0,0,0) scale(${zoom.scale})`);
73
-  },
74
-  onGestureEnd(e) {
75
-    const swiper = this;
76
-    const params = swiper.params.zoom;
77
-    const zoom = swiper.zoom;
78
-    const { gesture } = zoom;
79
-    if (!Support.gestures) {
80
-      if (!zoom.fakeGestureTouched || !zoom.fakeGestureMoved) {
81
-        return;
82
-      }
83
-      if (e.type !== 'touchend' || (e.type === 'touchend' && e.changedTouches.length < 2 && !Device.android)) {
84
-        return;
85
-      }
86
-      zoom.fakeGestureTouched = false;
87
-      zoom.fakeGestureMoved = false;
88
-    }
89
-    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
90
-    zoom.scale = Math.max(Math.min(zoom.scale, gesture.maxRatio), params.minRatio);
91
-    gesture.$imageEl.transition(swiper.params.speed).transform(`translate3d(0,0,0) scale(${zoom.scale})`);
92
-    zoom.currentScale = zoom.scale;
93
-    zoom.isScaling = false;
94
-    if (zoom.scale === 1) gesture.$slideEl = undefined;
95
-  },
96
-  onTouchStart(e) {
97
-    const swiper = this;
98
-    const zoom = swiper.zoom;
99
-    const { gesture, image } = zoom;
100
-    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
101
-    if (image.isTouched) return;
102
-    if (Device.android && e.cancelable) e.preventDefault();
103
-    image.isTouched = true;
104
-    image.touchesStart.x = e.type === 'touchstart' ? e.targetTouches[0].pageX : e.pageX;
105
-    image.touchesStart.y = e.type === 'touchstart' ? e.targetTouches[0].pageY : e.pageY;
106
-  },
107
-  onTouchMove(e) {
108
-    const swiper = this;
109
-    const zoom = swiper.zoom;
110
-    const { gesture, image, velocity } = zoom;
111
-    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
112
-    swiper.allowClick = false;
113
-    if (!image.isTouched || !gesture.$slideEl) return;
114
-
115
-    if (!image.isMoved) {
116
-      image.width = gesture.$imageEl[0].offsetWidth;
117
-      image.height = gesture.$imageEl[0].offsetHeight;
118
-      image.startX = Utils.getTranslate(gesture.$imageWrapEl[0], 'x') || 0;
119
-      image.startY = Utils.getTranslate(gesture.$imageWrapEl[0], 'y') || 0;
120
-      gesture.slideWidth = gesture.$slideEl[0].offsetWidth;
121
-      gesture.slideHeight = gesture.$slideEl[0].offsetHeight;
122
-      gesture.$imageWrapEl.transition(0);
123
-      if (swiper.rtl) {
124
-        image.startX = -image.startX;
125
-        image.startY = -image.startY;
126
-      }
127
-    }
128
-    // Define if we need image drag
129
-    const scaledWidth = image.width * zoom.scale;
130
-    const scaledHeight = image.height * zoom.scale;
131
-
132
-    if (scaledWidth < gesture.slideWidth && scaledHeight < gesture.slideHeight) return;
133
-
134
-    image.minX = Math.min(((gesture.slideWidth / 2) - (scaledWidth / 2)), 0);
135
-    image.maxX = -image.minX;
136
-    image.minY = Math.min(((gesture.slideHeight / 2) - (scaledHeight / 2)), 0);
137
-    image.maxY = -image.minY;
138
-
139
-    image.touchesCurrent.x = e.type === 'touchmove' ? e.targetTouches[0].pageX : e.pageX;
140
-    image.touchesCurrent.y = e.type === 'touchmove' ? e.targetTouches[0].pageY : e.pageY;
141
-
142
-    if (!image.isMoved && !zoom.isScaling) {
143
-      if (
144
-        swiper.isHorizontal()
145
-        && (
146
-          (Math.floor(image.minX) === Math.floor(image.startX) && image.touchesCurrent.x < image.touchesStart.x)
147
-          || (Math.floor(image.maxX) === Math.floor(image.startX) && image.touchesCurrent.x > image.touchesStart.x)
148
-        )
149
-      ) {
150
-        image.isTouched = false;
151
-        return;
152
-      } if (
153
-        !swiper.isHorizontal()
154
-        && (
155
-          (Math.floor(image.minY) === Math.floor(image.startY) && image.touchesCurrent.y < image.touchesStart.y)
156
-          || (Math.floor(image.maxY) === Math.floor(image.startY) && image.touchesCurrent.y > image.touchesStart.y)
157
-        )
158
-      ) {
159
-        image.isTouched = false;
160
-        return;
161
-      }
162
-    }
163
-    if (e.cancelable) {
164
-      e.preventDefault();
165
-    }
166
-    e.stopPropagation();
167
-
168
-    image.isMoved = true;
169
-    image.currentX = (image.touchesCurrent.x - image.touchesStart.x) + image.startX;
170
-    image.currentY = (image.touchesCurrent.y - image.touchesStart.y) + image.startY;
171
-
172
-    if (image.currentX < image.minX) {
173
-      image.currentX = (image.minX + 1) - (((image.minX - image.currentX) + 1) ** 0.8);
174
-    }
175
-    if (image.currentX > image.maxX) {
176
-      image.currentX = (image.maxX - 1) + (((image.currentX - image.maxX) + 1) ** 0.8);
177
-    }
178
-
179
-    if (image.currentY < image.minY) {
180
-      image.currentY = (image.minY + 1) - (((image.minY - image.currentY) + 1) ** 0.8);
181
-    }
182
-    if (image.currentY > image.maxY) {
183
-      image.currentY = (image.maxY - 1) + (((image.currentY - image.maxY) + 1) ** 0.8);
184
-    }
185
-
186
-    // Velocity
187
-    if (!velocity.prevPositionX) velocity.prevPositionX = image.touchesCurrent.x;
188
-    if (!velocity.prevPositionY) velocity.prevPositionY = image.touchesCurrent.y;
189
-    if (!velocity.prevTime) velocity.prevTime = Date.now();
190
-    velocity.x = (image.touchesCurrent.x - velocity.prevPositionX) / (Date.now() - velocity.prevTime) / 2;
191
-    velocity.y = (image.touchesCurrent.y - velocity.prevPositionY) / (Date.now() - velocity.prevTime) / 2;
192
-    if (Math.abs(image.touchesCurrent.x - velocity.prevPositionX) < 2) velocity.x = 0;
193
-    if (Math.abs(image.touchesCurrent.y - velocity.prevPositionY) < 2) velocity.y = 0;
194
-    velocity.prevPositionX = image.touchesCurrent.x;
195
-    velocity.prevPositionY = image.touchesCurrent.y;
196
-    velocity.prevTime = Date.now();
197
-
198
-    gesture.$imageWrapEl.transform(`translate3d(${image.currentX}px, ${image.currentY}px,0)`);
199
-  },
200
-  onTouchEnd() {
201
-    const swiper = this;
202
-    const zoom = swiper.zoom;
203
-    const { gesture, image, velocity } = zoom;
204
-    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
205
-    if (!image.isTouched || !image.isMoved) {
206
-      image.isTouched = false;
207
-      image.isMoved = false;
208
-      return;
209
-    }
210
-    image.isTouched = false;
211
-    image.isMoved = false;
212
-    let momentumDurationX = 300;
213
-    let momentumDurationY = 300;
214
-    const momentumDistanceX = velocity.x * momentumDurationX;
215
-    const newPositionX = image.currentX + momentumDistanceX;
216
-    const momentumDistanceY = velocity.y * momentumDurationY;
217
-    const newPositionY = image.currentY + momentumDistanceY;
218
-
219
-    // Fix duration
220
-    if (velocity.x !== 0) momentumDurationX = Math.abs((newPositionX - image.currentX) / velocity.x);
221
-    if (velocity.y !== 0) momentumDurationY = Math.abs((newPositionY - image.currentY) / velocity.y);
222
-    const momentumDuration = Math.max(momentumDurationX, momentumDurationY);
223
-
224
-    image.currentX = newPositionX;
225
-    image.currentY = newPositionY;
226
-
227
-    // Define if we need image drag
228
-    const scaledWidth = image.width * zoom.scale;
229
-    const scaledHeight = image.height * zoom.scale;
230
-    image.minX = Math.min(((gesture.slideWidth / 2) - (scaledWidth / 2)), 0);
231
-    image.maxX = -image.minX;
232
-    image.minY = Math.min(((gesture.slideHeight / 2) - (scaledHeight / 2)), 0);
233
-    image.maxY = -image.minY;
234
-    image.currentX = Math.max(Math.min(image.currentX, image.maxX), image.minX);
235
-    image.currentY = Math.max(Math.min(image.currentY, image.maxY), image.minY);
236
-
237
-    gesture.$imageWrapEl.transition(momentumDuration).transform(`translate3d(${image.currentX}px, ${image.currentY}px,0)`);
238
-  },
239
-  onTransitionEnd() {
240
-    const swiper = this;
241
-    const zoom = swiper.zoom;
242
-    const { gesture } = zoom;
243
-    if (gesture.$slideEl && swiper.previousIndex !== swiper.activeIndex) {
244
-      if (gesture.$imageEl) {
245
-        gesture.$imageEl.transform('translate3d(0,0,0) scale(1)');
246
-      }
247
-      if (gesture.$imageWrapEl) {
248
-        gesture.$imageWrapEl.transform('translate3d(0,0,0)');
249
-      }
250
-
251
-      zoom.scale = 1;
252
-      zoom.currentScale = 1;
253
-
254
-      gesture.$slideEl = undefined;
255
-      gesture.$imageEl = undefined;
256
-      gesture.$imageWrapEl = undefined;
257
-    }
258
-  },
259
-  // Toggle Zoom
260
-  toggle(e) {
261
-    const swiper = this;
262
-    const zoom = swiper.zoom;
263
-
264
-    if (zoom.scale && zoom.scale !== 1) {
265
-      // Zoom Out
266
-      zoom.out();
267
-    } else {
268
-      // Zoom In
269
-      zoom.in(e);
270
-    }
271
-  },
272
-  in(e) {
273
-    const swiper = this;
274
-
275
-    const zoom = swiper.zoom;
276
-    const params = swiper.params.zoom;
277
-    const { gesture, image } = zoom;
278
-
279
-    if (!gesture.$slideEl) {
280
-      if (swiper.params.virtual && swiper.params.virtual.enabled && swiper.virtual) {
281
-        gesture.$slideEl = swiper.$wrapperEl.children(`.${swiper.params.slideActiveClass}`);
282
-      } else {
283
-        gesture.$slideEl = swiper.slides.eq(swiper.activeIndex);
284
-      }
285
-      gesture.$imageEl = gesture.$slideEl.find('img, svg, canvas, picture, .swiper-zoom-target');
286
-      gesture.$imageWrapEl = gesture.$imageEl.parent(`.${params.containerClass}`);
287
-    }
288
-    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
289
-
290
-    gesture.$slideEl.addClass(`${params.zoomedSlideClass}`);
291
-
292
-    let touchX;
293
-    let touchY;
294
-    let offsetX;
295
-    let offsetY;
296
-    let diffX;
297
-    let diffY;
298
-    let translateX;
299
-    let translateY;
300
-    let imageWidth;
301
-    let imageHeight;
302
-    let scaledWidth;
303
-    let scaledHeight;
304
-    let translateMinX;
305
-    let translateMinY;
306
-    let translateMaxX;
307
-    let translateMaxY;
308
-    let slideWidth;
309
-    let slideHeight;
310
-
311
-    if (typeof image.touchesStart.x === 'undefined' && e) {
312
-      touchX = e.type === 'touchend' ? e.changedTouches[0].pageX : e.pageX;
313
-      touchY = e.type === 'touchend' ? e.changedTouches[0].pageY : e.pageY;
314
-    } else {
315
-      touchX = image.touchesStart.x;
316
-      touchY = image.touchesStart.y;
317
-    }
318
-
319
-    zoom.scale = gesture.$imageWrapEl.attr('data-swiper-zoom') || params.maxRatio;
320
-    zoom.currentScale = gesture.$imageWrapEl.attr('data-swiper-zoom') || params.maxRatio;
321
-    if (e) {
322
-      slideWidth = gesture.$slideEl[0].offsetWidth;
323
-      slideHeight = gesture.$slideEl[0].offsetHeight;
324
-      offsetX = gesture.$slideEl.offset().left;
325
-      offsetY = gesture.$slideEl.offset().top;
326
-      diffX = (offsetX + (slideWidth / 2)) - touchX;
327
-      diffY = (offsetY + (slideHeight / 2)) - touchY;
328
-
329
-      imageWidth = gesture.$imageEl[0].offsetWidth;
330
-      imageHeight = gesture.$imageEl[0].offsetHeight;
331
-      scaledWidth = imageWidth * zoom.scale;
332
-      scaledHeight = imageHeight * zoom.scale;
333
-
334
-      translateMinX = Math.min(((slideWidth / 2) - (scaledWidth / 2)), 0);
335
-      translateMinY = Math.min(((slideHeight / 2) - (scaledHeight / 2)), 0);
336
-      translateMaxX = -translateMinX;
337
-      translateMaxY = -translateMinY;
338
-
339
-      translateX = diffX * zoom.scale;
340
-      translateY = diffY * zoom.scale;
341
-
342
-      if (translateX < translateMinX) {
343
-        translateX = translateMinX;
344
-      }
345
-      if (translateX > translateMaxX) {
346
-        translateX = translateMaxX;
347
-      }
348
-
349
-      if (translateY < translateMinY) {
350
-        translateY = translateMinY;
351
-      }
352
-      if (translateY > translateMaxY) {
353
-        translateY = translateMaxY;
354
-      }
355
-    } else {
356
-      translateX = 0;
357
-      translateY = 0;
358
-    }
359
-    gesture.$imageWrapEl.transition(300).transform(`translate3d(${translateX}px, ${translateY}px,0)`);
360
-    gesture.$imageEl.transition(300).transform(`translate3d(0,0,0) scale(${zoom.scale})`);
361
-  },
362
-  out() {
363
-    const swiper = this;
364
-
365
-    const zoom = swiper.zoom;
366
-    const params = swiper.params.zoom;
367
-    const { gesture } = zoom;
368
-
369
-    if (!gesture.$slideEl) {
370
-      if (swiper.params.virtual && swiper.params.virtual.enabled && swiper.virtual) {
371
-        gesture.$slideEl = swiper.$wrapperEl.children(`.${swiper.params.slideActiveClass}`);
372
-      } else {
373
-        gesture.$slideEl = swiper.slides.eq(swiper.activeIndex);
374
-      }
375
-      gesture.$imageEl = gesture.$slideEl.find('img, svg, canvas, picture, .swiper-zoom-target');
376
-      gesture.$imageWrapEl = gesture.$imageEl.parent(`.${params.containerClass}`);
377
-    }
378
-    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
379
-
380
-    zoom.scale = 1;
381
-    zoom.currentScale = 1;
382
-    gesture.$imageWrapEl.transition(300).transform('translate3d(0,0,0)');
383
-    gesture.$imageEl.transition(300).transform('translate3d(0,0,0) scale(1)');
384
-    gesture.$slideEl.removeClass(`${params.zoomedSlideClass}`);
385
-    gesture.$slideEl = undefined;
386
-  },
387
-  // Attach/Detach Events
388
-  enable() {
389
-    const swiper = this;
390
-    const zoom = swiper.zoom;
391
-    if (zoom.enabled) return;
392
-    zoom.enabled = true;
393
-
394
-    const passiveListener = swiper.touchEvents.start === 'touchstart' && Support.passiveListener && swiper.params.passiveListeners ? { passive: true, capture: false } : false;
395
-    const activeListenerWithCapture = Support.passiveListener ? { passive: false, capture: true } : true;
396
-
397
-    const slideSelector = `.${swiper.params.slideClass}`;
398
-
399
-    // Scale image
400
-    if (Support.gestures) {
401
-      swiper.$wrapperEl.on('gesturestart', slideSelector, zoom.onGestureStart, passiveListener);
402
-      swiper.$wrapperEl.on('gesturechange', slideSelector, zoom.onGestureChange, passiveListener);
403
-      swiper.$wrapperEl.on('gestureend', slideSelector, zoom.onGestureEnd, passiveListener);
404
-    } else if (swiper.touchEvents.start === 'touchstart') {
405
-      swiper.$wrapperEl.on(swiper.touchEvents.start, slideSelector, zoom.onGestureStart, passiveListener);
406
-      swiper.$wrapperEl.on(swiper.touchEvents.move, slideSelector, zoom.onGestureChange, activeListenerWithCapture);
407
-      swiper.$wrapperEl.on(swiper.touchEvents.end, slideSelector, zoom.onGestureEnd, passiveListener);
408
-      if (swiper.touchEvents.cancel) {
409
-        swiper.$wrapperEl.on(swiper.touchEvents.cancel, slideSelector, zoom.onGestureEnd, passiveListener);
410
-      }
411
-    }
412
-
413
-    // Move image
414
-    swiper.$wrapperEl.on(swiper.touchEvents.move, `.${swiper.params.zoom.containerClass}`, zoom.onTouchMove, activeListenerWithCapture);
415
-  },
416
-  disable() {
417
-    const swiper = this;
418
-    const zoom = swiper.zoom;
419
-    if (!zoom.enabled) return;
420
-
421
-    swiper.zoom.enabled = false;
422
-
423
-    const passiveListener = swiper.touchEvents.start === 'touchstart' && Support.passiveListener && swiper.params.passiveListeners ? { passive: true, capture: false } : false;
424
-    const activeListenerWithCapture = Support.passiveListener ? { passive: false, capture: true } : true;
425
-
426
-    const slideSelector = `.${swiper.params.slideClass}`;
427
-
428
-    // Scale image
429
-    if (Support.gestures) {
430
-      swiper.$wrapperEl.off('gesturestart', slideSelector, zoom.onGestureStart, passiveListener);
431
-      swiper.$wrapperEl.off('gesturechange', slideSelector, zoom.onGestureChange, passiveListener);
432
-      swiper.$wrapperEl.off('gestureend', slideSelector, zoom.onGestureEnd, passiveListener);
433
-    } else if (swiper.touchEvents.start === 'touchstart') {
434
-      swiper.$wrapperEl.off(swiper.touchEvents.start, slideSelector, zoom.onGestureStart, passiveListener);
435
-      swiper.$wrapperEl.off(swiper.touchEvents.move, slideSelector, zoom.onGestureChange, activeListenerWithCapture);
436
-      swiper.$wrapperEl.off(swiper.touchEvents.end, slideSelector, zoom.onGestureEnd, passiveListener);
437
-      if (swiper.touchEvents.cancel) {
438
-        swiper.$wrapperEl.off(swiper.touchEvents.cancel, slideSelector, zoom.onGestureEnd, passiveListener);
439
-      }
440
-    }
441
-
442
-    // Move image
443
-    swiper.$wrapperEl.off(swiper.touchEvents.move, `.${swiper.params.zoom.containerClass}`, zoom.onTouchMove, activeListenerWithCapture);
444
-  },
445
-};
446
-
447
-export default {
448
-  name: 'zoom',
449
-  params: {
450
-    zoom: {
451
-      enabled: false,
452
-      maxRatio: 3,
453
-      minRatio: 1,
454
-      toggle: true,
455
-      containerClass: 'swiper-zoom-container',
456
-      zoomedSlideClass: 'swiper-slide-zoomed',
457
-    },
458
-  },
459
-  create() {
460
-    const swiper = this;
461
-    const zoom = {
462
-      enabled: false,
463
-      scale: 1,
464
-      currentScale: 1,
465
-      isScaling: false,
466
-      gesture: {
467
-        $slideEl: undefined,
468
-        slideWidth: undefined,
469
-        slideHeight: undefined,
470
-        $imageEl: undefined,
471
-        $imageWrapEl: undefined,
472
-        maxRatio: 3,
473
-      },
474
-      image: {
475
-        isTouched: undefined,
476
-        isMoved: undefined,
477
-        currentX: undefined,
478
-        currentY: undefined,
479
-        minX: undefined,
480
-        minY: undefined,
481
-        maxX: undefined,
482
-        maxY: undefined,
483
-        width: undefined,
484
-        height: undefined,
485
-        startX: undefined,
486
-        startY: undefined,
487
-        touchesStart: {},
488
-        touchesCurrent: {},
489
-      },
490
-      velocity: {
491
-        x: undefined,
492
-        y: undefined,
493
-        prevPositionX: undefined,
494
-        prevPositionY: undefined,
495
-        prevTime: undefined,
496
-      },
497
-    };
498
-
499
-    ('onGestureStart onGestureChange onGestureEnd onTouchStart onTouchMove onTouchEnd onTransitionEnd toggle enable disable in out').split(' ').forEach((methodName) => {
500
-      zoom[methodName] = Zoom[methodName].bind(swiper);
501
-    });
502
-    Utils.extend(swiper, {
503
-      zoom,
504
-    });
505
-
506
-    let scale = 1;
507
-    Object.defineProperty(swiper.zoom, 'scale', {
508
-      get() {
509
-        return scale;
510
-      },
511
-      set(value) {
512
-        if (scale !== value) {
513
-          const imageEl = swiper.zoom.gesture.$imageEl ? swiper.zoom.gesture.$imageEl[0] : undefined;
514
-          const slideEl = swiper.zoom.gesture.$slideEl ? swiper.zoom.gesture.$slideEl[0] : undefined;
515
-          swiper.emit('zoomChange', value, imageEl, slideEl);
516
-        }
517
-        scale = value;
518
-      },
519
-    });
520
-  },
521
-  on: {
522
-    init() {
523
-      const swiper = this;
524
-      if (swiper.params.zoom.enabled) {
525
-        swiper.zoom.enable();
526
-      }
527
-    },
528
-    destroy() {
529
-      const swiper = this;
530
-      swiper.zoom.disable();
531
-    },
532
-    touchStart(e) {
533
-      const swiper = this;
534
-      if (!swiper.zoom.enabled) return;
535
-      swiper.zoom.onTouchStart(e);
536
-    },
537
-    touchEnd(e) {
538
-      const swiper = this;
539
-      if (!swiper.zoom.enabled) return;
540
-      swiper.zoom.onTouchEnd(e);
541
-    },
542
-    doubleTap(e) {
543
-      const swiper = this;
544
-      if (swiper.params.zoom.enabled && swiper.zoom.enabled && swiper.params.zoom.toggle) {
545
-        swiper.zoom.toggle(e);
546
-      }
547
-    },
548
-    transitionEnd() {
549
-      const swiper = this;
550
-      if (swiper.zoom.enabled && swiper.params.zoom.enabled) {
551
-        swiper.zoom.onTransitionEnd();
552
-      }
553
-    },
554
-    slideChange() {
555
-      const swiper = this;
556
-      if (swiper.zoom.enabled && swiper.params.zoom.enabled && swiper.params.cssMode) {
557
-        swiper.zoom.onTransitionEnd();
558
-      }
559
-    },
560
-  },
561
-};
Browse code

Initial commit

Benjamin Roth authored on19/05/2020 21:59:44
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,561 @@
1
+import $ from '../../utils/dom';
2
+import Utils from '../../utils/utils';
3
+import Device from '../../utils/device';
4
+import Support from '../../utils/support';
5
+
6
+const Zoom = {
7
+  // Calc Scale From Multi-touches
8
+  getDistanceBetweenTouches(e) {
9
+    if (e.targetTouches.length < 2) return 1;
10
+    const x1 = e.targetTouches[0].pageX;
11
+    const y1 = e.targetTouches[0].pageY;
12
+    const x2 = e.targetTouches[1].pageX;
13
+    const y2 = e.targetTouches[1].pageY;
14
+    const distance = Math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2));
15
+    return distance;
16
+  },
17
+  // Events
18
+  onGestureStart(e) {
19
+    const swiper = this;
20
+    const params = swiper.params.zoom;
21
+    const zoom = swiper.zoom;
22
+    const { gesture } = zoom;
23
+    zoom.fakeGestureTouched = false;
24
+    zoom.fakeGestureMoved = false;
25
+    if (!Support.gestures) {
26
+      if (e.type !== 'touchstart' || (e.type === 'touchstart' && e.targetTouches.length < 2)) {
27
+        return;
28
+      }
29
+      zoom.fakeGestureTouched = true;
30
+      gesture.scaleStart = Zoom.getDistanceBetweenTouches(e);
31
+    }
32
+    if (!gesture.$slideEl || !gesture.$slideEl.length) {
33
+      gesture.$slideEl = $(e.target).closest(`.${swiper.params.slideClass}`);
34
+      if (gesture.$slideEl.length === 0) gesture.$slideEl = swiper.slides.eq(swiper.activeIndex);
35
+      gesture.$imageEl = gesture.$slideEl.find('img, svg, canvas, picture, .swiper-zoom-target');
36
+      gesture.$imageWrapEl = gesture.$imageEl.parent(`.${params.containerClass}`);
37
+      gesture.maxRatio = gesture.$imageWrapEl.attr('data-swiper-zoom') || params.maxRatio;
38
+      if (gesture.$imageWrapEl.length === 0) {
39
+        gesture.$imageEl = undefined;
40
+        return;
41
+      }
42
+    }
43
+    if (gesture.$imageEl) {
44
+      gesture.$imageEl.transition(0);
45
+    }
46
+    swiper.zoom.isScaling = true;
47
+  },
48
+  onGestureChange(e) {
49
+    const swiper = this;
50
+    const params = swiper.params.zoom;
51
+    const zoom = swiper.zoom;
52
+    const { gesture } = zoom;
53
+    if (!Support.gestures) {
54
+      if (e.type !== 'touchmove' || (e.type === 'touchmove' && e.targetTouches.length < 2)) {
55
+        return;
56
+      }
57
+      zoom.fakeGestureMoved = true;
58
+      gesture.scaleMove = Zoom.getDistanceBetweenTouches(e);
59
+    }
60
+    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
61
+    if (Support.gestures) {
62
+      zoom.scale = e.scale * zoom.currentScale;
63
+    } else {
64
+      zoom.scale = (gesture.scaleMove / gesture.scaleStart) * zoom.currentScale;
65
+    }
66
+    if (zoom.scale > gesture.maxRatio) {
67
+      zoom.scale = (gesture.maxRatio - 1) + (((zoom.scale - gesture.maxRatio) + 1) ** 0.5);
68
+    }
69
+    if (zoom.scale < params.minRatio) {
70
+      zoom.scale = (params.minRatio + 1) - (((params.minRatio - zoom.scale) + 1) ** 0.5);
71
+    }
72
+    gesture.$imageEl.transform(`translate3d(0,0,0) scale(${zoom.scale})`);
73
+  },
74
+  onGestureEnd(e) {
75
+    const swiper = this;
76
+    const params = swiper.params.zoom;
77
+    const zoom = swiper.zoom;
78
+    const { gesture } = zoom;
79
+    if (!Support.gestures) {
80
+      if (!zoom.fakeGestureTouched || !zoom.fakeGestureMoved) {
81
+        return;
82
+      }
83
+      if (e.type !== 'touchend' || (e.type === 'touchend' && e.changedTouches.length < 2 && !Device.android)) {
84
+        return;
85
+      }
86
+      zoom.fakeGestureTouched = false;
87
+      zoom.fakeGestureMoved = false;
88
+    }
89
+    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
90
+    zoom.scale = Math.max(Math.min(zoom.scale, gesture.maxRatio), params.minRatio);
91
+    gesture.$imageEl.transition(swiper.params.speed).transform(`translate3d(0,0,0) scale(${zoom.scale})`);
92
+    zoom.currentScale = zoom.scale;
93
+    zoom.isScaling = false;
94
+    if (zoom.scale === 1) gesture.$slideEl = undefined;
95
+  },
96
+  onTouchStart(e) {
97
+    const swiper = this;
98
+    const zoom = swiper.zoom;
99
+    const { gesture, image } = zoom;
100
+    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
101
+    if (image.isTouched) return;
102
+    if (Device.android && e.cancelable) e.preventDefault();
103
+    image.isTouched = true;
104
+    image.touchesStart.x = e.type === 'touchstart' ? e.targetTouches[0].pageX : e.pageX;
105
+    image.touchesStart.y = e.type === 'touchstart' ? e.targetTouches[0].pageY : e.pageY;
106
+  },
107
+  onTouchMove(e) {
108
+    const swiper = this;
109
+    const zoom = swiper.zoom;
110
+    const { gesture, image, velocity } = zoom;
111
+    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
112
+    swiper.allowClick = false;
113
+    if (!image.isTouched || !gesture.$slideEl) return;
114
+
115
+    if (!image.isMoved) {
116
+      image.width = gesture.$imageEl[0].offsetWidth;
117
+      image.height = gesture.$imageEl[0].offsetHeight;
118
+      image.startX = Utils.getTranslate(gesture.$imageWrapEl[0], 'x') || 0;
119
+      image.startY = Utils.getTranslate(gesture.$imageWrapEl[0], 'y') || 0;
120
+      gesture.slideWidth = gesture.$slideEl[0].offsetWidth;
121
+      gesture.slideHeight = gesture.$slideEl[0].offsetHeight;
122
+      gesture.$imageWrapEl.transition(0);
123
+      if (swiper.rtl) {
124
+        image.startX = -image.startX;
125
+        image.startY = -image.startY;
126
+      }
127
+    }
128
+    // Define if we need image drag
129
+    const scaledWidth = image.width * zoom.scale;
130
+    const scaledHeight = image.height * zoom.scale;
131
+
132
+    if (scaledWidth < gesture.slideWidth && scaledHeight < gesture.slideHeight) return;
133
+
134
+    image.minX = Math.min(((gesture.slideWidth / 2) - (scaledWidth / 2)), 0);
135
+    image.maxX = -image.minX;
136
+    image.minY = Math.min(((gesture.slideHeight / 2) - (scaledHeight / 2)), 0);
137
+    image.maxY = -image.minY;
138
+
139
+    image.touchesCurrent.x = e.type === 'touchmove' ? e.targetTouches[0].pageX : e.pageX;
140
+    image.touchesCurrent.y = e.type === 'touchmove' ? e.targetTouches[0].pageY : e.pageY;
141
+
142
+    if (!image.isMoved && !zoom.isScaling) {
143
+      if (
144
+        swiper.isHorizontal()
145
+        && (
146
+          (Math.floor(image.minX) === Math.floor(image.startX) && image.touchesCurrent.x < image.touchesStart.x)
147
+          || (Math.floor(image.maxX) === Math.floor(image.startX) && image.touchesCurrent.x > image.touchesStart.x)
148
+        )
149
+      ) {
150
+        image.isTouched = false;
151
+        return;
152
+      } if (
153
+        !swiper.isHorizontal()
154
+        && (
155
+          (Math.floor(image.minY) === Math.floor(image.startY) && image.touchesCurrent.y < image.touchesStart.y)
156
+          || (Math.floor(image.maxY) === Math.floor(image.startY) && image.touchesCurrent.y > image.touchesStart.y)
157
+        )
158
+      ) {
159
+        image.isTouched = false;
160
+        return;
161
+      }
162
+    }
163
+    if (e.cancelable) {
164
+      e.preventDefault();
165
+    }
166
+    e.stopPropagation();
167
+
168
+    image.isMoved = true;
169
+    image.currentX = (image.touchesCurrent.x - image.touchesStart.x) + image.startX;
170
+    image.currentY = (image.touchesCurrent.y - image.touchesStart.y) + image.startY;
171
+
172
+    if (image.currentX < image.minX) {
173
+      image.currentX = (image.minX + 1) - (((image.minX - image.currentX) + 1) ** 0.8);
174
+    }
175
+    if (image.currentX > image.maxX) {
176
+      image.currentX = (image.maxX - 1) + (((image.currentX - image.maxX) + 1) ** 0.8);
177
+    }
178
+
179
+    if (image.currentY < image.minY) {
180
+      image.currentY = (image.minY + 1) - (((image.minY - image.currentY) + 1) ** 0.8);
181
+    }
182
+    if (image.currentY > image.maxY) {
183
+      image.currentY = (image.maxY - 1) + (((image.currentY - image.maxY) + 1) ** 0.8);
184
+    }
185
+
186
+    // Velocity
187
+    if (!velocity.prevPositionX) velocity.prevPositionX = image.touchesCurrent.x;
188
+    if (!velocity.prevPositionY) velocity.prevPositionY = image.touchesCurrent.y;
189
+    if (!velocity.prevTime) velocity.prevTime = Date.now();
190
+    velocity.x = (image.touchesCurrent.x - velocity.prevPositionX) / (Date.now() - velocity.prevTime) / 2;
191
+    velocity.y = (image.touchesCurrent.y - velocity.prevPositionY) / (Date.now() - velocity.prevTime) / 2;
192
+    if (Math.abs(image.touchesCurrent.x - velocity.prevPositionX) < 2) velocity.x = 0;
193
+    if (Math.abs(image.touchesCurrent.y - velocity.prevPositionY) < 2) velocity.y = 0;
194
+    velocity.prevPositionX = image.touchesCurrent.x;
195
+    velocity.prevPositionY = image.touchesCurrent.y;
196
+    velocity.prevTime = Date.now();
197
+
198
+    gesture.$imageWrapEl.transform(`translate3d(${image.currentX}px, ${image.currentY}px,0)`);
199
+  },
200
+  onTouchEnd() {
201
+    const swiper = this;
202
+    const zoom = swiper.zoom;
203
+    const { gesture, image, velocity } = zoom;
204
+    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
205
+    if (!image.isTouched || !image.isMoved) {
206
+      image.isTouched = false;
207
+      image.isMoved = false;
208
+      return;
209
+    }
210
+    image.isTouched = false;
211
+    image.isMoved = false;
212
+    let momentumDurationX = 300;
213
+    let momentumDurationY = 300;
214
+    const momentumDistanceX = velocity.x * momentumDurationX;
215
+    const newPositionX = image.currentX + momentumDistanceX;
216
+    const momentumDistanceY = velocity.y * momentumDurationY;
217
+    const newPositionY = image.currentY + momentumDistanceY;
218
+
219
+    // Fix duration
220
+    if (velocity.x !== 0) momentumDurationX = Math.abs((newPositionX - image.currentX) / velocity.x);
221
+    if (velocity.y !== 0) momentumDurationY = Math.abs((newPositionY - image.currentY) / velocity.y);
222
+    const momentumDuration = Math.max(momentumDurationX, momentumDurationY);
223
+
224
+    image.currentX = newPositionX;
225
+    image.currentY = newPositionY;
226
+
227
+    // Define if we need image drag
228
+    const scaledWidth = image.width * zoom.scale;
229
+    const scaledHeight = image.height * zoom.scale;
230
+    image.minX = Math.min(((gesture.slideWidth / 2) - (scaledWidth / 2)), 0);
231
+    image.maxX = -image.minX;
232
+    image.minY = Math.min(((gesture.slideHeight / 2) - (scaledHeight / 2)), 0);
233
+    image.maxY = -image.minY;
234
+    image.currentX = Math.max(Math.min(image.currentX, image.maxX), image.minX);
235
+    image.currentY = Math.max(Math.min(image.currentY, image.maxY), image.minY);
236
+
237
+    gesture.$imageWrapEl.transition(momentumDuration).transform(`translate3d(${image.currentX}px, ${image.currentY}px,0)`);
238
+  },
239
+  onTransitionEnd() {
240
+    const swiper = this;
241
+    const zoom = swiper.zoom;
242
+    const { gesture } = zoom;
243
+    if (gesture.$slideEl && swiper.previousIndex !== swiper.activeIndex) {
244
+      if (gesture.$imageEl) {
245
+        gesture.$imageEl.transform('translate3d(0,0,0) scale(1)');
246
+      }
247
+      if (gesture.$imageWrapEl) {
248
+        gesture.$imageWrapEl.transform('translate3d(0,0,0)');
249
+      }
250
+
251
+      zoom.scale = 1;
252
+      zoom.currentScale = 1;
253
+
254
+      gesture.$slideEl = undefined;
255
+      gesture.$imageEl = undefined;
256
+      gesture.$imageWrapEl = undefined;
257
+    }
258
+  },
259
+  // Toggle Zoom
260
+  toggle(e) {
261
+    const swiper = this;
262
+    const zoom = swiper.zoom;
263
+
264
+    if (zoom.scale && zoom.scale !== 1) {
265
+      // Zoom Out
266
+      zoom.out();
267
+    } else {
268
+      // Zoom In
269
+      zoom.in(e);
270
+    }
271
+  },
272
+  in(e) {
273
+    const swiper = this;
274
+
275
+    const zoom = swiper.zoom;
276
+    const params = swiper.params.zoom;
277
+    const { gesture, image } = zoom;
278
+
279
+    if (!gesture.$slideEl) {
280
+      if (swiper.params.virtual && swiper.params.virtual.enabled && swiper.virtual) {
281
+        gesture.$slideEl = swiper.$wrapperEl.children(`.${swiper.params.slideActiveClass}`);
282
+      } else {
283
+        gesture.$slideEl = swiper.slides.eq(swiper.activeIndex);
284
+      }
285
+      gesture.$imageEl = gesture.$slideEl.find('img, svg, canvas, picture, .swiper-zoom-target');
286
+      gesture.$imageWrapEl = gesture.$imageEl.parent(`.${params.containerClass}`);
287
+    }
288
+    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
289
+
290
+    gesture.$slideEl.addClass(`${params.zoomedSlideClass}`);
291
+
292
+    let touchX;
293
+    let touchY;
294
+    let offsetX;
295
+    let offsetY;
296
+    let diffX;
297
+    let diffY;
298
+    let translateX;
299
+    let translateY;
300
+    let imageWidth;
301
+    let imageHeight;
302
+    let scaledWidth;
303
+    let scaledHeight;
304
+    let translateMinX;
305
+    let translateMinY;
306
+    let translateMaxX;
307
+    let translateMaxY;
308
+    let slideWidth;
309
+    let slideHeight;
310
+
311
+    if (typeof image.touchesStart.x === 'undefined' && e) {
312
+      touchX = e.type === 'touchend' ? e.changedTouches[0].pageX : e.pageX;
313
+      touchY = e.type === 'touchend' ? e.changedTouches[0].pageY : e.pageY;
314
+    } else {
315
+      touchX = image.touchesStart.x;
316
+      touchY = image.touchesStart.y;
317
+    }
318
+
319
+    zoom.scale = gesture.$imageWrapEl.attr('data-swiper-zoom') || params.maxRatio;
320
+    zoom.currentScale = gesture.$imageWrapEl.attr('data-swiper-zoom') || params.maxRatio;
321
+    if (e) {
322
+      slideWidth = gesture.$slideEl[0].offsetWidth;
323
+      slideHeight = gesture.$slideEl[0].offsetHeight;
324
+      offsetX = gesture.$slideEl.offset().left;
325
+      offsetY = gesture.$slideEl.offset().top;
326
+      diffX = (offsetX + (slideWidth / 2)) - touchX;
327
+      diffY = (offsetY + (slideHeight / 2)) - touchY;
328
+
329
+      imageWidth = gesture.$imageEl[0].offsetWidth;
330
+      imageHeight = gesture.$imageEl[0].offsetHeight;
331
+      scaledWidth = imageWidth * zoom.scale;
332
+      scaledHeight = imageHeight * zoom.scale;
333
+
334
+      translateMinX = Math.min(((slideWidth / 2) - (scaledWidth / 2)), 0);
335
+      translateMinY = Math.min(((slideHeight / 2) - (scaledHeight / 2)), 0);
336
+      translateMaxX = -translateMinX;
337
+      translateMaxY = -translateMinY;
338
+
339
+      translateX = diffX * zoom.scale;
340
+      translateY = diffY * zoom.scale;
341
+
342
+      if (translateX < translateMinX) {
343
+        translateX = translateMinX;
344
+      }
345
+      if (translateX > translateMaxX) {
346
+        translateX = translateMaxX;
347
+      }
348
+
349
+      if (translateY < translateMinY) {
350
+        translateY = translateMinY;
351
+      }
352
+      if (translateY > translateMaxY) {
353
+        translateY = translateMaxY;
354
+      }
355
+    } else {
356
+      translateX = 0;
357
+      translateY = 0;
358
+    }
359
+    gesture.$imageWrapEl.transition(300).transform(`translate3d(${translateX}px, ${translateY}px,0)`);
360
+    gesture.$imageEl.transition(300).transform(`translate3d(0,0,0) scale(${zoom.scale})`);
361
+  },
362
+  out() {
363
+    const swiper = this;
364
+
365
+    const zoom = swiper.zoom;
366
+    const params = swiper.params.zoom;
367
+    const { gesture } = zoom;
368
+
369
+    if (!gesture.$slideEl) {
370
+      if (swiper.params.virtual && swiper.params.virtual.enabled && swiper.virtual) {
371
+        gesture.$slideEl = swiper.$wrapperEl.children(`.${swiper.params.slideActiveClass}`);
372
+      } else {
373
+        gesture.$slideEl = swiper.slides.eq(swiper.activeIndex);
374
+      }
375
+      gesture.$imageEl = gesture.$slideEl.find('img, svg, canvas, picture, .swiper-zoom-target');
376
+      gesture.$imageWrapEl = gesture.$imageEl.parent(`.${params.containerClass}`);
377
+    }
378
+    if (!gesture.$imageEl || gesture.$imageEl.length === 0) return;
379
+
380
+    zoom.scale = 1;
381
+    zoom.currentScale = 1;
382
+    gesture.$imageWrapEl.transition(300).transform('translate3d(0,0,0)');
383
+    gesture.$imageEl.transition(300).transform('translate3d(0,0,0) scale(1)');
384
+    gesture.$slideEl.removeClass(`${params.zoomedSlideClass}`);
385
+    gesture.$slideEl = undefined;
386
+  },
387
+  // Attach/Detach Events
388
+  enable() {
389
+    const swiper = this;
390
+    const zoom = swiper.zoom;
391
+    if (zoom.enabled) return;
392
+    zoom.enabled = true;
393
+
394
+    const passiveListener = swiper.touchEvents.start === 'touchstart' && Support.passiveListener && swiper.params.passiveListeners ? { passive: true, capture: false } : false;
395
+    const activeListenerWithCapture = Support.passiveListener ? { passive: false, capture: true } : true;
396
+
397
+    const slideSelector = `.${swiper.params.slideClass}`;
398
+
399
+    // Scale image
400
+    if (Support.gestures) {
401
+      swiper.$wrapperEl.on('gesturestart', slideSelector, zoom.onGestureStart, passiveListener);
402
+      swiper.$wrapperEl.on('gesturechange', slideSelector, zoom.onGestureChange, passiveListener);
403
+      swiper.$wrapperEl.on('gestureend', slideSelector, zoom.onGestureEnd, passiveListener);
404
+    } else if (swiper.touchEvents.start === 'touchstart') {
405
+      swiper.$wrapperEl.on(swiper.touchEvents.start, slideSelector, zoom.onGestureStart, passiveListener);
406
+      swiper.$wrapperEl.on(swiper.touchEvents.move, slideSelector, zoom.onGestureChange, activeListenerWithCapture);
407
+      swiper.$wrapperEl.on(swiper.touchEvents.end, slideSelector, zoom.onGestureEnd, passiveListener);
408
+      if (swiper.touchEvents.cancel) {
409
+        swiper.$wrapperEl.on(swiper.touchEvents.cancel, slideSelector, zoom.onGestureEnd, passiveListener);
410
+      }
411
+    }
412
+
413
+    // Move image
414
+    swiper.$wrapperEl.on(swiper.touchEvents.move, `.${swiper.params.zoom.containerClass}`, zoom.onTouchMove, activeListenerWithCapture);
415
+  },
416
+  disable() {
417
+    const swiper = this;
418
+    const zoom = swiper.zoom;
419
+    if (!zoom.enabled) return;
420
+
421
+    swiper.zoom.enabled = false;
422
+
423
+    const passiveListener = swiper.touchEvents.start === 'touchstart' && Support.passiveListener && swiper.params.passiveListeners ? { passive: true, capture: false } : false;
424
+    const activeListenerWithCapture = Support.passiveListener ? { passive: false, capture: true } : true;
425
+
426
+    const slideSelector = `.${swiper.params.slideClass}`;
427
+
428
+    // Scale image
429
+    if (Support.gestures) {
430
+      swiper.$wrapperEl.off('gesturestart', slideSelector, zoom.onGestureStart, passiveListener);
431
+      swiper.$wrapperEl.off('gesturechange', slideSelector, zoom.onGestureChange, passiveListener);
432
+      swiper.$wrapperEl.off('gestureend', slideSelector, zoom.onGestureEnd, passiveListener);
433
+    } else if (swiper.touchEvents.start === 'touchstart') {
434
+      swiper.$wrapperEl.off(swiper.touchEvents.start, slideSelector, zoom.onGestureStart, passiveListener);
435
+      swiper.$wrapperEl.off(swiper.touchEvents.move, slideSelector, zoom.onGestureChange, activeListenerWithCapture);
436
+      swiper.$wrapperEl.off(swiper.touchEvents.end, slideSelector, zoom.onGestureEnd, passiveListener);
437
+      if (swiper.touchEvents.cancel) {
438
+        swiper.$wrapperEl.off(swiper.touchEvents.cancel, slideSelector, zoom.onGestureEnd, passiveListener);
439
+      }
440
+    }
441
+
442
+    // Move image
443
+    swiper.$wrapperEl.off(swiper.touchEvents.move, `.${swiper.params.zoom.containerClass}`, zoom.onTouchMove, activeListenerWithCapture);
444
+  },
445
+};
446
+
447
+export default {
448
+  name: 'zoom',
449
+  params: {
450
+    zoom: {
451
+      enabled: false,
452
+      maxRatio: 3,
453
+      minRatio: 1,
454
+      toggle: true,
455
+      containerClass: 'swiper-zoom-container',
456
+      zoomedSlideClass: 'swiper-slide-zoomed',
457
+    },
458
+  },
459
+  create() {
460
+    const swiper = this;
461
+    const zoom = {
462
+      enabled: false,
463
+      scale: 1,
464
+      currentScale: 1,
465
+      isScaling: false,
466
+      gesture: {
467
+        $slideEl: undefined,
468
+        slideWidth: undefined,
469
+        slideHeight: undefined,
470
+        $imageEl: undefined,
471
+        $imageWrapEl: undefined,
472
+        maxRatio: 3,
473
+      },
474
+      image: {
475
+        isTouched: undefined,
476
+        isMoved: undefined,
477
+        currentX: undefined,
478
+        currentY: undefined,
479
+        minX: undefined,
480
+        minY: undefined,
481
+        maxX: undefined,
482
+        maxY: undefined,
483
+        width: undefined,
484
+        height: undefined,
485
+        startX: undefined,
486
+        startY: undefined,
487
+        touchesStart: {},
488
+        touchesCurrent: {},
489
+      },
490
+      velocity: {
491
+        x: undefined,
492
+        y: undefined,
493
+        prevPositionX: undefined,
494
+        prevPositionY: undefined,
495
+        prevTime: undefined,
496
+      },
497
+    };
498
+
499
+    ('onGestureStart onGestureChange onGestureEnd onTouchStart onTouchMove onTouchEnd onTransitionEnd toggle enable disable in out').split(' ').forEach((methodName) => {
500
+      zoom[methodName] = Zoom[methodName].bind(swiper);
501
+    });
502
+    Utils.extend(swiper, {
503
+      zoom,
504
+    });
505
+
506
+    let scale = 1;
507
+    Object.defineProperty(swiper.zoom, 'scale', {
508
+      get() {
509
+        return scale;
510
+      },
511
+      set(value) {
512
+        if (scale !== value) {
513
+          const imageEl = swiper.zoom.gesture.$imageEl ? swiper.zoom.gesture.$imageEl[0] : undefined;
514
+          const slideEl = swiper.zoom.gesture.$slideEl ? swiper.zoom.gesture.$slideEl[0] : undefined;
515
+          swiper.emit('zoomChange', value, imageEl, slideEl);
516
+        }
517
+        scale = value;
518
+      },
519
+    });
520
+  },
521
+  on: {
522
+    init() {
523
+      const swiper = this;
524
+      if (swiper.params.zoom.enabled) {
525
+        swiper.zoom.enable();
526
+      }
527
+    },
528
+    destroy() {
529
+      const swiper = this;
530
+      swiper.zoom.disable();
531
+    },
532
+    touchStart(e) {
533
+      const swiper = this;
534
+      if (!swiper.zoom.enabled) return;
535
+      swiper.zoom.onTouchStart(e);
536
+    },
537
+    touchEnd(e) {
538
+      const swiper = this;
539
+      if (!swiper.zoom.enabled) return;
540
+      swiper.zoom.onTouchEnd(e);
541
+    },
542
+    doubleTap(e) {
543
+      const swiper = this;
544
+      if (swiper.params.zoom.enabled && swiper.zoom.enabled && swiper.params.zoom.toggle) {
545
+        swiper.zoom.toggle(e);
546
+      }
547
+    },
548
+    transitionEnd() {
549
+      const swiper = this;
550
+      if (swiper.zoom.enabled && swiper.params.zoom.enabled) {
551
+        swiper.zoom.onTransitionEnd();
552
+      }
553
+    },
554
+    slideChange() {
555
+      const swiper = this;
556
+      if (swiper.zoom.enabled && swiper.params.zoom.enabled && swiper.params.cssMode) {
557
+        swiper.zoom.onTransitionEnd();
558
+      }
559
+    },
560
+  },
561
+};