lg-zoom.js 15 KB

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