效果图:

HTML结构如下:

<div id="preview">
<div id="mediumDiv">
<img id="mImg" src="data:images/products/product-s1-m.jpg"/>
<div id="mask"></div>
<div id="superMask"></div>
</div>
<div id="largeDiv"></div>
<h1>
<a class="backward_disabled"></a>
<a class="forward"></a>
<ul id="icon_list">
<li><img src="data:images\products\product-s1.jpg" /></li>
<li><img src="data:images\products\product-s2.jpg" /></li>
<li><img src="data:images\products\product-s3.jpg" /></li>
<li><img src="data:images\products\product-s4.jpg" /></li>
<li><img src="data:images\products\product-s1.jpg" /></li>
<li><img src="data:images\products\product-s2.jpg" /></li>
<li><img src="data:images\products\product-s3.jpg" /></li>
<li><img src="data:images\products\product-s4.jpg" /></li>
</ul>
</h1>
</div>

JS部分:

var pPhoto = {
LIWIDTH : 62, //每个小图片的li的固定宽度
moved : 0, //记录左移小图片的个数
count : 0, //记录小图片的总数
ul: null, //小图父元素ul
btnL: null, //左边按钮 控制右移
btnR : null,
superMark :null,
SUPERWIDTH : 350, //supermask的宽高
SUPERHEIGHT :350,
MASKWIDTH:175, //遮罩层mask的宽高
MASKHEIGHT:175,
inti : function (){ //初始化方法
this.ul = $("#icon_list")[0];
this.ul.onmouseover =this.changeMImg;
this.btnL = this.ul.parentNode.$("a")[0];
this.btnR = this.ul.parentNode.$("a")[1];
this.btnR.onclick=this.btnL.onclick=function () {
pPhoto.move(this); //将btn通过此时的this作为参数传入 move方法
//将move的this指向pPhoto对象
};
this.count = this.ul.$("li").length;
//图片放大效果遮罩层
this.supermask = $("#superMask")[0];
this.supermask.onmouseover=this.supermask.onmouseout=this.showMask;
this.supermask.onmousemove = function(){
var e = window.event || arguments[0];
pPhoto.zoom(e);
}
},
move : function (btn){//移动方法
if(!btn.className.endsWith("_disabled")) {
if (btn == this.btnR) { //判断当前btn的左右
this.ul.style.left = -(this.LIWIDTH * (++this.moved) - 20) + "px";
//-20 根据css原left属性进行修正
} else {
this.ul.style.left = -(this.LIWIDTH * (--this.moved) - 20) + "px";
}
this.btnEnable();
}
},
btnEnable : function (){//修改按钮状态方法 this-->pPhoto
if(this.moved==0){
this.btnL.className+="_disabled";
}else if(this.count-this.moved==5){
this.btnR.className+="_disabled";
}else{
this.btnL.className=this.btnL.className.replace("_disabled","");
//replace()方法并不能直接修改calssname的属性值 需要重新赋值
this.btnR.className=this.btnR.className.replace("_disabled","");
}
},
changeMImg :function (){ //this-->ul
//获取事件对象
var e = window.event || arguments[0];
//获取目标对象
var src=e.srcElemnt||e.target;
if(src.nodeName=="IMG"){
var path = src.src;
var i = path.lastIndexOf(".");
$("#mImg")[0].src=path.slice(0,i)+"-m"+path.slice(i);
}
},
showMask : function(){
var mask = $("#mask")[0];
var style = getComputedStyle(mask);
var largeDiv = $("#largeDiv")[0];
largeDiv.style.display=mask.style.display=style.display=="none"?"block":"none";
if(largeDiv.style.display=="block"){
var path = $("#mImg")[0].src;
var i=path.lastIndexOf(".");
$("#largeDiv")[0].style.backgroundImage="url('"+path.slice(0,i-1)+"l"+path.slice(i)+"')";
}
},
zoom :function (e){
var x=e.offsetX
var y=e.offsetY;
var mLeft = x-this.MASKWIDTH/2;
var mTop = y-this.MASKHEIGHT/2;
mLeft<0 && (mLeft=0);
mLeft>this.SUPERWIDTH-this.MASKWIDTH && (mLeft=this.SUPERWIDTH-this.MASKWIDTH);
mTop<0 && (mTop=0);
mTop>this.SUPERHEIGHT-this.MASKHEIGHT &&(mTop=this.SUPERHEIGHT-this.MASKHEIGHT);
var mask = $("#mask")[0];
mask.style.left=mLeft+"px";
mask.style.top=mTop+"px";
var largeDiv = $("#largeDiv")[0]
largeDiv.style.backgroundPosition=-mTop*2+"px "+-mLeft*2+"px";
}
}

