Skip to content

Commit 7794f9c

Browse files
committed
Merge branch 'master' into pr/6544
2 parents 6776359 + 6016a50 commit 7794f9c

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

src/modules/zoom/zoom.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
2929
let fakeGestureMoved;
3030
const evCache = [];
3131
const gesture = {
32+
originX: 0,
33+
originY: 0,
3234
slideEl: undefined,
3335
slideWidth: undefined,
3436
slideHeight: undefined,
@@ -158,7 +160,8 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
158160
}
159161
if (gesture.imageEl) {
160162
const [originX, originY] = getScaleOrigin();
161-
gesture.imageEl.style.transformOrigin = `${originX}px ${originY}px`;
163+
gesture.originX = originX;
164+
gesture.originY = originY;
162165
gesture.imageEl.style.transitionDuration = '0ms';
163166
}
164167
isScaling = true;
@@ -187,7 +190,6 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
187190
if (zoom.scale < params.minRatio) {
188191
zoom.scale = params.minRatio + 1 - (params.minRatio - zoom.scale + 1) ** 0.5;
189192
}
190-
191193
gesture.imageEl.style.transform = `translate3d(0,0,0) scale(${zoom.scale})`;
192194
}
193195
function onGestureEnd(e) {
@@ -212,22 +214,31 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
212214
gesture.imageEl.style.transform = `translate3d(0,0,0) scale(${zoom.scale})`;
213215
currentScale = zoom.scale;
214216
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+
}
216227
}
217228
function onTouchStart(e) {
218229
const device = swiper.device;
219230
if (!gesture.imageEl) return;
220231
if (image.isTouched) return;
221232
if (device.android && e.cancelable) e.preventDefault();
222233
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;
225237
}
226238
function onTouchMove(e) {
227239
if (!eventWithinSlide(e) || !eventWithinZoomContainer(e)) return;
228240
const zoom = swiper.zoom;
229241
if (!gesture.imageEl) return;
230-
swiper.allowClick = false;
231242
if (!image.isTouched || !gesture.slideEl) return;
232243

233244
if (!image.isMoved) {
@@ -252,6 +263,13 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
252263

253264
image.touchesCurrent.x = evCache.length > 0 ? evCache[0].pageX : e.pageX;
254265
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+
}
255273

256274
if (!image.isMoved && !isScaling) {
257275
if (
@@ -281,8 +299,20 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
281299
e.stopPropagation();
282300

283301
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);
286316

287317
if (image.currentX < image.minX) {
288318
image.currentX = image.minX + 1 - (image.minX - image.currentX + 1) ** 0.8;
@@ -340,7 +370,6 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
340370

341371
image.currentX = newPositionX;
342372
image.currentY = newPositionY;
343-
344373
// Define if we need image drag
345374
const scaledWidth = image.width * zoom.scale;
346375
const scaledHeight = image.height * zoom.scale;
@@ -356,19 +385,22 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
356385
}
357386
function onTransitionEnd() {
358387
const zoom = swiper.zoom;
359-
if (gesture.slideEl && swiper.previousIndex !== swiper.activeIndex) {
388+
if (gesture.slideEl && swiper.activeIndex !== swiper.slides.indexOf(gesture.slideEl)) {
360389
if (gesture.imageEl) {
361390
gesture.imageEl.style.transform = 'translate3d(0,0,0) scale(1)';
362391
}
363392
if (gesture.imageWrapEl) {
364393
gesture.imageWrapEl.style.transform = 'translate3d(0,0,0)';
365394
}
395+
gesture.slideEl.classList.remove(`${swiper.params.zoom.zoomedSlideClass}`);
366396

367397
zoom.scale = 1;
368398
currentScale = 1;
369399
gesture.slideEl = undefined;
370400
gesture.imageEl = undefined;
371401
gesture.imageWrapEl = undefined;
402+
gesture.originX = 0;
403+
gesture.originY = 0;
372404
}
373405
}
374406

@@ -446,6 +478,7 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
446478
forceZoomRatio || gesture.imageWrapEl.getAttribute('data-swiper-zoom') || params.maxRatio;
447479
currentScale =
448480
forceZoomRatio || gesture.imageWrapEl.getAttribute('data-swiper-zoom') || params.maxRatio;
481+
449482
if (e && !(currentScale === 1 && forceZoomRatio)) {
450483
slideWidth = gesture.slideEl.offsetWidth;
451484
slideHeight = gesture.slideEl.offsetHeight;
@@ -484,6 +517,10 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
484517
translateX = 0;
485518
translateY = 0;
486519
}
520+
if (forceZoomRatio && zoom.scale === 1) {
521+
gesture.originX = 0;
522+
gesture.originY = 0;
523+
}
487524
gesture.imageWrapEl.style.transitionDuration = '300ms';
488525
gesture.imageWrapEl.style.transform = `translate3d(${translateX}px, ${translateY}px,0)`;
489526
gesture.imageEl.style.transitionDuration = '300ms';
@@ -524,6 +561,8 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
524561

525562
gesture.slideEl.classList.remove(`${params.zoomedSlideClass}`);
526563
gesture.slideEl = undefined;
564+
gesture.originX = 0;
565+
gesture.originY = 0;
527566
}
528567

529568
// Toggle Zoom
@@ -557,7 +596,6 @@ export default function Zoom({ swiper, extendParams, on, emit }) {
557596
const { passiveListener, activeListenerWithCapture } = getListeners();
558597

559598
// Scale image
560-
561599
swiper.wrapperEl.addEventListener('pointerdown', onGestureStart, passiveListener);
562600
swiper.wrapperEl.addEventListener('pointermove', onGestureChange, activeListenerWithCapture);
563601
['pointerup', 'pointercancel', 'pointerout'].forEach((eventName) => {

0 commit comments

Comments
 (0)