diff options
Diffstat (limited to 'js/jquery.jqzoom-core.js')
-rw-r--r-- | js/jquery.jqzoom-core.js | 375 |
1 files changed, 93 insertions, 282 deletions
diff --git a/js/jquery.jqzoom-core.js b/js/jquery.jqzoom-core.js index 7ce4ecc..481e428 100644 --- a/js/jquery.jqzoom-core.js +++ b/js/jquery.jqzoom-core.js @@ -1,10 +1,10 @@ /*! - * jQzoom Evolution Library v2.1 - Javascript Image magnifier + * jQzoom Evolution Library v2.2 - Javascript Image magnifier * http://www.mind-projects.it * * Copyright 2011, Engineer Marco Renzi * Licensed under the BSD license. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright @@ -14,19 +14,16 @@ * documentation and/or other materials provided with the distribution. * * Neither the name of the organization nor the * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. + * derived from this software without specific prior written permission. * - * Date: 21 March 2011 09:26:00 + * Date: 11 April 2011 22:16:00 */ - - (function ($) { //GLOBAL VARIABLES var isIE6 = ($.browser.msie && $.browser.version < 7); var body = $(document.body); var window = $(window); var jqzoompluging_disabled = false; //disabilita globalmente il plugin - $.fn.jqzoom = function (options) { return this.each(function () { var node = this.nodeName.toLowerCase(); @@ -34,14 +31,11 @@ new jqzoom(this, options); } }); - } - + }; jqzoom = function (el, options) { var api = null; api = $(el).data("jqzoom"); if (api) return api; - - var obj = this; var settings = $.extend({}, $.jqzoom.defaults, options || {}); obj.el = el; @@ -52,81 +46,61 @@ el.largeimageloading = false; //tell us if large image is loading el.largeimageloaded = false; //tell us if large image is loaded el.scale = {}; + el.timer = null; el.mousepos = {}; el.mouseDown = false; $(el).css({ 'outline-style': 'none', 'text-decoration': 'none' }); - //BASE IMAGE var img = $("img:eq(0)", el); el.title = $(el).attr('title'); el.imagetitle = img.attr('title'); - var zoomtitle = ($.trim(el.title).length > 0) ? el.title : el.imagetitle; var smallimage = new Smallimage(img); var lens = new Lens(); var stage = new Stage(); var largeimage = new Largeimage(); var loader = new Loader(); - - - //preventing default click,allowing the onclick event [exmple: lightbox] $(el).bind('click', function (e) { e.preventDefault(); return false; }); - //setting the default zoomType if not in settings var zoomtypes = ['standard', 'drag', 'innerzoom', 'reverse']; if ($.inArray($.trim(settings.zoomType), zoomtypes) < 0) { settings.zoomType = 'standard'; } - - $.extend(obj, { - create: function () { //create the main objects - //create ZoomPad if ($(".zoomPad", el).length == 0) { el.zoomPad = $('<div/>').addClass('zoomPad'); img.wrap(el.zoomPad); } - - //creating ZoomPup if ($(".zoomPup", el).length == 0) { lens.append(); } - //creating zoomWindow if ($(".zoomWindow", el).length == 0) { stage.append(); } - //creating Preload if ($(".zoomPreload", el).length == 0) { loader.append(); } - - //preloading images if (settings.preloadImages || settings.zoomType == 'drag' || settings.alwaysOn) { obj.load(); } - obj.init(); - - }, init: function () { //drag option if (settings.zoomType == 'drag') { - - $(".zoomPad", el).mousedown(function () { el.mouseDown = true; }); @@ -135,48 +109,41 @@ }); document.body.ondragstart = function () { return false; - } + }; $(".zoomPad", el).css({ cursor: 'default' }); $(".zoomPup", el).css({ cursor: 'move' }); - } - if (settings.zoomType == 'innerzoom') { $(".zoomWrapper", el).css({ cursor: 'crosshair' }); } - - - $(".zoomPad", el).bind('mouseenter mouseover', function (event) { - img.attr('title', ''); $(el).attr('title', ''); el.zoom_active = true; //if loaded then activate else load large image + smallimage.fetchdata(); if (el.largeimageloaded) { obj.activate(event); - } else { + } + else { obj.load(); } - - - }); - - ////////////////////////////////////////////////////////////////////// - $(".zoomPad", el).bind('mouseleave', this, function (event) { + $(".zoomPad", el).bind('mouseleave', function (event) { obj.deactivate(); - el.zoom_active = false; - }); - $(".zoomPad", el).bind('mousemove', function (e) { + //prevent fast mouse mevements not to fire the mouseout event + if (e.pageX > smallimage.pos.r || e.pageX < smallimage.pos.l || e.pageY < smallimage.pos.t || e.pageY > smallimage.pos.b) { + lens.setcenter(); + return false; + } el.zoom_active = true; if (el.largeimageloaded && !$('.zoomWindow', el).is(':visible')) { obj.activate(e); @@ -185,31 +152,23 @@ lens.setposition(e); } }); - - - var thumb_preload = new Array(); var i = 0; //binding click event on thumbnails var thumblist = new Array(); thumblist = $('a').filter(function () { var regex = new RegExp("gallery[\\s]*:[\\s]*'" + $.trim(el.rel) + "'", "i"); - var rel = $(this).attr('rel'); if (regex.test(rel)) { return this; } }); - if (thumblist.length > 0) { //getting the first to the last var first = thumblist.splice(0, 1); thumblist.push(first); } - - thumblist.each(function () { - //preloading thumbs if (settings.preloadImages) { var thumb_options = $.extend({}, eval("(" + $.trim($(this).attr('rel')) + ")")); @@ -217,40 +176,30 @@ thumb_preload[i].src = thumb_options.largeimage; i++; } - - - $(this).click(function (e) { - thumblist.each(function () { $(this).removeClass('zoomThumbActive'); }); - e.preventDefault() - + e.preventDefault(); obj.swapimage(this); return false; }); }); - - }, - load: function () { if (el.largeimageloaded == false && el.largeimageloading == false) { var url = $(el).attr('href'); el.largeimageloading = true; largeimage.loadimage(url); } - - }, activate: function (e) { + clearTimeout(el.timer); //show lens and zoomWindow lens.show(); stage.show(); }, deactivate: function (e) { - switch (settings.zoomType) { case 'drag': //nothing or lens.setcenter(); @@ -260,18 +209,16 @@ $(el).attr('title', el.title); if (settings.alwaysOn) { lens.setcenter(); - } else { - - lens.hide(); + } + else { stage.hide(); + lens.hide(); } - break; } - + el.zoom_active = false; }, swapimage: function (link) { - el.largeimageloading = false; el.largeimageloaded = false; var options = new Object(); @@ -282,42 +229,32 @@ $(link).addClass('zoomThumbActive'); $(el).attr('href', largeimage); img.attr('src', smallimage); - lens.hide(); stage.hide(); obj.load(); - - } else { + } + else { alert('ERROR :: Missing parameter for largeimage or smallimage.'); throw 'ERROR :: Missing parameter for largeimage or smallimage.'; } return false; } - }); - - - //sometimes image is already loaded and onload will not fire if (img[0].complete) { //fetching data from sallimage if was previously loaded smallimage.fetchdata(); if ($(".zoomPad", el).length == 0) obj.create(); - } - - /*========================================================, | Smallimage |---------------------------------------------------------: -| Base image that is the smallest one +| Base image into the anchor element `========================================================*/ function Smallimage(image) { var $obj = this; - this.node = image[0]; - this.findborder = function () { var bordertop = 0; bordertop = image.css('border-top-width'); @@ -325,49 +262,44 @@ var borderleft = 0; borderleft = image.css('border-left-width'); bleft = ''; - if (bordertop) { for (i = 0; i < 3; i++) { var x = []; x = bordertop.substr(i, 1); - if (isNaN(x) == false) { btop = btop + '' + bordertop.substr(i, 1); - } else { + } + else { break; } } } - if (borderleft) { for (i = 0; i < 3; i++) { if (!isNaN(borderleft.substr(i, 1))) { bleft = bleft + borderleft.substr(i, 1) - } else { + } + else { break; } } } $obj.btop = (btop.length > 0) ? eval(btop) : 0; $obj.bleft = (bleft.length > 0) ? eval(bleft) : 0; - - }; this.fetchdata = function () { + $obj.findborder(); $obj.w = image.width(); $obj.h = image.height(); $obj.ow = image.outerWidth(); $obj.oh = image.outerHeight(); - $obj.pos = image.offset(); - $obj.pos.l = image.offset().left; - $obj.pos.t = image.offset().top; + $obj.pos.l = image.offset().left + $obj.bleft; + $obj.pos.t = image.offset().top + $obj.btop; $obj.pos.r = $obj.w + $obj.pos.l; $obj.pos.b = $obj.h + $obj.pos.t; - - - - $obj.findborder(); + $obj.rightlimit = image.offset().left + $obj.ow; + $obj.bottomlimit = image.offset().top + $obj.oh; }; this.node.onerror = function () { alert('Problems while loading image.'); @@ -379,8 +311,6 @@ }; return $obj; }; - - /*========================================================, | Loader |---------------------------------------------------------: @@ -388,8 +318,7 @@ `========================================================*/ function Loader() { - var $obj = this - + var $obj = this; this.append = function () { this.node = $('<div/>').addClass('zoomPreload').css('visibility', 'hidden').html(settings.preloadText); $('.zoomPad', el).append(this.node); @@ -397,8 +326,6 @@ this.show = function () { this.node.top = (smallimage.oh - this.node.height()) / 2; this.node.left = (smallimage.ow - this.node.width()) / 2; - - //setting position this.node.css({ top: this.node.top, @@ -409,13 +336,9 @@ }; this.hide = function () { this.node.css('visibility', 'hidden'); - } + }; return this; } - - - - /*========================================================, | Lens |---------------------------------------------------------: @@ -435,14 +358,10 @@ } }; this.setdimensions = function () { - - this.node.w = (settings.zoomWidth) / el.scale.x; - this.node.h = (settings.zoomHeight) / el.scale.y; - + this.node.w = (parseInt((settings.zoomWidth) / el.scale.x) > smallimage.w) ? smallimage.w : parseInt((settings.zoomWidth) / el.scale.x); + this.node.h = (parseInt((settings.zoomHeight) / el.scale.y) > smallimage.h) ? smallimage.h : parseInt((settings.zoomHeight) / el.scale.y); this.node.top = (smallimage.oh - this.node.h - 2) / 2; this.node.left = (smallimage.ow - this.node.w - 2) / 2; - - //centering lens this.node.css({ top: 0, @@ -453,8 +372,6 @@ display: 'none', borderWidth: 1 + 'px' }); - - if (settings.zoomType == 'reverse') { this.image.src = smallimage.node.src; $(this.node).css({ @@ -465,24 +382,17 @@ left: -(this.node.left + 1 - smallimage.bleft) + 'px', top: -(this.node.top + 1 - smallimage.btop) + 'px' }); - } - - }; - this.setcenter = function () { - //calculating center position this.node.top = (smallimage.oh - this.node.h - 2) / 2; this.node.left = (smallimage.ow - this.node.w - 2) / 2; - //centering lens this.node.css({ top: this.node.top, left: this.node.left }); - if (settings.zoomType == 'reverse') { $(this.image).css({ position: 'absolute', @@ -490,81 +400,50 @@ top: -(this.node.top + 1 - smallimage.btop) + 'px' }); } - - //centering large image largeimage.setposition(); - }; - - this.setposition = function (e) { - el.mousepos.x = e.pageX; el.mousepos.y = e.pageY; var lensleft = 0; var lenstop = 0; function overleft(lens) { - return el.mousepos.x - (lens.w) / 2 - smallimage.bleft < smallimage.pos.l; + return el.mousepos.x - (lens.w) / 2 < smallimage.pos.l; //el.mousepos.x - (lens.w) / 2 - smallimage.bleft < } function overright(lens) { - return el.mousepos.x + (lens.w) / 2 > smallimage.pos.r + smallimage.bleft; + return el.mousepos.x + (lens.w) / 2 > smallimage.pos.r; //+ smallimage.bleft } function overtop(lens) { - return el.mousepos.y - (lens.h) / 2 < smallimage.pos.t + smallimage.btop; + return el.mousepos.y - (lens.h) / 2 < smallimage.pos.t; // + smallimage.btop; } function overbottom(lens) { - return el.mousepos.y + (lens.h) / 2 > smallimage.pos.b + smallimage.btop; + return el.mousepos.y + (lens.h) / 2 > smallimage.pos.b; // + smallimage.btop; } - - lensleft = el.mousepos.x - smallimage.pos.l - (this.node.w + 2) / 2; - lenstop = el.mousepos.y - smallimage.pos.t - (this.node.h + 2) / 2; - - + lensleft = el.mousepos.x + smallimage.bleft - smallimage.pos.l - (this.node.w + 2) / 2; + lenstop = el.mousepos.y + smallimage.btop - smallimage.pos.t - (this.node.h + 2) / 2; if (overleft(this.node)) { - lensleft = smallimage.btop - 1; - } else - - if (overright(this.node)) { - - if ($.browser.mozilla) { - lensleft = smallimage.w + smallimage.bleft - this.node.w - 1; - } else { - lensleft = smallimage.w + smallimage.bleft - this.node.w; - } - + lensleft = smallimage.bleft - 1; + } + else if (overright(this.node)) { + lensleft = smallimage.w + smallimage.bleft - this.node.w - 1; } - - if (overtop(this.node)) { lenstop = smallimage.btop - 1; - } else - - if (overbottom(this.node)) { - - - if ($.browser.mozilla) { - lenstop = smallimage.h + smallimage.btop - this.node.h - 1; - } else { - lenstop = smallimage.h + smallimage.btop - this.node.h; - } - - } - - - + else if (overbottom(this.node)) { + lenstop = smallimage.h + smallimage.btop - this.node.h - 1; + } this.node.left = lensleft; this.node.top = lenstop; this.node.css({ 'left': lensleft + 'px', 'top': lenstop + 'px' }); - if (settings.zoomType == 'reverse') { $(this.image).css({ position: 'absolute', @@ -572,10 +451,7 @@ top: -(this.node.top + 1 - smallimage.btop) + 'px' }); } - largeimage.setposition(); - - }; this.hide = function () { img.css({ @@ -597,15 +473,10 @@ var o = {}; o.left = $obj.node.left; o.top = $obj.node.top; - return o; }; return this; }; - - - - /*========================================================, | Stage |---------------------------------------------------------: @@ -617,27 +488,25 @@ this.node = $("<div class='zoomWindow'><div class='zoomWrapper'><div class='zoomWrapperTitle'></div><div class='zoomWrapperImage'></div></div></div>"); this.ieframe = $('<iframe class="zoomIframe" src="javascript:\'\';" marginwidth="0" marginheight="0" align="bottom" scrolling="no" frameborder="0" ></iframe>'); this.setposition = function () { - this.node.leftpos = 0; this.node.toppos = 0; if (settings.zoomType != 'innerzoom') { - //positioning switch (settings.position) { case "left": - this.node.leftpos = (smallimage.pos.l - smallimage.bleft - Math.abs(settings.xOffset) - settings.zoomWidth > 0) ? (0 - settings.zoomWidth - Math.abs(settings.xOffset)) : (smallimage.w + 2 * smallimage.bleft + Math.abs(settings.xOffset)); + this.node.leftpos = (smallimage.pos.l - smallimage.bleft - Math.abs(settings.xOffset) - settings.zoomWidth > 0) ? (0 - settings.zoomWidth - Math.abs(settings.xOffset)) : (smallimage.ow + Math.abs(settings.xOffset)); this.node.toppos = Math.abs(settings.yOffset); break; case "top": - this.node.leftpos = Math.abs(settings.yOffset); - this.node.toppos = (smallimage.pos.t - smallimage.btop - Math.abs(settings.yOffset) - settings.zoomHeight > 0) ? (0 - settings.zoomHeight - Math.abs(settings.yOffset)) : (smallimage.h + 2 * smallimage.btop + Math.abs(settings.yOffset)); + this.node.leftpos = Math.abs(settings.xOffset); + this.node.toppos = (smallimage.pos.t - smallimage.btop - Math.abs(settings.yOffset) - settings.zoomHeight > 0) ? (0 - settings.zoomHeight - Math.abs(settings.yOffset)) : (smallimage.oh + Math.abs(settings.yOffset)); break; case "bottom": - this.node.leftpos = Math.abs(settings.yOffset); - this.node.toppos = (smallimage.pos.t + smallimage.w + 2 * smallimage.btop + Math.abs(settings.yOffset) + settings.zoomHeight < screen.height) ? (smallimage.h + 2 * smallimage.btop + Math.abs(settings.yOffset)) : (0 - settings.zoomHeight - Math.abs(settings.yOffset)); + this.node.leftpos = Math.abs(settings.xOffset); + this.node.toppos = (smallimage.pos.t - smallimage.btop + smallimage.oh + Math.abs(settings.yOffset) + settings.zoomHeight < screen.height) ? (smallimage.oh + Math.abs(settings.yOffset)) : (0 - settings.zoomHeight - Math.abs(settings.yOffset)); break; default: - this.node.leftpos = (smallimage.pos.l + 2 * smallimage.bleft + smallimage.w + Math.abs(settings.xOffset) + settings.zoomWidth < screen.width) ? (smallimage.w + 2 * smallimage.bleft + Math.abs(settings.xOffset)) : (0 - settings.zoomWidth - Math.abs(settings.xOffset)); + this.node.leftpos = (smallimage.rightlimit + Math.abs(settings.xOffset) + settings.zoomWidth < screen.width) ? (smallimage.ow + Math.abs(settings.xOffset)) : (0 - settings.zoomWidth - Math.abs(settings.xOffset)); this.node.toppos = Math.abs(settings.yOffset); break; } @@ -649,22 +518,17 @@ return this; }; this.append = function () { - $('.zoomPad', el).append(this.node); - this.node.css({ position: 'absolute', display: 'none', zIndex: 5001 }); - - if (settings.zoomType == 'innerzoom') { - this.node.css({ cursor: 'default' }); - var thickness = (smallimage.bleft == 0) ? 1 : smallimage.bleft + var thickness = (smallimage.bleft == 0) ? 1 : smallimage.bleft; $('.zoomWrapper', this.node).css({ width: smallimage.w + 'px', borderWidth: thickness + 'px' @@ -673,82 +537,57 @@ width: '100%', height: smallimage.h + 'px' }); - $('.zoomWrapperTitle', this.node).css({ width: '100%', position: 'absolute' }); - } else { - - + } + else { $('.zoomWrapper', this.node).css({ width: Math.round(settings.zoomWidth) + 'px' }); - $('.zoomWrapperImage', this.node).css({ width: '100%', height: Math.round(settings.zoomHeight) + 'px' }); - - - - this.node.bind('mouseover mousemove mouseenter', function (event) { - obj.deactivate(); - if (settings.zoomType != 'innerzoom' && !settings.alwaysOn) { - //obj.deactivate(); - } - }); - $('.zoomWrapperTitle', this.node).css({ width: '100%', position: 'absolute' }); } - //zoom title $('.zoomWrapperTitle', this.node).hide(); if (settings.title && zoomtitle.length > 0) { - $('.zoomWrapperTitle', this.node).html(zoomtitle).show(); } $obj.setposition(); - - }; - this.hide = function () { switch (settings.hideEffect) { case 'fadeout': - this.node.fadeOut(settings.fadeoutSpeed, function () { - if (el.zoom_active) $(this).show(); - }); + this.node.fadeOut(settings.fadeoutSpeed, function () {}); break; default: this.node.hide(); break; } this.ieframe.hide(); - } + }; this.show = function () { - switch (settings.showEffect) { case 'fadein': - this.node.fadeIn(settings.fadeinSpeed, function () { - if (!el.zoom_active) $(this).hide(); - }); + this.node.fadeIn(); + this.node.fadeIn(settings.fadeinSpeed, function () {}); break; default: this.node.show(); break; } - if (isIE6 && settings.zoomType != 'innerzoom') { - this.ieframe.width = this.node.width(); this.ieframe.height = this.node.height(); this.ieframe.left = this.node.leftpos; this.ieframe.top = this.node.toppos; - this.ieframe.css({ display: 'block', position: "absolute", @@ -761,11 +600,8 @@ $('.zoomPad', el).append(this.ieframe); this.ieframe.show(); }; - - - } - } - + }; + }; /*========================================================, | LargeImage |---------------------------------------------------------: @@ -775,36 +611,24 @@ function Largeimage() { var $obj = this; this.node = new Image(); - this.loadimage = function (url) { - //showing preload loader.show(); this.url = url; - - - //if(!this.node) - //this.node = new Image(); this.node.style.position = 'absolute'; this.node.style.border = '0px'; this.node.style.display = 'none'; this.node.style.left = '-5000px'; this.node.style.top = '0px'; - - document.body.appendChild(this.node); this.node.src = url; // fires off async - - } + }; this.fetchdata = function () { var image = $(this.node); - var scale = {} + var scale = {}; this.node.style.display = 'block'; - $obj.w = image.width(); $obj.h = image.height(); - - $obj.pos = image.offset(); $obj.pos.l = image.offset().left; $obj.pos.t = image.offset().top; @@ -813,77 +637,67 @@ scale.x = ($obj.w / smallimage.w); scale.y = ($obj.h / smallimage.h); el.scale = scale; - document.body.removeChild(this.node); $('.zoomWrapperImage', el).empty().append(this.node); - //setting lens dimensions; lens.setdimensions(); - - }; - this.node.onerror = function () { alert('Problems while loading the big image.'); throw 'Problems while loading the big image.'; - } - - + }; this.node.onload = function () { - //fetching data $obj.fetchdata(); - - loader.hide(); el.largeimageloading = false; el.largeimageloaded = true; if (settings.zoomType == 'drag' || settings.alwaysOn) { - lens.show(); stage.show(); lens.setcenter(); } }; - this.setposition = function () { - - var left = -el.scale.x * (lens.getoffset().left - smallimage.bleft + 1); var top = -el.scale.y * (lens.getoffset().top - smallimage.btop + 1); $(this.node).css({ 'left': left + 'px', 'top': top + 'px' }); - }; - return this; - } - - $(el).data("jqzoom",obj); - } - - - + }; + $(el).data("jqzoom", obj); + }; //es. $.jqzoom.disable('#jqzoom1'); $.jqzoom = { defaults: { - zoomType: 'standard',//innerzoom/standard/reverse/drag - zoomWidth: 300,//zoomWindow default width - zoomHeight: 300,//zoomWindow default height - xOffset: 10,//zoomWindow x offset, can be negative(more on the left) or positive(more on the right) - yOffset: 0,//zoomWindow y offset, can be negative(more on the left) or positive(more on the right) - position: "right",//zoomWindow default position - preloadImages: true,//image preload + zoomType: 'standard', + //innerzoom/standard/reverse/drag + zoomWidth: 300, + //zoomWindow default width + zoomHeight: 300, + //zoomWindow default height + xOffset: 10, + //zoomWindow x offset, can be negative(more on the left) or positive(more on the right) + yOffset: 0, + //zoomWindow y offset, can be negative(more on the left) or positive(more on the right) + position: "right", + //zoomWindow default position + preloadImages: true, + //image preload preloadText: 'Loading zoom', title: true, lens: true, imageOpacity: 0.4, alwaysOn: false, - showEffect: 'show',//show/fadein - hideEffect: 'hide',//hide/fadeout - fadeinSpeed: 'slow',//fast/slow/number + showEffect: 'show', + //show/fadein + hideEffect: 'hide', + //hide/fadeout + fadeinSpeed: 'slow', + //fast/slow/number fadeoutSpeed: '2000' //fast/slow/number }, disable: function (el) { @@ -902,8 +716,5 @@ enableAll: function (el) { jqzoompluging_disabled = false; } - - } - - + }; })(jQuery);
\ No newline at end of file |