放大镜
2016-06-30 08:31
246 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>放大镜test</title> </head> <body> <style> *{margin:0;padding: 0;} #demo{width:350px;height: 350px;position: relative;left:100px;top:100px;} #smallpic{width:350px;height: 350px;cursor:move;} #mask{width:150px;height: 150px;background: #fff;opacity: 0.5;filter:alpha(opacity=50); position: absolute;left:0;top:0;display: none;} #bigmask{width:350px;height: 350px;background: #000;position: absolute;left:0;top:0; opacity: 0;filter:alpha(opacity=0);} #bigpic{position: relative;top:-350px;left:350px;width:300px;height: 300px;overflow: hidden;display: none;} #bigpic img{position: absolute;} </style> <script type="text/javascript"> /* clientX 鼠标指针绝对坐标; layerX 鼠标相对与事件对象的相对坐标; cursor:move;移动鼠标; offsetLeft offsetTop 只读的; style.left style.right 要读取必须设定对应元素的style=“left:0;top:0”, style css文件中设定不行; */ window.onload=function(){ var _div=document.getElementById('bigmask'); var _mask=document.getElementById('mask'); var _bigpic=document.getElementById('bigpic'); var _bigimg=_bigpic.getElementsByTagName('img')[0]; _div.onmousemove=function(e){ var _mousex=e.layerX; var _mousey=e.layerY; _mask.style.left=(_mousex-_mask.offsetWidth/2)+'px'; _mask.style.top=(_mousey-_mask.offsetHeight/2)+'px'; if(_mask.offsetLeft<=0){ _mask.style.left=0; } if(_mask.offsetTop<=0){ _mask.style.top=0 } var _maxleft=this.offsetWidth-_mask.offsetWidth; if(_mask.offsetLeft>_maxleft){ _mask.style.left=_maxleft+'px'; } var _maxtop=this.offsetHeight-_mask.offsetHeight; if(_mask.offsetTop>_maxtop){ _mask.style.top=_maxtop+'px';//top t小写; } _bigimg.style.left=-(parseInt(_mask.style.left)*2)+'px'; _bigimg.style.top=-(parseInt(_mask.style.top)*2)+'px'; } _div.onmouseover=function(){ _mask.style.display='block'; _bigpic.style.display='block'; } _div.onmouseout=function(){ _mask.style.display='none'; _bigpic.style.display='none'; } } </script> <div id="demo" style="background:#000;"> <div id="smallpic"> <img src="images/p2.jpg" width="350" height="350" alt=""> <div id="mask"></div> <div id="bigmask"></div> </div> <div id="bigpic"> <img src="images/p2.jpg" width="700" height="700" alt=""> </div> </div> </body> </html>
相关文章推荐
- Anroid搭建一个局域网Web服务器
- Qt Quick里的图形效果:阴影(Drop Shadow)
- 没有自动联想补齐代码的解决办法
- 指尖下的js ——多触式web前端开发之一:对于Touch的处理
- 基本分段存储管理方式
- 安卓简单开发-安卓自带几种动画效果
- 14.1 threading--多线程
- 抽象方法不能是static或native或synchroniz
- 【伸展树splay学习小记】
- 注解开发--基础
- iOS 开发中的争议(二)
- 【bzoj1588】【HNOI2002】【营业额统计】【splay】
- mysql+hibernate 一些报错
- 第三方的使用
- iOS 开发中的争议(一)
- Hourai Jeweled
- JavaScript中的匿名函数及函数的闭包
- iOS APP可执行文件的组成
- 一篇搞定RSA加密与SHA签名|与Java完全同步
- linux——深入理解linux内存分布—基础篇