@@ -29,6 +29,8 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
29
29
let fakeGestureMoved ;
30
30
const evCache = [ ] ;
31
31
const gesture = {
32
+ originX : 0 ,
33
+ originY : 0 ,
32
34
slideEl : undefined ,
33
35
slideWidth : undefined ,
34
36
slideHeight : undefined ,
@@ -158,7 +160,8 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
158
160
}
159
161
if ( gesture . imageEl ) {
160
162
const [ originX , originY ] = getScaleOrigin ( ) ;
161
- gesture . imageEl . style . transformOrigin = `${ originX } px ${ originY } px` ;
163
+ gesture . originX = originX ;
164
+ gesture . originY = originY ;
162
165
gesture . imageEl . style . transitionDuration = '0ms' ;
163
166
}
164
167
isScaling = true ;
@@ -187,7 +190,6 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
187
190
if ( zoom . scale < params . minRatio ) {
188
191
zoom . scale = params . minRatio + 1 - ( params . minRatio - zoom . scale + 1 ) ** 0.5 ;
189
192
}
190
-
191
193
gesture . imageEl . style . transform = `translate3d(0,0,0) scale(${ zoom . scale } )` ;
192
194
}
193
195
function onGestureEnd ( e ) {
@@ -212,22 +214,31 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
212
214
gesture . imageEl . style . transform = `translate3d(0,0,0) scale(${ zoom . scale } )` ;
213
215
currentScale = zoom . scale ;
214
216
isScaling = false ;
215
- if ( zoom . scale === 1 ) gesture . slideEl = undefined ;
217
+ if ( zoom . scale > 1 && gesture . slideEl ) {
218
+ gesture . slideEl . classList . add ( `${ params . zoomedSlideClass } ` ) ;
219
+ } else if ( zoom . scale <= 1 && gesture . slideEl ) {
220
+ gesture . slideEl . classList . remove ( `${ params . zoomedSlideClass } ` ) ;
221
+ }
222
+ if ( zoom . scale === 1 ) {
223
+ gesture . originX = 0 ;
224
+ gesture . originY = 0 ;
225
+ gesture . slideEl = undefined ;
226
+ }
216
227
}
217
228
function onTouchStart ( e ) {
218
229
const device = swiper . device ;
219
230
if ( ! gesture . imageEl ) return ;
220
231
if ( image . isTouched ) return ;
221
232
if ( device . android && e . cancelable ) e . preventDefault ( ) ;
222
233
image . isTouched = true ;
223
- image . touchesStart . x = e . pageX ;
224
- image . touchesStart . y = e . pageY ;
234
+ const event = evCache . length > 0 ? evCache [ 0 ] : e ;
235
+ image . touchesStart . x = event . pageX ;
236
+ image . touchesStart . y = event . pageY ;
225
237
}
226
238
function onTouchMove ( e ) {
227
239
if ( ! eventWithinSlide ( e ) || ! eventWithinZoomContainer ( e ) ) return ;
228
240
const zoom = swiper . zoom ;
229
241
if ( ! gesture . imageEl ) return ;
230
- swiper . allowClick = false ;
231
242
if ( ! image . isTouched || ! gesture . slideEl ) return ;
232
243
233
244
if ( ! image . isMoved ) {
@@ -252,6 +263,13 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
252
263
253
264
image . touchesCurrent . x = evCache . length > 0 ? evCache [ 0 ] . pageX : e . pageX ;
254
265
image . touchesCurrent . y = evCache . length > 0 ? evCache [ 0 ] . pageY : e . pageY ;
266
+ const touchesDiff = Math . max (
267
+ Math . abs ( image . touchesCurrent . x - image . touchesStart . x ) ,
268
+ Math . abs ( image . touchesCurrent . y - image . touchesStart . y ) ,
269
+ ) ;
270
+ if ( touchesDiff > 5 ) {
271
+ swiper . allowClick = false ;
272
+ }
255
273
256
274
if ( ! image . isMoved && ! isScaling ) {
257
275
if (
@@ -281,8 +299,20 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
281
299
e . stopPropagation ( ) ;
282
300
283
301
image . isMoved = true ;
284
- image . currentX = image . touchesCurrent . x - image . touchesStart . x + image . startX ;
285
- image . currentY = image . touchesCurrent . y - image . touchesStart . y + image . startY ;
302
+ const scaleRatio =
303
+ ( zoom . scale - currentScale ) / ( gesture . maxRatio - swiper . params . zoom . minRatio ) ;
304
+ const { originX, originY } = gesture ;
305
+
306
+ image . currentX =
307
+ image . touchesCurrent . x -
308
+ image . touchesStart . x +
309
+ image . startX +
310
+ scaleRatio * ( image . width - originX * 2 ) ;
311
+ image . currentY =
312
+ image . touchesCurrent . y -
313
+ image . touchesStart . y +
314
+ image . startY +
315
+ scaleRatio * ( image . height - originY * 2 ) ;
286
316
287
317
if ( image . currentX < image . minX ) {
288
318
image . currentX = image . minX + 1 - ( image . minX - image . currentX + 1 ) ** 0.8 ;
@@ -340,7 +370,6 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
340
370
341
371
image . currentX = newPositionX ;
342
372
image . currentY = newPositionY ;
343
-
344
373
// Define if we need image drag
345
374
const scaledWidth = image . width * zoom . scale ;
346
375
const scaledHeight = image . height * zoom . scale ;
@@ -356,19 +385,22 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
356
385
}
357
386
function onTransitionEnd ( ) {
358
387
const zoom = swiper . zoom ;
359
- if ( gesture . slideEl && swiper . previousIndex !== swiper . activeIndex ) {
388
+ if ( gesture . slideEl && swiper . activeIndex !== swiper . slides . indexOf ( gesture . slideEl ) ) {
360
389
if ( gesture . imageEl ) {
361
390
gesture . imageEl . style . transform = 'translate3d(0,0,0) scale(1)' ;
362
391
}
363
392
if ( gesture . imageWrapEl ) {
364
393
gesture . imageWrapEl . style . transform = 'translate3d(0,0,0)' ;
365
394
}
395
+ gesture . slideEl . classList . remove ( `${ swiper . params . zoom . zoomedSlideClass } ` ) ;
366
396
367
397
zoom . scale = 1 ;
368
398
currentScale = 1 ;
369
399
gesture . slideEl = undefined ;
370
400
gesture . imageEl = undefined ;
371
401
gesture . imageWrapEl = undefined ;
402
+ gesture . originX = 0 ;
403
+ gesture . originY = 0 ;
372
404
}
373
405
}
374
406
@@ -446,6 +478,7 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
446
478
forceZoomRatio || gesture . imageWrapEl . getAttribute ( 'data-swiper-zoom' ) || params . maxRatio ;
447
479
currentScale =
448
480
forceZoomRatio || gesture . imageWrapEl . getAttribute ( 'data-swiper-zoom' ) || params . maxRatio ;
481
+
449
482
if ( e && ! ( currentScale === 1 && forceZoomRatio ) ) {
450
483
slideWidth = gesture . slideEl . offsetWidth ;
451
484
slideHeight = gesture . slideEl . offsetHeight ;
@@ -484,6 +517,10 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
484
517
translateX = 0 ;
485
518
translateY = 0 ;
486
519
}
520
+ if ( forceZoomRatio && zoom . scale === 1 ) {
521
+ gesture . originX = 0 ;
522
+ gesture . originY = 0 ;
523
+ }
487
524
gesture . imageWrapEl . style . transitionDuration = '300ms' ;
488
525
gesture . imageWrapEl . style . transform = `translate3d(${ translateX } px, ${ translateY } px,0)` ;
489
526
gesture . imageEl . style . transitionDuration = '300ms' ;
@@ -524,6 +561,8 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
524
561
525
562
gesture . slideEl . classList . remove ( `${ params . zoomedSlideClass } ` ) ;
526
563
gesture . slideEl = undefined ;
564
+ gesture . originX = 0 ;
565
+ gesture . originY = 0 ;
527
566
}
528
567
529
568
// Toggle Zoom
@@ -557,7 +596,6 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
557
596
const { passiveListener, activeListenerWithCapture } = getListeners ( ) ;
558
597
559
598
// Scale image
560
-
561
599
swiper . wrapperEl . addEventListener ( 'pointerdown' , onGestureStart , passiveListener ) ;
562
600
swiper . wrapperEl . addEventListener ( 'pointermove' , onGestureChange , activeListenerWithCapture ) ;
563
601
[ 'pointerup' , 'pointercancel' , 'pointerout' ] . forEach ( ( eventName ) => {
0 commit comments