////////////////////////////////////////////////////////////////////////////////// // 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 formaturl(str) { return str.replace(/[\']+/g,""); } 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 l = $('.mousetrap').position().left; var t = $('.mousetrap').position().top; var x = (mx - simg.offset().left + l - (cw * 0.5)) >> 0; var y = (my - simg.offset().top + t - (ch * 0.5)) >> 0; if (x < l) { x = l; } else if (x > (simg.outerwidth() + l - cw)) { x = (simg.outerwidth() + l - cw); } if (y < t) { y = t; } else if (y > (simg.outerheight() + t - ch)) { y = (simg.outerheight() + t - ch); } lens.css({ left: x, top: y }); lens.css('background-position', (-x) + 'px ' + (-y) + 'px'); destu = (((x-l) / simg.outerwidth()) * zoomimage.width) >> 0; destv = (((y-t) / simg.outerheight()) * zoomimage.height) >> 0; curru += (destu - curru+l) / opts.smoothmove; currv += (destv - currv+t) / 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 () {//debugger // 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()).remove(); $mousetrap = jwin.parent().append(format("
", simg.outerwidth(), simg.outerheight())).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) { if (lens) { lens.remove(); lens = null; } /* work out size of cursor */ if (zoomdiv) { zoomdiv.stop(true, false); zoomdiv.remove(); } if(img2.width (atop-dtop)) && ((atop-dtop+sih/2) < dheight/2))//èç¹ûí¼æ¬éïãæµäïô꾿õ¼ä²»×㣬²¢çòí¼æ¬î»óúæá仵äéï°ë²¿·ö£¬ôò·åµ½í¼æ¬ïâ·½ïôê¾ opts.position = 'bottom'; break; case 'right': if(((dwidth-(aleft-dleft+siw)) < (w + xpos)) &&((aleft-dleft+siw/2) > (dwidth/2))) opts.position = 'left'; break; case 'bottom': if(((h+ypos) > (dheight-(atop-dtop+sih))) && ((atop-dtop+sih/2) > dheight/2))//èç¹ûí¼æ¬ïâãæµäïô꾿õ¼ä²»×㣬²¢çòí¼æ¬î»óúæá仵äïâ°ë²¿·ö£¬ôò·åµ½í¼æ¬éï·½ïôê¾ opts.position = 'top'; break; case 'left': if(((aleft-dleft) < (w + xpos)) &&((aleft-dleft+siw/2) < (dwidth/2))) opts.position = 'right'; break; } switch (opts.position) { case 'top': ypos = -ypos - h; // + opts.adjusty; break; case 'right': xpos = xpos + siw; // + opts.adjustx; break; case 'bottom': ypos = ypos + sih; // + opts.adjusty; break; case 'left': xpos = -xpos - w; // + opts.adjustx; break; case 'inside': w = siw; h = sih; xpos = xpos + l; ypos = ypos + t; 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); 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('
', simg.outerwidth(), simg.outerheight(), opts.tint)).find(':last'); $tint.css('opacity', opts.tintopacity); notrans = true; $tint.fadein(500); } if (opts.softfocus) { lens.css('background', 'url("' + simg.attr('src') + '")'); softfocus = jwin.append(format('
', simg.outerwidth() - 2, simg.outerheight() - 2, opts.tint)).find(':last'); softfocus.css('background', 'url("' + simg.attr('src') + '")'); softfocus.css('opacity', 0.5); notrans = true; softfocus.fadein(500); } if (!notrans) { lens.css('opacity', opts.lensopacity); } if ( opts.position !== 'inside' ) { lens.fadein(500); } // start processing. zw.controlloop(); return; // don't return false here otherwise opera will not detect change of the mouse pointer type. }); }; img1 = new image(); $(img1).load(function () { ctx.init2(this, 0); }); img1.src = formaturl(simg.attr('src')); img2 = new image(); $(img2).load(function () { ctx.init2(this, 1); }); img2.src = formaturl(jwin.attr('href')); } $.fn.cloudzoom = function (options) { // ie6 background image flicker fix try { document.execcommand("backgroundimagecache", false, true); } catch (e) {} this.each(function () { var relopts, opts; // hmm...eval...slap on wrist. eval('var a = {' + $(this).attr('rel') + '}'); relopts = a; if ($(this).is('.cloud-zoom')) { $(this).css({ 'position': 'relative', 'display': 'block' }); $('img', $(this)).css({ 'display': 'block' }); // wrap an outer div around the link so we can attach things without them becoming part of the link. // but not if wrap already exists. if ($(this).parent().attr('id') != 'wrap') { $(this).wrap('
'); } opts = $.extend({}, $.fn.cloudzoom.defaults, options); opts = $.extend({}, opts, relopts); $(this).data('zoom', new cloudzoom($(this), opts)); } else if ($(this).is('.cloud-zoom-gallery')) { opts = $.extend({}, relopts, options); $(this).data('relopts', opts); $(this).bind('click', $(this), function (event) { var data = event.data.data('relopts'); // destroy the previous zoom $('#' + data.usezoom).data('zoom').destroy(); // change the biglink to point to the new big image. $('#' + data.usezoom).attr('href', event.data.attr('href')); // change the small image to point to the new small image. $('#' + data.usezoom + ' img').attr('src', event.data.data('relopts').smallimage); // init a new zoom with the new images. $('#' + event.data.data('relopts').usezoom).cloudzoom(); return false; }); } }); return this; }; $.fn.cloudzoom.defaults = { zoomwidth: 'auto', zoomheight: 'auto', position: 'right', tint: false, tintopacity: 0.5, lensopacity: 0.5, softfocus: false, smoothmove: 3, showtitle: true, titleopacity: 0.5, adjustx: 0, adjusty: 0, minwidth:1200, minheight:1200 }; })(jquery);