jquery.zoom.js
238 lines
| 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 | |
| 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 | $target = $(target), |
| 29 | position = $target.css('position'), |
| 30 | $source = $(source); |
| 31 | |
| 32 | // The parent element needs positioning so that the zoomed element can be correctly positioned within. |
| 33 | target.style.position = /(absolute|fixed)/.test(position) ? position : 'relative'; |
| 34 | target.style.overflow = 'hidden'; |
| 35 | img.style.width = img.style.height = ''; |
| 36 | |
| 37 | $(img) |
| 38 | .addClass('zoomImg') |
| 39 | .css({ |
| 40 | position: 'absolute', |
| 41 | top: 0, |
| 42 | left: 0, |
| 43 | opacity: 0, |
| 44 | width: img.width * magnify, |
| 45 | height: img.height * magnify, |
| 46 | border: 'none', |
| 47 | maxWidth: 'none', |
| 48 | maxHeight: 'none' |
| 49 | }) |
| 50 | .appendTo(target); |
| 51 | |
| 52 | return { |
| 53 | init: function() { |
| 54 | targetWidth = $target.outerWidth(); |
| 55 | targetHeight = $target.outerHeight(); |
| 56 | |
| 57 | if (source === target) { |
| 58 | sourceWidth = targetWidth; |
| 59 | sourceHeight = targetHeight; |
| 60 | } else { |
| 61 | sourceWidth = $source.outerWidth(); |
| 62 | sourceHeight = $source.outerHeight(); |
| 63 | } |
| 64 | |
| 65 | xRatio = (img.width - targetWidth) / sourceWidth; |
| 66 | yRatio = (img.height - targetHeight) / sourceHeight; |
| 67 | |
| 68 | offset = $source.offset(); |
| 69 | }, |
| 70 | move: function (e) { |
| 71 | var left = (e.pageX - offset.left), |
| 72 | top = (e.pageY - offset.top); |
| 73 | |
| 74 | top = Math.max(Math.min(top, sourceHeight), 0); |
| 75 | left = Math.max(Math.min(left, sourceWidth), 0); |
| 76 | |
| 77 | img.style.left = (left * -xRatio) + 'px'; |
| 78 | img.style.top = (top * -yRatio) + 'px'; |
| 79 | } |
| 80 | }; |
| 81 | }; |
| 82 | |
| 83 | $.fn.zoom = function (options) { |
| 84 | return this.each(function () { |
| 85 | var |
| 86 | settings = $.extend({}, defaults, options || {}), |
| 87 | //target will display the zoomed image |
| 88 | target = settings.target && $(settings.target)[0] || this, |
| 89 | //source will provide zoom location info (thumbnail) |
| 90 | source = this, |
| 91 | $source = $(source), |
| 92 | img = document.createElement('img'), |
| 93 | $img = $(img), |
| 94 | mousemove = 'mousemove.zoom', |
| 95 | clicked = false, |
| 96 | touched = false; |
| 97 | |
| 98 | // If a url wasn't specified, look for an image element. |
| 99 | if (!settings.url) { |
| 100 | var srcElement = source.querySelector('img'); |
| 101 | if (srcElement) { |
| 102 | settings.url = srcElement.getAttribute('data-src') || srcElement.currentSrc || srcElement.src; |
| 103 | settings.alt = srcElement.getAttribute('data-alt') || srcElement.alt; |
| 104 | } |
| 105 | if (!settings.url) { |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $source.one('zoom.destroy', function(position, overflow){ |
| 111 | $source.off(".zoom"); |
| 112 | target.style.position = position; |
| 113 | target.style.overflow = overflow; |
| 114 | img.onload = null; |
| 115 | $img.remove(); |
| 116 | }.bind(this, target.style.position, target.style.overflow)); |
| 117 | |
| 118 | img.onload = function () { |
| 119 | var zoom = $.zoom(target, source, img, settings.magnify); |
| 120 | |
| 121 | function start(e) { |
| 122 | zoom.init(); |
| 123 | zoom.move(e); |
| 124 | |
| 125 | // Skip the fade-in for IE8 and lower since it chokes on fading-in |
| 126 | // and changing position based on mousemovement at the same time. |
| 127 | $img.stop() |
| 128 | .fadeTo($.support.opacity ? settings.duration : 0, 1, 'function' === typeof settings.onZoomIn ? settings.onZoomIn.call(img) : false); |
| 129 | } |
| 130 | |
| 131 | function stop() { |
| 132 | $img.stop() |
| 133 | .fadeTo(settings.duration, 0, 'function' === typeof settings.onZoomOut ? settings.onZoomOut.call(img) : false); |
| 134 | } |
| 135 | |
| 136 | // Mouse events |
| 137 | if (settings.on === 'grab') { |
| 138 | $source |
| 139 | .on('mousedown.zoom', |
| 140 | function (e) { |
| 141 | if (e.which === 1) { |
| 142 | $(document).one('mouseup.zoom', |
| 143 | function () { |
| 144 | stop(); |
| 145 | |
| 146 | $(document).off(mousemove, zoom.move); |
| 147 | } |
| 148 | ); |
| 149 | |
| 150 | start(e); |
| 151 | |
| 152 | $(document).on(mousemove, zoom.move); |
| 153 | |
| 154 | e.preventDefault(); |
| 155 | } |
| 156 | } |
| 157 | ); |
| 158 | } else if (settings.on === 'click') { |
| 159 | $source.on('click.zoom', |
| 160 | function (e) { |
| 161 | if (clicked) { |
| 162 | // bubble the event up to the document to trigger the unbind. |
| 163 | return; |
| 164 | } else { |
| 165 | clicked = true; |
| 166 | start(e); |
| 167 | $(document).on(mousemove, zoom.move); |
| 168 | $(document).one('click.zoom', |
| 169 | function () { |
| 170 | stop(); |
| 171 | clicked = false; |
| 172 | $(document).off(mousemove, zoom.move); |
| 173 | } |
| 174 | ); |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | ); |
| 179 | } else if (settings.on === 'toggle') { |
| 180 | $source.on('click.zoom', |
| 181 | function (e) { |
| 182 | if (clicked) { |
| 183 | stop(); |
| 184 | } else { |
| 185 | start(e); |
| 186 | } |
| 187 | clicked = !clicked; |
| 188 | } |
| 189 | ); |
| 190 | } else if (settings.on === 'mouseover') { |
| 191 | zoom.init(); // Preemptively call init because IE7 will fire the mousemove handler before the hover handler. |
| 192 | |
| 193 | $source |
| 194 | .on('mouseenter.zoom', start) |
| 195 | .on('mouseleave.zoom', stop) |
| 196 | .on(mousemove, zoom.move); |
| 197 | } |
| 198 | |
| 199 | // Touch fallback |
| 200 | if (settings.touch) { |
| 201 | $source |
| 202 | .on('touchstart.zoom', function (e) { |
| 203 | e.preventDefault(); |
| 204 | if (touched) { |
| 205 | touched = false; |
| 206 | stop(); |
| 207 | } else { |
| 208 | touched = true; |
| 209 | start( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] ); |
| 210 | } |
| 211 | }) |
| 212 | .on('touchmove.zoom', function (e) { |
| 213 | e.preventDefault(); |
| 214 | zoom.move( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] ); |
| 215 | }) |
| 216 | .on('touchend.zoom', function (e) { |
| 217 | e.preventDefault(); |
| 218 | if (touched) { |
| 219 | touched = false; |
| 220 | stop(); |
| 221 | } |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | if ('function' === typeof settings.callback) { |
| 226 | settings.callback.call(img); |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | img.setAttribute('role', 'presentation'); |
| 231 | img.alt = settings.alt || ''; |
| 232 | img.src = settings.url; |
| 233 | }); |
| 234 | }; |
| 235 | |
| 236 | $.fn.zoom.defaults = defaults; |
| 237 | }(window.jQuery)); |
| 238 |