zoom.js 20 KB

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