////////////////////////////////////////////////////////////////////////////////// // Cloud Zoom V1.0.3 // Rev 201209181857 // (c) 2010-2012 by R Cecco. // MIT License // // Please retain this copyright header in all versions of the software // ////////////////////////////////////////////////////////////////////////////////// (function ($) { // Get the path of executing script. var scripts = document.getElementsByTagName("script"); var last = scripts[scripts.length-1].src.lastIndexOf('/'); var scriptPath = scripts[scripts.length-1].src.slice(0,last-2); $(document).ready(function () { $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom(); }); function format(str) { for (var i = 1; i < arguments.length; i++) { str = str.replace('%' + (i - 1), arguments[i]); } return str; } function CloudZoom(jWin, opts) { var sImg = $('img', jWin); var img1; var img2; var zoomDiv = null; var $mouseTrap = null; var lens = null; var $tint = null; var softFocus = null; var $ie6Fix = null; var zoomImage; var controlTimer = 0; var cw, ch; var destU = 0; var destV = 0; var currV = 0; var currU = 0; var filesLoaded = 0; var mx, my; var ctx = this, zw; // Display an image loading message. This message gets deleted when the images have loaded and the zoom init function is called. // We add a small delay before the message is displayed to avoid the message flicking on then off again virtually immediately if the // images load really fast, e.g. from the cache. //var ctx = this; setTimeout(function () { if ($mouseTrap === null) { var w = jWin.width(); jWin.parent().append(format('
Loading...
', w / 3, (w / 2) - (w / 6))); $('cloud-zoom-loading', jWin.parent()).css('opacity', 0.5) } }, 200); var ie6FixRemove = function () { if ($ie6Fix !== null) { $ie6Fix.remove(); $ie6Fix = null; } }; // Removes cursor, tint layer, blur layer etc. this.removeBits = function () { //$mouseTrap.unbind(); if (lens) { lens.remove(); lens = null; } if ($tint) { $tint.remove(); $tint = null; } if (softFocus) { softFocus.remove(); softFocus = null; } ie6FixRemove(); $('.cloud-zoom-loading', jWin.parent()).remove(); }; this.destroy = function () { jWin.data('zoom', null); if ($mouseTrap) { $mouseTrap.unbind(); $mouseTrap.remove(); $mouseTrap = null; } if (zoomDiv) { zoomDiv.remove(); zoomDiv = null; } //ie6FixRemove(); this.removeBits(); // DON'T FORGET TO REMOVE JQUERY 'DATA' VALUES }; // This is called when the zoom window has faded out so it can be removed. this.fadedOut = function () { if (zoomDiv) { zoomDiv.remove(); zoomDiv = null; } this.removeBits(); //ie6FixRemove(); }; this.controlLoop = function () { if (lens) { var x = (mx - sImg.offset().left - (cw * 0.5)) >> 0; var y = (my - sImg.offset().top - (ch * 0.5)) >> 0; if (x < 0) { x = 0; } else if (x > (sImg.outerWidth() - cw)) { x = (sImg.outerWidth() - cw); } if (y < 0) { y = 0; } else if (y > (sImg.outerHeight() - ch)) { y = (sImg.outerHeight() - ch); } lens.css({ left: x, top: y }); lens.css('background-position', (-x) + 'px ' + (-y) + 'px'); destU = (((x) / sImg.outerWidth()) * zoomImage.width) >> 0; destV = (((y) / sImg.outerHeight()) * zoomImage.height) >> 0; currU += (destU - currU) / opts.smoothMove; currV += (destV - currV) / opts.smoothMove; zoomDiv.css('background-position', (-(currU >> 0) + 'px ') + (-(currV >> 0) + 'px')); } controlTimer = setTimeout(function () { ctx.controlLoop(); }, 30); }; this.init2 = function (img, id) { filesLoaded++; //console.log(img.src + ' ' + id + ' ' + img.width); if (id === 1) { zoomImage = img; } //this.images[id] = img; if (filesLoaded === 2) { this.init(); } }; /* Init function start. */ this.init = function () { // Remove loading message (if present); $('.cloud-zoom-loading', jWin.parent()).remove(); /* Add a box (mouseTrap) over the small image to trap mouse events. It has priority over zoom window to avoid issues with inner zoom. We need the dummy background image as IE does not trap mouse events on transparent parts of a div. */ $mouseTrap = $('.mousetrap', jWin.parent()); if (!$mouseTrap.length){ var $m = $("
"); $mouseTrap = jWin.parent().append($m).find(':last'); $mouseTrap = $('.mousetrap', jWin.parent()); } $mouseTrap.css({ width:sImg.outerWidth(), height:sImg.outerHeight(), top:0, left:0, position:'absolute', zIndex:9999 }); ////////////////////////////////////////////////////////////////////// /* Do as little as possible in mousemove event to prevent slowdown. */ $mouseTrap.bind('mousemove', this, function (event) { // Just update the mouse position mx = event.pageX; my = event.pageY; }); ////////////////////////////////////////////////////////////////////// $mouseTrap.bind('mouseleave', this, function (event) { clearTimeout(controlTimer); //event.data.removeBits(); if(lens) { lens.fadeOut(299); } if($tint) { $tint.fadeOut(299); } if(softFocus) { softFocus.fadeOut(299); } zoomDiv.fadeOut(300, function () { ctx.fadedOut(); }); return false; }); ////////////////////////////////////////////////////////////////////// $mouseTrap.bind('mouseenter', this, function (event) { mx = event.pageX; my = event.pageY; zw = event.data; if (zoomDiv) { zoomDiv.stop(true, false); zoomDiv.remove(); } var xPos = opts.adjustX, yPos = opts.adjustY; var siw = sImg.outerWidth(); var sih = sImg.outerHeight(); var w = opts.zoomWidth; var h = opts.zoomHeight; if (opts.zoomWidth == 'auto') { w = siw; } if (opts.zoomHeight == 'auto') { h = sih; } //$('#info').text( xPos + ' ' + yPos + ' ' + siw + ' ' + sih ); var appendTo = jWin.parent(); // attach to the wrapper switch (opts.position) { case 'top': yPos -= h; // + opts.adjustY; break; case 'right': xPos += siw; // + opts.adjustX; break; case 'bottom': yPos += sih; // + opts.adjustY; break; case 'left': xPos -= w; // + opts.adjustX; break; case 'inside': w = siw; h = sih; break; // All other values, try and find an id in the dom to attach to. default: appendTo = $('#' + opts.position); // If dom element doesn't exit, just use 'right' position as default. if (!appendTo.length) { appendTo = jWin; xPos += siw; //+ opts.adjustX; yPos += sih; // + opts.adjustY; } else { w = appendTo.innerWidth(); h = appendTo.innerHeight(); } } zoomDiv = $('.cloud-zoom-big', appendTo); if (zoomDiv.length){ zoomDiv.replaceWith(format('', xPos, yPos, w, h, zoomImage.src)).find(':last'); }else{ zoomDiv = appendTo.append(format('', xPos, yPos, w, h, zoomImage.src)).find(':last'); } zoomDiv = $('.cloud-zoom-big', appendTo); // Add the title from title tag. if (sImg.attr('title') && opts.showTitle) { zoomDiv.append(format('
%0
', sImg.attr('title'))); $('.cloud-zoom-title', zoomDiv).css('opacity', opts.titleOpacity); } // Fix ie6 select elements wrong z-index bug. Placing an iFrame over the select element solves the issue... if ($.browser && $.browser.msie && $.browser.version < 7) { $ie6Fix = $('').css({ position: "absolute", left: xPos, top: yPos, zIndex: 99, width: w, height: h }).insertBefore(zoomDiv); } zoomDiv.fadeIn(500); if (lens) { lens.remove(); lens = null; } /* Work out size of cursor */ cw = (sImg.outerWidth() / zoomImage.width) * zoomDiv.width(); ch = (sImg.outerHeight() / zoomImage.height) * zoomDiv.height(); // Attach mouse, initially invisible to prevent first frame glitch lens = $('.cloud-zoom-lens', jWin); if (lens.length){ lens.replaceWith(format("", cw, ch)).find(':last'); }else{ lens = jWin.append(format("", cw, ch)).find(':last'); } lens = $('.cloud-zoom-lens', jWin); $mouseTrap.css('cursor', lens.css('cursor')); var noTrans = false; // Init tint layer if needed. (Not relevant if using inside mode) if (opts.tint) { lens.css('background', 'url("' + sImg.attr('src') + '")'); $tint = jWin.append(format('