自己改写了一个图片局部放大的jquery插件页面里面的html代码少了,同一个页面可以调用多个,兼容ie8以上浏览器,别忘了引用jquery,我用的jquery/1.11.0/其他版本没有测试,另外需要注意的时候设置要局部放大的图片以及外部容器的宽度必须一致,否则效果不正确

 (function($){
var wlfimgzooms=[];
$.fn.wlfimgzoom=function(options){
if ($(this).length > 1) {
var instances = [];
$(this).each(function (i) {
instances[i] = new wlfimgzoom($(this), options);
wlfimgzooms.push(instances[i]);
});
return instances;
} else {
var instance = new wlfimgzoom($(this), options);
wlfimgzooms.push(instance);
return instance;
}
} var wlfimgzoom = function (select, options) {
this.op = {width:"300px",height:"300px",zooms:"90"};
$.extend(this.op, options);
this.obj = select;
this.zoom_m = this.obj.width();
this.zoom_w = this.obj.children("img").width();
this.zooms=this.op.zooms/2;
this.fd=$("<div class='fd'><div class='fd1'><img src='' /></div></div>");
this.xt=$("<div class='xt'><div class='xt1'><img src='' /></div></div>")
this.init();
};
wlfimgzoom.prototype = {
init: function () {
this.build();
this.bindEvents();
},
build: function () {
this.obj.css("position","relative");
var src=this.obj.children("img").attr("src");
this.fd.appendTo(this.obj).children().children("img").attr("src",src);
this.fd.css({"position":"absolute","top":"0","left":this.obj.width()+20,"width":this.op.width,"height":this.op.height,"overflow":"hidden","display":"none",});
this.xt.appendTo(this.obj).children().children("img").attr({"src":src,"width":this.obj.children("img").width(),"height":this.obj.children("img").height()});
this.xt.css({width:this.op.zooms+"px",height:this.op.zooms+"px","position":"absolute","overflow":"hidden","display":"none","border":"1px #444 solid","left":"0","top":"0"});
},
bindEvents: function () {
var self=this;
this.obj.hover(function(){self.xt.show();self.fd.show();
self.zoom_b=self.fd.children().children("img").width();},function(){self.xt.hide();self.fd.hide()})
this.obj.mousemove(function(e) {
var x = e.offsetX;//||e.originalEvent.x || e.originalEvent.layerX || 0;
var y = e.offsetY;//||e.originalEvent.y || e.originalEvent.layerY || 0;
movebigpic(x,y);
movediv(x,y);
});
function movebigpic(x, y)
{
var xx = 0;
var yy = 0;
if (x < self.zooms)
{
xx = 0;
}
else if (x > (self.zoom_m - self.zooms))
{
xx = self.zoom_w - self.zoom_b;
}
else
{
xx = (self.zooms - x) * (self.zoom_b - self.zoom_w) / (self.zoom_m - self.op.zooms);
} if (y < self.zooms)
{
yy = 0;
}
else if (y > (self.zoom_m - self.zooms))
{
yy = self.zoom_w - self.zoom_b;
}
else
{
console.clear();
console.log("self.zoom_w",self.zoom_w)
console.log("self.zoom_b",self.zoom_b)
yy = (self.zooms - y) * (self.zoom_b - self.zoom_w) / (self.zoom_m - self.op.zooms);
}
self.fd.children().css({"left":xx + "px","top":yy + "px","position":"absolute"}) } function movediv(x, y)
{
var xx = 0;
var yy = 0;
if (x < self.zooms)
{
xx = 0;
}
else if (x > (self.zoom_m - self.zooms))
{
xx = self.zoom_m - self.op.zooms;
}
else
{
xx = x - self.zooms;
}
if (y < self.zooms)
{
yy = 0;
}
else if (y > (self.zoom_m - self.zooms))
{
yy = self.zoom_m - self.op.zooms;
}
else
{
yy = y - self.zooms;
}
self.xt.css({"left":xx + "px","top":yy + "px","position":"absolute"})
movesmallpic(xx, yy);
} function movesmallpic(x, y)
{
var xx = 0 - x;
var yy = 0 - y; self.xt.children().css({"left":xx + "px","top":yy + "px","position":"absolute"}) }
}
}
})(jQuery)

上面是核心的插件代码

调用方法是

其中参数width,height表示局部放大的容器宽高,zooms表示鼠标放到图片上时小方块的宽高

$(function(){
$(".imgzoom").wlfimgzoom({width:"300px",height:"300px",zooms:"90"}); })

html代码

     <div id="one" class="imgzoom">
<img class="small" src="data:images/1_b.jpg" />
</div>
<div id="two" class="imgzoom">
<img class="small" src="http://img.sootuu.com/vector/200801/097/655.jpg" />
</div>

主要样式表

 .imgzoom{width:150px;}
.imgzoom img.small{width:150px;height:150px;}

