lg-zoom.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /**!
  2. * lg-zoom.js | 0.0.2 | August 4th 2016
  3. * http://sachinchoolur.github.io/lg-zoom.js
  4. * Copyright (c) 2016 Sachin N;
  5. * @license Apache 2.0
  6. */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.LgZoom = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  7. (function (global, factory) {
  8. if (typeof define === "function" && define.amd) {
  9. define([], factory);
  10. } else if (typeof exports !== "undefined") {
  11. factory();
  12. } else {
  13. var mod = {
  14. exports: {}
  15. };
  16. factory();
  17. global.lgZoom = mod.exports;
  18. }
  19. })(this, function () {
  20. 'use strict';
  21. var _extends = Object.assign || function (target) {
  22. for (var i = 1; i < arguments.length; i++) {
  23. var source = arguments[i];
  24. for (var key in source) {
  25. if (Object.prototype.hasOwnProperty.call(source, key)) {
  26. target[key] = source[key];
  27. }
  28. }
  29. }
  30. return target;
  31. };
  32. var zoomDefaults = {
  33. scale: 1,
  34. zoom: true,
  35. actualSize: true,
  36. enableZoomAfter: 300
  37. };
  38. var Zoom = function Zoom(element) {
  39. this.el = element;
  40. this.core = window.lgData[this.el.getAttribute('lg-uid')];
  41. this.core.s = _extends({}, zoomDefaults, this.core.s);
  42. if (this.core.s.zoom && this.core.doCss()) {
  43. this.init();
  44. // Store the zoomable timeout value just to clear it while closing
  45. this.zoomabletimeout = false;
  46. // Set the initial value center
  47. this.pageX = window.innerWidth / 2;
  48. this.pageY = window.innerHeight / 2 + (document.documentElement.scrollTop || document.body.scrollTop);
  49. }
  50. return this;
  51. };
  52. Zoom.prototype.init = function () {
  53. var _this = this;
  54. var zoomIcons = '<span id="lg-zoom-in" class="lg-icon"></span><span id="lg-zoom-out" class="lg-icon"></span>';
  55. if (_this.core.s.actualSize) {
  56. zoomIcons += '<span id="lg-actual-size" class="lg-icon"></span>';
  57. }
  58. this.core.outer.querySelector('.lg-toolbar').insertAdjacentHTML('beforeend', zoomIcons);
  59. // Add zoomable class
  60. utils.on(_this.core.el, 'onSlideItemLoad.lgtmzoom', function (event) {
  61. // delay will be 0 except first time
  62. var _speed = _this.core.s.enableZoomAfter + event.detail.delay;
  63. // set _speed value 0 if gallery opened from direct url and if it is first slide
  64. if (utils.hasClass(document.body, 'lg-from-hash') && event.detail.delay) {
  65. // will execute only once
  66. _speed = 0;
  67. } else {
  68. // Remove lg-from-hash to enable starting animation.
  69. utils.removeClass(document.body, 'lg-from-hash');
  70. }
  71. _this.zoomabletimeout = setTimeout(function () {
  72. utils.addClass(_this.core.___slide[event.detail.index], 'lg-zoomable');
  73. }, _speed + 30);
  74. });
  75. var scale = 1;
  76. /**
  77. * @desc Image zoom
  78. * Translate the wrap and scale the image to get better user experience
  79. *
  80. * @param {String} scaleVal - Zoom decrement/increment value
  81. */
  82. var zoom = function zoom(scaleVal) {
  83. var image = _this.core.outer.querySelector('.lg-current .lg-image');
  84. var _x;
  85. var _y;
  86. // Find offset manually to avoid issue after zoom
  87. var offsetX = (window.innerWidth - image.clientWidth) / 2;
  88. var offsetY = (window.innerHeight - image.clientHeight) / 2 + (document.documentElement.scrollTop || document.body.scrollTop);
  89. _x = _this.pageX - offsetX;
  90. _y = _this.pageY - offsetY;
  91. var x = (scaleVal - 1) * _x;
  92. var y = (scaleVal - 1) * _y;
  93. utils.setVendor(image, 'Transform', 'scale3d(' + scaleVal + ', ' + scaleVal + ', 1)');
  94. image.setAttribute('data-scale', scaleVal);
  95. image.parentElement.style.left = -x + 'px';
  96. image.parentElement.style.top = -y + 'px';
  97. image.parentElement.setAttribute('data-x', x);
  98. image.parentElement.setAttribute('data-y', y);
  99. };
  100. var callScale = function callScale() {
  101. if (scale > 1) {
  102. utils.addClass(_this.core.outer, 'lg-zoomed');
  103. } else {
  104. _this.resetZoom();
  105. }
  106. if (scale < 1) {
  107. scale = 1;
  108. }
  109. zoom(scale);
  110. };
  111. var actualSize = function actualSize(event, image, index, fromIcon) {
  112. var w = image.clientWidth;
  113. var nw;
  114. if (_this.core.s.dynamic) {
  115. nw = _this.core.s.dynamicEl[index].width || image.naturalWidth || w;
  116. } else {
  117. nw = _this.core.items[index].getAttribute('data-width') || image.naturalWidth || w;
  118. }
  119. var _scale;
  120. if (utils.hasClass(_this.core.outer, 'lg-zoomed')) {
  121. scale = 1;
  122. } else {
  123. if (nw > w) {
  124. _scale = nw / w;
  125. scale = _scale || 2;
  126. }
  127. }
  128. if (fromIcon) {
  129. _this.pageX = window.innerWidth / 2;
  130. _this.pageY = window.innerHeight / 2 + (document.documentElement.scrollTop || document.body.scrollTop);
  131. } else {
  132. _this.pageX = event.pageX || event.targetTouches[0].pageX;
  133. _this.pageY = event.pageY || event.targetTouches[0].pageY;
  134. }
  135. callScale();
  136. setTimeout(function () {
  137. utils.removeClass(_this.core.outer, 'lg-grabbing');
  138. utils.addClass(_this.core.outer, 'lg-grab');
  139. }, 10);
  140. };
  141. var tapped = false;
  142. // event triggered after appending slide content
  143. utils.on(_this.core.el, 'onAferAppendSlide.lgtmzoom', function (event) {
  144. var index = event.detail.index;
  145. // Get the current element
  146. var image = _this.core.___slide[index].querySelector('.lg-image');
  147. if (!_this.core.isTouch) {
  148. utils.on(image, 'dblclick', function (event) {
  149. actualSize(event, image, index);
  150. });
  151. }
  152. if (_this.core.isTouch) {
  153. utils.on(image, 'touchstart', function (event) {
  154. if (!tapped) {
  155. tapped = setTimeout(function () {
  156. tapped = null;
  157. }, 300);
  158. } else {
  159. clearTimeout(tapped);
  160. tapped = null;
  161. actualSize(event, image, index);
  162. }
  163. event.preventDefault();
  164. });
  165. }
  166. });
  167. // Update zoom on resize and orientationchange
  168. utils.on(window, 'resize.lgzoom scroll.lgzoom orientationchange.lgzoom', function () {
  169. _this.pageX = window.innerWidth / 2;
  170. _this.pageY = window.innerHeight / 2 + (document.documentElement.scrollTop || document.body.scrollTop);
  171. zoom(scale);
  172. });
  173. utils.on(document.getElementById('lg-zoom-out'), 'click.lg', function () {
  174. if (_this.core.outer.querySelector('.lg-current .lg-image')) {
  175. scale -= _this.core.s.scale;
  176. callScale();
  177. }
  178. });
  179. utils.on(document.getElementById('lg-zoom-in'), 'click.lg', function () {
  180. if (_this.core.outer.querySelector('.lg-current .lg-image')) {
  181. scale += _this.core.s.scale;
  182. callScale();
  183. }
  184. });
  185. utils.on(document.getElementById('lg-actual-size'), 'click.lg', function (event) {
  186. actualSize(event, _this.core.___slide[_this.core.index].querySelector('.lg-image'), _this.core.index, true);
  187. });
  188. // Reset zoom on slide change
  189. utils.on(_this.core.el, 'onBeforeSlide.lgtm', function () {
  190. scale = 1;
  191. _this.resetZoom();
  192. });
  193. // Drag option after zoom
  194. if (!_this.core.isTouch) {
  195. _this.zoomDrag();
  196. }
  197. if (_this.core.isTouch) {
  198. _this.zoomSwipe();
  199. }
  200. };
  201. // Reset zoom effect
  202. Zoom.prototype.resetZoom = function () {
  203. utils.removeClass(this.core.outer, 'lg-zoomed');
  204. for (var i = 0; i < this.core.___slide.length; i++) {
  205. if (this.core.___slide[i].querySelector('.lg-img-wrap')) {
  206. this.core.___slide[i].querySelector('.lg-img-wrap').removeAttribute('style');
  207. this.core.___slide[i].querySelector('.lg-img-wrap').removeAttribute('data-x');
  208. this.core.___slide[i].querySelector('.lg-img-wrap').removeAttribute('data-y');
  209. }
  210. }
  211. for (var j = 0; j < this.core.___slide.length; j++) {
  212. if (this.core.___slide[j].querySelector('.lg-image')) {
  213. this.core.___slide[j].querySelector('.lg-image').removeAttribute('style');
  214. this.core.___slide[j].querySelector('.lg-image').removeAttribute('data-scale');
  215. }
  216. }
  217. // Reset pagx pagy values to center
  218. this.pageX = window.innerWidth / 2;
  219. this.pageY = window.innerHeight / 2 + (document.documentElement.scrollTop || document.body.scrollTop);
  220. };
  221. Zoom.prototype.zoomSwipe = function () {
  222. var _this = this;
  223. var startCoords = {};
  224. var endCoords = {};
  225. var isMoved = false;
  226. // Allow x direction drag
  227. var allowX = false;
  228. // Allow Y direction drag
  229. var allowY = false;
  230. for (var i = 0; i < _this.core.___slide.length; i++) {
  231. /*jshint loopfunc: true */
  232. utils.on(_this.core.___slide[i], 'touchstart.lg', function (e) {
  233. if (utils.hasClass(_this.core.outer, 'lg-zoomed')) {
  234. var image = _this.core.___slide[_this.core.index].querySelector('.lg-object');
  235. allowY = image.offsetHeight * image.getAttribute('data-scale') > _this.core.outer.querySelector('.lg').clientHeight;
  236. allowX = image.offsetWidth * image.getAttribute('data-scale') > _this.core.outer.querySelector('.lg').clientWidth;
  237. if (allowX || allowY) {
  238. e.preventDefault();
  239. startCoords = {
  240. x: e.targetTouches[0].pageX,
  241. y: e.targetTouches[0].pageY
  242. };
  243. }
  244. }
  245. });
  246. }
  247. for (var j = 0; j < _this.core.___slide.length; j++) {
  248. /*jshint loopfunc: true */
  249. utils.on(_this.core.___slide[j], 'touchmove.lg', function (e) {
  250. if (utils.hasClass(_this.core.outer, 'lg-zoomed')) {
  251. var _el = _this.core.___slide[_this.core.index].querySelector('.lg-img-wrap');
  252. var distanceX;
  253. var distanceY;
  254. e.preventDefault();
  255. isMoved = true;
  256. endCoords = {
  257. x: e.targetTouches[0].pageX,
  258. y: e.targetTouches[0].pageY
  259. };
  260. // reset opacity and transition duration
  261. utils.addClass(_this.core.outer, 'lg-zoom-dragging');
  262. if (allowY) {
  263. distanceY = -Math.abs(_el.getAttribute('data-y')) + (endCoords.y - startCoords.y);
  264. } else {
  265. distanceY = -Math.abs(_el.getAttribute('data-y'));
  266. }
  267. if (allowX) {
  268. distanceX = -Math.abs(_el.getAttribute('data-x')) + (endCoords.x - startCoords.x);
  269. } else {
  270. distanceX = -Math.abs(_el.getAttribute('data-x'));
  271. }
  272. if (Math.abs(endCoords.x - startCoords.x) > 15 || Math.abs(endCoords.y - startCoords.y) > 15) {
  273. _el.style.left = distanceX + 'px';
  274. _el.style.top = distanceY + 'px';
  275. }
  276. }
  277. });
  278. }
  279. for (var k = 0; k < _this.core.___slide.length; k++) {
  280. /*jshint loopfunc: true */
  281. utils.on(_this.core.___slide[k], 'touchend.lg', function () {
  282. if (utils.hasClass(_this.core.outer, 'lg-zoomed')) {
  283. if (isMoved) {
  284. isMoved = false;
  285. utils.removeClass(_this.core.outer, 'lg-zoom-dragging');
  286. _this.touchendZoom(startCoords, endCoords, allowX, allowY);
  287. }
  288. }
  289. });
  290. }
  291. };
  292. Zoom.prototype.zoomDrag = function () {
  293. var _this = this;
  294. var startCoords = {};
  295. var endCoords = {};
  296. var isDraging = false;
  297. var isMoved = false;
  298. // Allow x direction drag
  299. var allowX = false;
  300. // Allow Y direction drag
  301. var allowY = false;
  302. for (var i = 0; i < _this.core.___slide.length; i++) {
  303. /*jshint loopfunc: true */
  304. utils.on(_this.core.___slide[i], 'mousedown.lgzoom', function (e) {
  305. // execute only on .lg-object
  306. var image = _this.core.___slide[_this.core.index].querySelector('.lg-object');
  307. allowY = image.offsetHeight * image.getAttribute('data-scale') > _this.core.outer.querySelector('.lg').clientHeight;
  308. allowX = image.offsetWidth * image.getAttribute('data-scale') > _this.core.outer.querySelector('.lg').clientWidth;
  309. if (utils.hasClass(_this.core.outer, 'lg-zoomed')) {
  310. if (utils.hasClass(e.target, 'lg-object') && (allowX || allowY)) {
  311. e.preventDefault();
  312. startCoords = {
  313. x: e.pageX,
  314. y: e.pageY
  315. };
  316. isDraging = true;
  317. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  318. _this.core.outer.scrollLeft += 1;
  319. _this.core.outer.scrollLeft -= 1;
  320. utils.removeClass(_this.core.outer, 'lg-grab');
  321. utils.addClass(_this.core.outer, 'lg-grabbing');
  322. }
  323. }
  324. });
  325. }
  326. utils.on(window, 'mousemove.lgzoom', function (e) {
  327. if (isDraging) {
  328. var _el = _this.core.___slide[_this.core.index].querySelector('.lg-img-wrap');
  329. var distanceX;
  330. var distanceY;
  331. isMoved = true;
  332. endCoords = {
  333. x: e.pageX,
  334. y: e.pageY
  335. };
  336. // reset opacity and transition duration
  337. utils.addClass(_this.core.outer, 'lg-zoom-dragging');
  338. if (allowY) {
  339. distanceY = -Math.abs(_el.getAttribute('data-y')) + (endCoords.y - startCoords.y);
  340. } else {
  341. distanceY = -Math.abs(_el.getAttribute('data-y'));
  342. }
  343. if (allowX) {
  344. distanceX = -Math.abs(_el.getAttribute('data-x')) + (endCoords.x - startCoords.x);
  345. } else {
  346. distanceX = -Math.abs(_el.getAttribute('data-x'));
  347. }
  348. _el.style.left = distanceX + 'px';
  349. _el.style.top = distanceY + 'px';
  350. }
  351. });
  352. utils.on(window, 'mouseup.lgzoom', function (e) {
  353. if (isDraging) {
  354. isDraging = false;
  355. utils.removeClass(_this.core.outer, 'lg-zoom-dragging');
  356. // Fix for chrome mouse move on click
  357. if (isMoved && (startCoords.x !== endCoords.x || startCoords.y !== endCoords.y)) {
  358. endCoords = {
  359. x: e.pageX,
  360. y: e.pageY
  361. };
  362. _this.touchendZoom(startCoords, endCoords, allowX, allowY);
  363. }
  364. isMoved = false;
  365. }
  366. utils.removeClass(_this.core.outer, 'lg-grabbing');
  367. utils.addClass(_this.core.outer, 'lg-grab');
  368. });
  369. };
  370. Zoom.prototype.touchendZoom = function (startCoords, endCoords, allowX, allowY) {
  371. var _this = this;
  372. var _el = _this.core.___slide[_this.core.index].querySelector('.lg-img-wrap');
  373. var image = _this.core.___slide[_this.core.index].querySelector('.lg-object');
  374. var distanceX = -Math.abs(_el.getAttribute('data-x')) + (endCoords.x - startCoords.x);
  375. var distanceY = -Math.abs(_el.getAttribute('data-y')) + (endCoords.y - startCoords.y);
  376. var minY = (_this.core.outer.querySelector('.lg').clientHeight - image.offsetHeight) / 2;
  377. var maxY = Math.abs(image.offsetHeight * Math.abs(image.getAttribute('data-scale')) - _this.core.outer.querySelector('.lg').clientHeight + minY);
  378. var minX = (_this.core.outer.querySelector('.lg').clientWidth - image.offsetWidth) / 2;
  379. var maxX = Math.abs(image.offsetWidth * Math.abs(image.getAttribute('data-scale')) - _this.core.outer.querySelector('.lg').clientWidth + minX);
  380. if (Math.abs(endCoords.x - startCoords.x) > 15 || Math.abs(endCoords.y - startCoords.y) > 15) {
  381. if (allowY) {
  382. if (distanceY <= -maxY) {
  383. distanceY = -maxY;
  384. } else if (distanceY >= -minY) {
  385. distanceY = -minY;
  386. }
  387. }
  388. if (allowX) {
  389. if (distanceX <= -maxX) {
  390. distanceX = -maxX;
  391. } else if (distanceX >= -minX) {
  392. distanceX = -minX;
  393. }
  394. }
  395. if (allowY) {
  396. _el.setAttribute('data-y', Math.abs(distanceY));
  397. } else {
  398. distanceY = -Math.abs(_el.getAttribute('data-y'));
  399. }
  400. if (allowX) {
  401. _el.setAttribute('data-x', Math.abs(distanceX));
  402. } else {
  403. distanceX = -Math.abs(_el.getAttribute('data-x'));
  404. }
  405. _el.style.left = distanceX + 'px';
  406. _el.style.top = distanceY + 'px';
  407. }
  408. };
  409. Zoom.prototype.destroy = function () {
  410. var _this = this;
  411. // Unbind all events added by lightGallery zoom plugin
  412. utils.off(_this.core.el, '.lgzoom');
  413. utils.off(window, '.lgzoom');
  414. for (var i = 0; i < _this.core.___slide.length; i++) {
  415. utils.off(_this.core.___slide[i], '.lgzoom');
  416. }
  417. utils.off(_this.core.el, '.lgtmzoom');
  418. _this.resetZoom();
  419. clearTimeout(_this.zoomabletimeout);
  420. _this.zoomabletimeout = false;
  421. };
  422. window.lgModules.zoom = Zoom;
  423. });
  424. },{}]},{},[1])(1)
  425. });