淘宝上经常用到的一个功能是利用图片的放大镜功能来查看商品的细节

下面我们来实现这样一个功能吧,原理很简单:

实现一个可以随鼠标移动的虚框

在另外一个块中对应显示虚框中的内容

实现思路:

虚框用css中的透明度实现filter:alpha(opacity:20); opacity:0.2;

鼠标移动到小图上面时:虚框出现,大图对应出现

细节注意的地方:

1:虚框的定位:保持鼠标位于虚框的中心,如何处理鼠标中心距离四边距离小于虚框半径(或者方形的边长的一半)的情况?

2:保持大图中显示的内容是虚框选中的内容(保持大小图的比例问题)

代码如下:

<!DOCTYPE html>
<html>
<head>
<title>js_study</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<script src="js_study.js" type="text/javascript" charset="utf-8"></script>
<style>
#div1 { width:280px; height:200px; position:relative; margin:30px auto 0px;}
#div1 img{width:280px; height:200px;}
#div1 span { width:100px; height:100px; background:blue; left:0px;top:0px; position:absolute; display:none; filter:alpha(opacity:20); opacity:0.2;}
.show { width:100%; height:100%; background:blue; position:absolute; z-index:10px; filter:alpha(opacity:10); opacity:0.1; left:0px; top:0px; }
#div2 {width:400px; height:400px; position:relative; display:none; overflow:hidden; margin:0px auto 0px;}
#img1 { position:absolute;}
</style>
</head>
<body style="margin 0px; text-align: center" onload="init();" >
<div id="div1">
<img src="./num/2.jpg">
<span style="display: none; left: 204px; top: 41px;"></span>
<div class="show"></div>
</div>
<div id="div2" style="display: none;">
<img id="img1" src="./num/2.jpg" style="left: -610px; top: -149.21311475409834px;">
</div>
</body>
</html>
//放大镜效果
var moveme = false;
function init () {
var d1 = document.getElementById('div1');
var the_float = d1.getElementsByTagName('div')[0];
var the_span = d1.getElementsByTagName('span')[0];
var the_img = document.getElementById('img1');
the_float.onmouseover = function () {
the_span.style.display = 'block';
the_img.parentNode.style.display = 'block';
}
the_float.onmouseout = function () {
the_span.style.display = 'none';
the_img.parentNode.style.display = 'none';
}
the_float.onmousemove = function (ev) {
var oEvent = ev||window.event;
var x = oEvent.clientX - d1.offsetLeft - the_span.offsetWidth/2;//鼠标横坐标-父块距离浏览器窗口左距离-虚框的半
var y = oEvent.clientY - d1.offsetTop - the_span.offsetHeight/2;
//小图区域
if(x<0) x=0;//左边界
else if(x>the_float.offsetWidth - the_span.offsetWidth)//右边界
x = the_float.offsetWidth - the_span.offsetWidth;
if(y<0) y= 0;//上边界
else if(y>the_float.offsetHeight - the_span.offsetHeight)//下边界
y = the_float.offsetHeight -the_span.offsetHeight;
the_span.style.left = x+"px";
the_span.style.top = y+"px";
//大图对应区域
var percentX = x/(the_float.offsetWidth - the_span.offsetWidth);//计算比例
var percentY = y/(the_float.offsetHeight - the_span.offsetHeight);
var iParent = the_img.parentNode;
//保持大小图之间的比例关系
the_img.style.width = parseFloat(the_float.offsetWidth)/parseFloat(the_span.offsetWidth)*parseFloat(iParent.offsetWidth)+"px";
the_img.style.left = -percentX*(the_img.offsetWidth - iParent.offsetWidth)+"px";
the_img.style.top = -percentY*(the_img.offsetHeight - iParent.offsetHeight)+"px";
}
}

最新文章

  1. Java并发编程学习笔记(一)——线程安全性
  2. spring 装配核心笔记
  3. C# IEnumerable,Lambda表达式和 Parallel并行编程的用法
  4. SQL 性能调优日常积累【转】
  5. Linux 读书笔记 一
  6. Linode各机房在中国访问速度性能测试
  7. 代码审查工具 StyleCop 的探索
  8. SignalR介绍与Asp.net,前台即时通信【转】
  9. Kafka的Producer以及Consumer远程调用问题
  10. java传值和通过引用传递
  11. Unity NGUI UIPanel下对粒子的剪裁
  12. 开发抓包工具 Mac charles 3.11.5 破解版 安装包
  13. 如何卸载CentOS自带的apache
  14. 【webGL入门2】点线面的绘制
  15. (三)ajax请求不同源之cors跨域
  16. Docker集群管理工具 - Kubernetes 部署记录 (运维小结)
  17. Python写的链接数据库存取数据
  18. Expo大作战(三)--针对已经开发过react native项目开发人员有针对性的介绍了expo,expo的局限性,开发时项目选型注意点等
  19. Ubuntu18.04下的音频录制和编辑软件Ardour及QjackCtl(jackd gui)
  20. 测试word发表博客

热门文章

  1. BZOJ2395:[Balkan 2011]Timeismoney——题解
  2. BZOJ3675 &amp; 洛谷3648 &amp; UOJ104:[Apio2014]序列分割——题解
  3. BZOJ2226 &amp; SPOJ5971:LCMSum——题解
  4. BZOJ2565:最长双回文串——题解
  5. snmp理论篇
  6. bzoj 4414 数量积 结论题
  7. redis 选择数据库
  8. uboot各文件及文件夹分析
  9. Test Index
  10. maven插件理解