lg-zoom.js 17 KB

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