自己改写了一个图片局部放大的jquery插件页面里面的html代码少了,同一个页面可以调用多个的更多相关文章

  1. javascript - 简单实现一个图片延迟加载的jQuery插件

    最近在看一本书<Third-Party Javascript>很不错,推荐给大家,下载地址各位自己搜索了. 步骤: 1.打开google,鉴于google基本打不开,那么就打开这个网址吧. ...

  2. javascript设计模式实践之模板方法--具有百叶窗切换图片效果的JQuery插件(二)

    在上一篇<javascript设计模式实践之迭代器--具有百叶窗切换图片效果的JQuery插件(一)>里,通过采用迭代器模式完成了各初始化函数的定义和调用. 接下来就要完成各个切换效果的编 ...

  3. javascript设计模式实践之迭代器--具有百叶窗切换图片效果的JQuery插件(一)

    类似于幻灯片的切换效果,有时需要在网页中完成一些图片的自动切换效果,比如广告,宣传,产品介绍之类的,那么单纯的切就没意思了,需要在切换的时候通过一些效果使得切换生动些. 比较常用之一的就是窗帘切换了. ...

  4. 图片上传jQuery插件(兼容IE8)

      图片上传jQuery插件(兼容IE8) 代码来源 :https://github.com/zilan93/uploadImg   html <!DOCTYPE html> <ht ...

  5. javascript设计模式实践之职责链--具有百叶窗切换图片效果的JQuery插件(三)

    在上一篇<javascript设计模式实践之模板方法--具有百叶窗切换图片效果的JQuery插件(二)>里,通过采用模板方法模式完成了切换效果对象的构建编写. 接下来就是完成各效果对象的调 ...

  6. jQuery file upload里面的_create的调用和_initEventHandlers的调用

    首先是jquery.ui.widget.js中_createWidget方法内部调用 this._create(); this._trigger( "create", null, ...

  7. SlipHover,能感知鼠标方向的图片遮罩效果jQuery插件

    接上一篇博文,介绍完jQuery插件开发后这里上一个自己的作品,也是初次实践,小有成就的感觉. 话说这个插件年前就写好了,然后挂到GitHub,然后就偷偷看着Google Analysis心中暗自激动 ...

  8. 一个很简单的jQuery插件实例教程(菜鸟级)

    很多公司的前端设计开发人员都是女孩子,而这些女孩子很多JavaScript技能都不是很好.而前端开发过程中,JavaScript技能又是必不可少的.所以,如果前端小MM正在为某个JavaScript效 ...

  9. Vue实现一个图片懒加载插件(转载)

    Vue是可以自定义指令的,最近学习过程中遇见了一个需要图片懒加载的功能,最后参考了别人的代码和思路自己重新写了一遍.以下将详细介绍如何实现自定义指令v-lazyload. 先看如何使用这个指令: &l ...

随机推荐

  1. 无序线性搜索(Unordered Linear Search)

    假定有一个元素顺序情况不明的数组.这种情况如果我们要搜索一个元素就要遍历整个数组,才能知道这个元素是否在数组中. 这种方法要检查整个数组,核对每个元素.下面是算法实现: #include<std ...

  2. poj 1088 动态规划+dfs(记忆化搜索)

    滑雪 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Description Mi ...

  3. Python 字符串相关操作

    # 1 * 重复输出字符串 print('hello'*2) # 2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表 print('helloworld'[2: ...

  4. Unity 制作RPG地图2(自己控制地图上图标)

    上一次用Unity摄像机方式实现了地图的制作,现在介绍另一种实现地图的方式: 自己通过代码实现小地图NCP图标的显示和隐藏 制作地图的步骤: 1. 根据游戏人物的3D坐标转换成2D平面坐标,根据距离显 ...

  5. 基于纹理边缘抑制的轮廓和边界检测(Contour and Boundary Detection)

    基于纹理边缘抑制的轮廓和边界检测(Contour and Boundary Detection) kezunhai@gmail.com http://blog.csdn.net/kezunhai 一幅 ...

  6. linux配置yum源

    yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...

  7. 【中途相遇+二进制】【NEERC 2003】Jurassic Remains

    例题25  侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...

  8. linux进程间通信之共享内存篇

    本文是对http://www.cnblogs.com/andtt/articles/2136279.html中共享内存(上)的进一步阐释说说明 1 共享内存的实现原理 共享内存是linux进程间通讯的 ...

  9. Eclipse 常用快捷键 (动画讲解)(转载)

    http://www.cnblogs.com/TankXiao/p/4018219.html#fix 很详细呀/

  10. SQL日期格式转换(经常用又经常忘记的东西)转载自http://www.cnblogs.com/wangyuelang0526/archive/2012/06/06/2538224.html

    Select CONVERT(varchar(100), GETDATE(), 8):14:53:14Select CONVERT(varchar(100), GETDATE(), 9): 06 6 ...