Tips

  *面向对象方法在编写页面中某一块交互功能时,能有效的避免因代码过多而造成的变量污染,更有助于代码的可读性和重用性。

  *this 很好很强大 在面向对象方法中如果需要指定this的指向可以使用以下方法:

this.btnR.onclick=this.btnL.onclick=function () {
pPhoto.move(this); //将btn通过此时的this作为参数传入 move方法
//将move的this指向pPhoto对象
};

  *事件对象e 只能在事件绑定函数中获得 :

 this.supermask.onmousemove = function(){
var e = window.event || arguments[0];
//获取事件对象e 作为参数传递给 放大方法zoom;
pPhoto.zoom(e);
}
zoom :function (e){ //zoom函数类无法获得 sueromask元素的事件对象e
var x=e.offsetX
var y=e.offsetY;
var mLeft = x-this.MASKWIDTH/2;
var mTop = y-this.MASKHEIGHT/2;
mLeft<0 && (mLeft=0);
mLeft>this.SUPERWIDTH-this.MASKWIDTH && (mLeft=this.SUPERWIDTH-this.MASKWIDTH);
mTop<0 && (mTop=0);
mTop>this.SUPERHEIGHT-this.MASKHEIGHT &&(mTop=this.SUPERHEIGHT-this.MASKHEIGHT);
var mask = $("#mask")[0];
mask.style.left=mLeft+"px";
mask.style.top=mTop+"px";
var largeDiv = $("#largeDiv")[0]
largeDiv.style.backgroundPosition=-mTop*2+"px "+-mLeft*2+"px";
}

最新文章

  1. &amp;#65279; 非法字符 原因 以及解决办法
  2. redis 的源码编译安装
  3. Java中用ClassLoader载入各种资源(类、文件、web资源)的方法
  4. MFC的几处坑
  5. 7个你可能不认识的CSS单位:rem vh vw vmin vmax ex ch
  6. iOS设计模式 - 单例
  7. [AngularJS] ui-router: named views
  8. [记录] js判断数组key是否存在
  9. ReactiveCocoa框架学习1
  10. 本地windows下PHP连接远程oracle遇到的诸多问题
  11. C#版二维码生成器附皮肤下载
  12. 动画——animation部分
  13. 启动ipython notebook(jupyter)
  14. Java线程:堵塞队列与堵塞栈
  15. 基于JAX-WS的WebService实现
  16. 配置shiro错误
  17. 【Teradata Utility】使用SQL Assistant导出导入数据
  18. mysqlQL 5.7 安装报错CMake Error at cmake/boost.cmake:81 (MESSAGE)
  19. ajax跨域请求 Uncaught SyntaxError: Unexpected token :
  20. Redis笔记(3)多数据库实现

热门文章

  1. linux常用命令与技巧(不断添加与更新)
  2. ActivityManagerService服务线程启动源码分析【转】
  3. Codeforces Beta Round #96 (Div. 1) C. Logo Turtle —— DP
  4. Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&amp;arch=x86_64&amp;repo=os&amp;infra=stoc
  5. 动态负载均衡(Nginx+Consul+UpSync)环境搭建
  6. BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告
  7. YII好的博客
  8. kamailio/opensips snmp/cacti/zabbix监控
  9. POJ1904(有向图缩点+输入输出挂参考)
  10. Spark Streaming之三:DStream解析