No Description

jquery.zoom.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*!
  2. Zoom 1.7.21
  3. license: MIT
  4. http://www.jacklmoore.com/zoom
  5. */
  6. (function ($) {
  7. var defaults = {
  8. url: false,
  9. callback: false,
  10. target: false,
  11. duration: 120,
  12. on: 'mouseover', // other options: grab, click, toggle
  13. touch: true, // enables a touch fallback
  14. onZoomIn: false,
  15. onZoomOut: false,
  16. magnify: 1
  17. };
  18. // Core Zoom Logic, independent of event listeners.
  19. $.zoom = function(target, source, img, magnify) {
  20. var targetHeight,
  21. targetWidth,
  22. sourceHeight,
  23. sourceWidth,
  24. xRatio,
  25. yRatio,
  26. offset,
  27. $target = $(target),
  28. position = $target.css('position'),
  29. $source = $(source);
  30. // The parent element needs positioning so that the zoomed element can be correctly positioned within.
  31. target.style.position = /(absolute|fixed)/.test(position) ? position : 'relative';
  32. target.style.overflow = 'hidden';
  33. img.style.width = img.style.height = '';
  34. $(img)
  35. .addClass('zoomImg')
  36. .css({
  37. position: 'absolute',
  38. top: 0,
  39. left: 0,
  40. opacity: 0,
  41. width: img.width * magnify,
  42. height: img.height * magnify,
  43. border: 'none',
  44. maxWidth: 'none',
  45. maxHeight: '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 && $(settings.target)[0] || this,
  79. //source will provide zoom location info (thumbnail)
  80. source = this,
  81. $source = $(source),
  82. img = document.createElement('img'),
  83. $img = $(img),
  84. mousemove = 'mousemove.zoom',
  85. clicked = false,
  86. touched = false;
  87. // If a url wasn't specified, look for an image element.
  88. if (!settings.url) {
  89. var srcElement = source.querySelector('img');
  90. if (srcElement) {
  91. settings.url = srcElement.getAttribute('data-src') || srcElement.currentSrc || srcElement.src;
  92. settings.alt = srcElement.getAttribute('data-alt') || srcElement.alt;
  93. }
  94. if (!settings.url) {
  95. return;
  96. }
  97. }
  98. $source.one('zoom.destroy', function(position, overflow){
  99. $source.off(".zoom");
  100. target.style.position = position;
  101. target.style.overflow = overflow;
  102. img.onload = null;
  103. $img.remove();
  104. }.bind(this, target.style.position, target.style.overflow));
  105. img.onload = function () {
  106. var zoom = $.zoom(target, source, img, settings.magnify);
  107. function start(e) {
  108. zoom.init();
  109. zoom.move(e);
  110. // Skip the fade-in for IE8 and lower since it chokes on fading-in
  111. // and changing position based on mousemovement at the same time.
  112. $img.stop()
  113. .fadeTo($.support.opacity ? settings.duration : 0, 1, 'function' === typeof settings.onZoomIn ? settings.onZoomIn.call(img) : false);
  114. }
  115. function stop() {
  116. $img.stop()
  117. .fadeTo(settings.duration, 0, 'function' === typeof settings.onZoomOut ? settings.onZoomOut.call(img) : false);
  118. }
  119. // Mouse events
  120. if (settings.on === 'grab') {
  121. $source
  122. .on('mousedown.zoom',
  123. function (e) {
  124. if (e.which === 1) {
  125. $(document).one('mouseup.zoom',
  126. function () {
  127. stop();
  128. $(document).off(mousemove, zoom.move);
  129. }
  130. );
  131. start(e);
  132. $(document).on(mousemove, zoom.move);
  133. e.preventDefault();
  134. }
  135. }
  136. );
  137. } else if (settings.on === 'click') {
  138. $source.on('click.zoom',
  139. function (e) {
  140. if (clicked) {
  141. // bubble the event up to the document to trigger the unbind.
  142. return;
  143. } else {
  144. clicked = true;
  145. start(e);
  146. $(document).on(mousemove, zoom.move);
  147. $(document).one('click.zoom',
  148. function () {
  149. stop();
  150. clicked = false;
  151. $(document).off(mousemove, zoom.move);
  152. }
  153. );
  154. return false;
  155. }
  156. }
  157. );
  158. } else if (settings.on === 'toggle') {
  159. $source.on('click.zoom',
  160. function (e) {
  161. if (clicked) {
  162. stop();
  163. } else {
  164. start(e);
  165. }
  166. clicked = !clicked;
  167. }
  168. );
  169. } else if (settings.on === 'mouseover') {
  170. zoom.init(); // Preemptively call init because IE7 will fire the mousemove handler before the hover handler.
  171. $source
  172. .on('mouseenter.zoom', start)
  173. .on('mouseleave.zoom', stop)
  174. .on(mousemove, zoom.move);
  175. }
  176. // Touch fallback
  177. if (settings.touch) {
  178. $source
  179. .on('touchstart.zoom', function (e) {
  180. e.preventDefault();
  181. if (touched) {
  182. touched = false;
  183. stop();
  184. } else {
  185. touched = true;
  186. start( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
  187. }
  188. })
  189. .on('touchmove.zoom', function (e) {
  190. e.preventDefault();
  191. zoom.move( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
  192. })
  193. .on('touchend.zoom', function (e) {
  194. e.preventDefault();
  195. if (touched) {
  196. touched = false;
  197. stop();
  198. }
  199. });
  200. }
  201. if ('function' === typeof settings.callback) {
  202. settings.callback.call(img);
  203. }
  204. };
  205. img.setAttribute('role', 'presentation');
  206. img.alt = settings.alt || '';
  207. img.src = settings.url;
  208. });
  209. };
  210. $.fn.zoom.defaults = defaults;
  211. }(window.jQuery));