jquery.zoom.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*!
  2. Zoom v1.7.11 - 2013-11-12
  3. Enlarge images on click or mouseover.
  4. (c) 2013 Jack Moore - http://www.jacklmoore.com/zoom
  5. license: http://www.opensource.org/licenses/mit-license.php
  6. */
  7. (function ($) {
  8. var defaults = {
  9. url: false,
  10. callback: false,
  11. target: false,
  12. duration: 120,
  13. on: 'mouseover', // other options: grab, click, toggle
  14. touch: true, // enables a touch fallback
  15. onZoomIn: false,
  16. onZoomOut: false,
  17. magnify: 1
  18. };
  19. // Core Zoom Logic, independent of event listeners.
  20. $.zoom = function(target, source, img, magnify) {
  21. var targetHeight,
  22. targetWidth,
  23. sourceHeight,
  24. sourceWidth,
  25. xRatio,
  26. yRatio,
  27. offset,
  28. position = $(target).css('position');
  29. // The parent element needs positioning so that the zoomed element can be correctly positioned within.
  30. $(target).css({
  31. position: /(absolute|fixed)/.test(position) ? position : 'relative',
  32. overflow: 'hidden'
  33. });
  34. img.style.width = img.style.height = '';
  35. $(img)
  36. .addClass('zoomImg')
  37. .css({
  38. position: 'absolute',
  39. top: 0,
  40. left: 0,
  41. opacity: 0,
  42. width: img.width * magnify,
  43. height: img.height * magnify,
  44. border: 'none',
  45. maxWidth: 'none'
  46. })
  47. .appendTo(target);
  48. return {
  49. init: function() {
  50. targetWidth = $(target).outerWidth();
  51. targetHeight = $(target).outerHeight();
  52. if (source === target) {
  53. sourceWidth = targetWidth;
  54. sourceHeight = targetHeight;
  55. } else {
  56. sourceWidth = $(source).outerWidth();
  57. sourceHeight = $(source).outerHeight();
  58. }
  59. xRatio = (img.width - targetWidth) / sourceWidth;
  60. yRatio = (img.height - targetHeight) / sourceHeight;
  61. offset = $(source).offset();
  62. },
  63. move: function (e) {
  64. var left = (e.pageX - offset.left),
  65. top = (e.pageY - offset.top);
  66. top = Math.max(Math.min(top, sourceHeight), 0);
  67. left = Math.max(Math.min(left, sourceWidth), 0);
  68. img.style.left = (left * -xRatio) + 'px';
  69. img.style.top = (top * -yRatio) + 'px';
  70. }
  71. };
  72. };
  73. $.fn.zoom = function (options) {
  74. return this.each(function () {
  75. var
  76. settings = $.extend({}, defaults, options || {}),
  77. //target will display the zoomed image
  78. target = settings.target || this,
  79. //source will provide zoom location info (thumbnail)
  80. source = this,
  81. img = document.createElement('img'),
  82. $img = $(img),
  83. mousemove = 'mousemove.zoom',
  84. clicked = false,
  85. touched = false,
  86. $urlElement;
  87. // If a url wasn't specified, look for an image element.
  88. if (!settings.url) {
  89. $urlElement = $(source).find('img');
  90. if ($urlElement[0]) {
  91. settings.url = $urlElement.data('src') || $urlElement.attr('src');
  92. }
  93. if (!settings.url) {
  94. return;
  95. }
  96. }
  97. img.onload = function () {
  98. var zoom = $.zoom(target, source, img, settings.magnify);
  99. function start(e) {
  100. zoom.init();
  101. zoom.move(e);
  102. // Skip the fade-in for IE8 and lower since it chokes on fading-in
  103. // and changing position based on mousemovement at the same time.
  104. $img.stop()
  105. .fadeTo($.support.opacity ? settings.duration : 0, 1, $.isFunction(settings.onZoomIn) ? settings.onZoomIn.call(img) : false);
  106. }
  107. function stop() {
  108. $img.stop()
  109. .fadeTo(settings.duration, 0, $.isFunction(settings.onZoomOut) ? settings.onZoomOut.call(img) : false);
  110. }
  111. // Mouse events
  112. if (settings.on === 'grab') {
  113. $(source)
  114. .on('mousedown.zoom',
  115. function (e) {
  116. if (e.which === 1) {
  117. $(document).one('mouseup.zoom',
  118. function () {
  119. stop();
  120. $(document).off(mousemove, zoom.move);
  121. }
  122. );
  123. start(e);
  124. $(document).on(mousemove, zoom.move);
  125. e.preventDefault();
  126. }
  127. }
  128. );
  129. } else if (settings.on === 'click') {
  130. $(source).on('click.zoom',
  131. function (e) {
  132. if (clicked) {
  133. // bubble the event up to the document to trigger the unbind.
  134. return;
  135. } else {
  136. clicked = true;
  137. start(e);
  138. $(document).on(mousemove, zoom.move);
  139. $(document).one('click.zoom',
  140. function () {
  141. stop();
  142. clicked = false;
  143. $(document).off(mousemove, zoom.move);
  144. }
  145. );
  146. return false;
  147. }
  148. }
  149. );
  150. } else if (settings.on === 'toggle') {
  151. $(source).on('click.zoom',
  152. function (e) {
  153. if (clicked) {
  154. stop();
  155. } else {
  156. start(e);
  157. }
  158. clicked = !clicked;
  159. }
  160. );
  161. } else if (settings.on === 'mouseover') {
  162. zoom.init(); // Preemptively call init because IE7 will fire the mousemove handler before the hover handler.
  163. $(source)
  164. .on('mouseenter.zoom', start)
  165. .on('mouseleave.zoom', stop)
  166. .on(mousemove, zoom.move);
  167. }
  168. // Touch fallback
  169. if (settings.touch) {
  170. $(source)
  171. .on('touchstart.zoom', function (e) {
  172. e.preventDefault();
  173. if (touched) {
  174. touched = false;
  175. stop();
  176. } else {
  177. touched = true;
  178. start( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
  179. }
  180. })
  181. .on('touchmove.zoom', function (e) {
  182. e.preventDefault();
  183. zoom.move( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
  184. });
  185. }
  186. if ($.isFunction(settings.callback)) {
  187. settings.callback.call(img);
  188. }
  189. };
  190. img.src = settings.url;
  191. $(source).one('zoom.destroy', function(){
  192. $(source).off(".zoom");
  193. $img.remove();
  194. });
  195. });
  196. };
  197. $.fn.zoom.defaults = defaults;
  198. }(window.jQuery));