下图只是给大家举个例子,类似于这种效果图:
具体实现过程请看下文代码:
css
.zoompad{position:relative;float:left;z-index:99;cursor:crosshair;}.zoompreload{-moz-opacity:0.8;opacity: 0.8;filter: alpha(opacity = 80);color: #333;font-size: 12px;font-family: tahoma;text-decoration: none;border: 1px solid #ccc;padding: 8px;text-align:center;background-image: url(../images/zoomloader.gif);background-repeat: no-repeat;background-position: 43px 30px;z-index:110;width:90px;height:43px;position:absolute;top:0px;left:0px;* width:100px;* height:49px;}.zoompup{overflow:hidden;-moz-opacity:0.6;opacity: 0.6;filter: alpha(opacity = 60);z-index:120;position:absolute;border:1px solid #ccc;z-index:101;cursor:crosshair;}.zoomoverlay{position:absolute;left:0px;top:0px;/*opacity:0.5;*/z-index:5000;width:100%;height:100%;display:none;z-index:101;}.zoomwindow{position:absolute;left:110%;top:40px;z-index:6000;height:auto;z-index:10000;z-index:110;}.zoomwrapper{position:relative;border:1px solid #999;z-index:110;}.zoomwrappertitle{display:block;background:#999;color:#fff;height:18px;line-height:18px;width:100%;overflow:hidden;text-align:center;font-size:10px;position:absolute;top:0px;left:0px;z-index:120;-moz-opacity:0.6;opacity: 0.6;filter: alpha(opacity = 60);}.zoomwrapperimage{display:block;position:relative;overflow:hidden;z-index:110;}.zoomwrapperimage img{border:0px;display:block;position:absolute;z-index:101;}.zoomiframe{z-index: -1;filter:alpha(opacity=0);-moz-opacity: 0.80;opacity: 0.80;position:absolute;display:block;}/*********************************************************/ when clicking on thumbs jqzoom will add the class/ zoomthumbactive on the anchor selected/*********************************************************/
js
/*!* jqzoom evolution library v2.3 - 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* notice, this list of conditions and the following disclaimer.* * redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* 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.** date: 03 may 2011 22:16:00*/(function ($) {//global variablesvar isie6 = ($.browser.msie && $.browser.version 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 settingsvar zoomtypes = ['standard', 'drag', 'innerzoom', 'reverse'];if ($.inarray($.trim(settings.zoomtype), zoomtypes) smallimage.pos.r || e.pagex 0) {//getting the first to the lastvar first = thumblist.splice(0, 1);thumblist.push(first);}thumblist.each(function () {//preloading thumbsif (settings.preloadimages) {var thumb_options = $.extend({}, eval(( + $.trim($(this).attr('rel')) + )));thumb_preload[i] = new image();thumb_preload[i].src = thumb_options.largeimage;i++;}$(this).click(function (e) {if($(this).hasclass('zoomthumbactive')){return false;}thumblist.each(function () {$(this).removeclass('zoomthumbactive');});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 zoomwindowlens.show();stage.show();},deactivate: function (e) {switch (settings.zoomtype) {case 'drag'://nothing or lens.setcenter();break;default:img.attr('title', el.imagetitle);$(el).attr('title', el.title);if (settings.alwayson) {lens.setcenter();} else {stage.hide();lens.hide();}break;}el.zoom_active = false;},swapimage: function (link) {el.largeimageloading = false;el.largeimageloaded = false;var options = new object();options = $.extend({}, eval(( + $.trim($(link).attr('rel')) + )));if (options.smallimage && options.largeimage) {var smallimage = options.smallimage;var largeimage = options.largeimage;$(link).addclass('zoomthumbactive');$(el).attr('href', largeimage);img.attr('src', smallimage);lens.hide();stage.hide();obj.load();} 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 fireif (img[0].complete) {//fetching data from sallimage if was previously loadedsmallimage.fetchdata();if ($(.zoompad, el).length == 0) obj.create();}/*========================================================,| smallimage|---------------------------------------------------------:| 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');btop = '';var borderleft = 0;borderleft = image.css('border-left-width');bleft = '';if (bordertop) {for (i = 0; i 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.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.rightlimit = image.offset().left + $obj.ow;$obj.bottomlimit = image.offset().top + $obj.oh;};this.node.onerror = function () {alert('problems while loading image.');throw 'problems while loading image.';};this.node.onload = function () {$obj.fetchdata();if ($(.zoompad, el).length == 0) obj.create();};return $obj;};/*========================================================,| loader|---------------------------------------------------------:| show that the large image is loading`========================================================*/function loader() {var $obj = this;this.append = function () {this.node = $('').addclass('zoompreload').css('visibility', 'hidden').html(settings.preloadtext);$('.zoompad', el).append(this.node);};this.show = function () {this.node.top = (smallimage.oh - this.node.height()) / 2;this.node.left = (smallimage.ow - this.node.width()) / 2;//setting positionthis.node.css({top: this.node.top,left: this.node.left,position: 'absolute',visibility: 'visible'});};this.hide = function () {this.node.css('visibility', 'hidden');};return this;}/*========================================================,| lens|---------------------------------------------------------:| lens over the image`========================================================*/function lens() {var $obj = this;this.node = $('').addclass('zoompup');//this.nodeimgwrapper = $().addclass('zoompupimgwrapper');this.append = function () {$('.zoompad', el).append($(this.node).hide());if (settings.zoomtype == 'reverse') {this.image = new image();this.image.src = smallimage.node.src; // fires off async$(this.node).empty().append(this.image);}};this.setdimensions = function () {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 lensthis.node.css({top: 0,left: 0,width: this.node.w + 'px',height: this.node.h + 'px',position: 'absolute',display: 'none',borderwidth: 1 + 'px'});if (settings.zoomtype == 'reverse') {this.image.src = smallimage.node.src;$(this.node).css({'opacity': 1});$(this.image).css({position: 'absolute',display: 'block',left: -(this.node.left + 1 - smallimage.bleft) + 'px',top: -(this.node.top + 1 - smallimage.btop) + 'px'});}};this.setcenter = function () {//calculating center positionthis.node.top = (smallimage.oh - this.node.h - 2) / 2;this.node.left = (smallimage.ow - this.node.w - 2) / 2;//centering lensthis.node.css({top: this.node.top,left: this.node.left});if (settings.zoomtype == 'reverse') {$(this.image).css({position: 'absolute',display: 'block',left: -(this.node.left + 1 - smallimage.bleft) + 'px',top: -(this.node.top + 1 - smallimage.btop) + 'px'});}//centering large imagelargeimage.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.pos.r; }function overtop(lens) {return el.mousepos.y - (lens.h) / 2 smallimage.pos.b; }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.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)) {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') {if ($.browser.msie && $.browser.version > 7) {$(this.node).empty().append(this.image);}$(this.image).css({position: 'absolute',display: 'block',left: -(this.node.left + 1 - smallimage.bleft) + 'px',top: -(this.node.top + 1 - smallimage.btop) + 'px'});}largeimage.setposition();};this.hide = function () {img.css({'opacity': 1});this.node.hide();};this.show = function () { if (settings.zoomtype != 'innerzoom' && (settings.lens || settings.zoomtype == 'drag')) {this.node.show();} if (settings.zoomtype == 'reverse') {img.css({'opacity': settings.imageopacity});}};this.getoffset = function () {var o = {};o.left = $obj.node.left;o.top = $obj.node.top;return o;};return this;};/*========================================================,| stage|---------------------------------------------------------:| window area that contains the large image`========================================================*/function stage() {var $obj = this;this.node = $(
);this.ieframe = $('');this.setposition = function () {this.node.leftpos = 0;this.node.toppos = 0;if (settings.zoomtype != 'innerzoom') {//positioningswitch (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.ow + math.abs(settings.xoffset));this.node.toppos = math.abs(settings.yoffset);break;case top: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.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.rightlimit + math.abs(settings.xoffset) + settings.zoomwidth 0) {$('.zoomwrappertitle', this.node).html(zoomtitle).show();}$obj.setposition();};this.hide = function () {switch (settings.hideeffect) {case 'fadeout':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();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,left: this.ieframe.left,top: this.ieframe.top,zindex: 99,width: this.ieframe.width + 'px',height: this.ieframe.height + 'px'});$('.zoompad', el).append(this.ieframe);this.ieframe.show();};};};/*========================================================,| largeimage|---------------------------------------------------------:| the large detailed image`========================================================*/function largeimage() {var $obj = this;this.node = new image();this.loadimage = function (url) {//showing preloadloader.show();this.url = url;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 = {};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;$obj.pos.r = $obj.w + $obj.pos.l;$obj.pos.b = $obj.h + $obj.pos.t;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);};//es. $.jqzoom.disable('#jqzoom1');$.jqzoom = {defaults: {zoomtype: 'standard',//innerzoom/standard/reverse/dragzoomwidth: 300,//zoomwindow default widthzoomheight: 300,//zoomwindow default heightxoffset: 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 positionpreloadimages: true,//image preloadpreloadtext: 'loading zoom',title: true,lens: true,imageopacity: 0.4,alwayson: false,showeffect: 'show',//show/fadeinhideeffect: 'hide',//hide/fadeoutfadeinspeed: 'slow',//fast/slow/numberfadeoutspeed: '2000' //fast/slow/number},disable: function (el) {var api = $(el).data('jqzoom');api.disable();return false;},enable: function (el) {var api = $(el).data('jqzoom');api.enable();return false;},disableall: function (el) {jqzoompluging_disabled = true;},enableall: function (el) {jqzoompluging_disabled = false;}};})(jquery);
还需要有jquery.js
使用方法:
1.当页面导入的时候,载入 jqzoom 插件。
$(function(){$(.jqzoom).jqzoom({zoomwidth: 300, zoomheight: 300,lens:true,preloadimages: false,alwayson:false,title:false,xoffset:20,position: right});})
2.创建一个放图片的容器,指定一个a标记用于显示放大后的图片的一部分:
下面给一些基本的配置参数:
•zoomtype,默认值:'standard',另一个值是'reverse',是否将原图用半透明图层遮盖。
•zoomwidth,默认值:200,放大窗口的宽度。
•zoomheight,默认值:200,放大窗口的高度。
•xoffset,默认值:10,放大窗口相对于原图的x轴偏移值,可以为负。
•yoffset,默认值:0,放大窗口相对于原图的y轴偏移值,可以为负。
•position,默认值:'right',放大窗口的位置,值还可以是:'right' ,'left' ,'top' ,'bottom'。
•lens,默认值:true,若为false,则不在原图上显示镜头。
•imageopacity,默认值:0.2,当zoomtype的值为'reverse'时,这个参数用于指定遮罩的透明度。
•title,默认值:true,在放大窗口中显示标题,值可以为a标记的title值,若无,则为原图的title值。
•showeffect,默认值:'show',显示放大窗口时的效果,值可以为: ‘show' ,'fadein'。
•hideeffect,默认值:'hide',隐藏放大窗口时的效果: ‘hide' ,'fadeout'。
•fadeinspeed,默认值:'fast',放大窗口的渐显速度(选项: ‘fast','slow','medium')。
•fadeoutspeed,默认值:'slow',放大窗口的渐隐速度(选项: ‘fast','slow','medium')。
•showpreload,默认值:true,是否显示加载提示loading zoom(选项: ‘true','false')。
•preloadtext,默认值:'loading zoom',自定义加载提示文本。
•preloadposition,默认值:'center',加载提示的位置,值也可以为'bycss',以通过css指定位置。
样式的个性化当然也可以直接修改jqzoom.css文件。