自己改写了一个图片局部放大的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. PHP 表单验证 - 必填字段

    -------------------------------------------------------------------------- 本节展示如何制作必填输入字段,并创建需要时所用的错 ...

  2. Unity扩展让枚举视图中变成多选框

    如图: 定义属性描述特性(因为没有描述的数据,让绘制类去绘制所以为空) using UnityEngine; using System.Collections; public class EnumFl ...

  3. H5页面之iphone6的适配

    兼容iphone各版本机型最佳的方式就是自适应. 1.viewport 简单粗暴的方式: 1 <meta name="viewport" content="widt ...

  4. 网页 php开发中html空文本节点问题user agent stylesheetbody

    最近开发中遇到一个奇怪的问题,我的一个网站头部,代码固定不变,放在了不同的模板进行展示,一部分出现了问题,总是距离相差8个像素,用firebug查看发现:meta 跑到 body 下面去了,并且发现了 ...

  5. css系列教程--简介及基础语法和注意事项

    css简介:css指的是层叠样式表,cascading style sheets.用来定义html中的dom节点如何展示在页面中的问题.解决了内容与表现形式的分离问题.常见的样式表有外部链接样式表和内 ...

  6. U - stl 的 优先队列 Ⅰ

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  7. error LNK2019: 无法解析的外部符号 "public:

    错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall test::test(void)" (??0test@@QAE@XZ),该符号在 ...

  8. .Net框架中,.ashx页面访问session

    public class ClassName : System.Web.SessionState.IRequiresSessionState 需实现session接口

  9. YII学习,初体验 ,对YII的一些理解.

    先说点没用的: 不会选择,选择后不坚持,不断的选择.这是人生中的一个死循环,前两一直迷茫.觉得自己前进方向很不明朗.想去学的东西有很多.想学好YII,想学PYTHON 想学学hadoop什么的,又想研 ...

  10. 匈牙利算法(素数伴侣(HW1112))

    #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> #include<string&g ...