// zoom 1.4 - jquery image zooming plugin // (c) 2012 jack moore - jacklmoore.com // license: www.opensource.org/licenses/mit-license.php (function ($) { var defaults = { url: false, icon: true, callback: false, target: false, duration: 120, on: 'mouseover' // other options: 'grab' or 'click' }; $.fn.zoom = function (options) { return this.each(function () { var settings = $.extend({}, defaults, options || {}), //target will display the zoomed iamge target = settings.target || this, $target = $(target), //source will provide zoom location info (thumbnail) source = this, $source = $(source), img = new image(), $img = $(img), $icon, position = $target.css('position'), mousemove = 'mousemove', clicked = false; // the parent element needs positioning so that the zoomed element can be correctly positioned within. $target.css({ position: /(absolute|fixed)/.test(position) ? position : 'relative', overflow: 'hidden' }); // if a url wasn't specified, look for an image element. if (!settings.url) { settings.url = $source.find('img').attr('src'); if (!settings.url) { return; } } if (settings.icon) { $icon = $('
').appendto($source); } img.onload = function () { var outerwidth, outerheight, xratio, yratio, left, top, offset = $source.offset(); function ratio() { outerwidth = $target.outerwidth(); outerheight = $target.outerheight(); xratio = (img.width - outerwidth) / $source.outerwidth(); yratio = (img.height - outerheight) / $source.outerheight(); } function move(e) { left = (e.pagex - offset.left); top = (e.pagey - offset.top); if (left > outerwidth) { left = outerwidth; } else if (left < 0) { left = 0; } if (top > outerheight) { top = outerheight; } else if (top < 0) { top = 0; } img.style.left = (left * -xratio) + 'px'; img.style.top = (top * -yratio) + 'px'; e.preventdefault(); } function start(e) { offset = $source.offset(); ratio(); move(e); // skip the fade-in for ie8 and lower since it chokes on fading-in // and changing position based on mousemovement at the same time. $img.stop() .fadeto($.support.opacity ? settings.duration : 0, 1); } function stop() { $img.stop() .fadeto(settings.duration, 0); } $img .addclass('zoomimg') .css({ position: 'absolute', top: 0, left: 0, opacity: 0, width: img.width, height: img.height, border: 'none', maxwidth: 'none' }) .appendto($target); if (settings.on === 'grab') { $source.mousedown( function (e) { offset = $source.offset(); $(document).one('mouseup', function () { stop(); $(document).unbind(mousemove, move); } ); start(e); $(document)[mousemove](move); e.preventdefault(); } ); } else if (settings.on === 'click') { $source.click( function (e) { if (clicked) { // bubble the event up to the document to trigger the unbind. return; } else { clicked = true; start(e); $(document)[mousemove](move); $(document).one('click', function () { stop(); clicked = false; $(document).unbind(mousemove, move); } ); return false; } } ); } else { ratio(); // pre-emptively call ratio because ie7 will fire the mousemove callback before the hover callback. $source.hover( start, stop )[mousemove](move); } if ($.isfunction(settings.callback)) { settings.callback.call(img); } }; img.src = settings.url; }); }; $.fn.zoom.defaults = defaults; }(window.jquery));