////////////////////////////////////////////////////////////////////////////////// // cloud zoom v1.0.2 // (c) 2010 by r cecco. // mit license // // please retain this copyright header in all versions of the software ////////////////////////////////////////////////////////////////////////////////// (function ($) { 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))).find(':last').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 = jwin.parent().append(format("
", simg.outerwidth(), simg.outerheight(), 0, 0)).find(':last'); ////////////////////////////////////////////////////////////////////// /* 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 = appendto.append(format('', xpos, ypos, w, h, zoomimage.src)).find(':last'); // add the title from title tag. if (simg.attr('title') && opts.showtitle) { zoomdiv.append(format('
%0
', simg.attr('title'))).find(':last').css('opacity', opts.titleopacity); } // fix ie6 select elements wrong z-index bug. placing an iframe over the select element solves the issue... if ($.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 = jwin.append(format("", cw, ch)).find(':last'); $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('