放大镜的实现过程是将一个小图放置在一个盒子里。宽高都是100%。当鼠标在小图盒子里移动时,出现一个移动块(阴影区域)。右侧大图片盒子出现一个等比例放大的在小图盒子移动块中的图片内容。如图(请勿过于认真看图片,注意圈圈(¬_¬)):javascript
必定要理解上图中圈起来的阴影块是箭头指向的粉红色圈的等比缩小版。理解了这个在接下来的代码中,咱们才知道怎么去计算右侧大图区域中的left、top值。也能够说成阴影移动块是模拟右侧大图盒子。右侧大图盒子中放置的是一张大的图片,而后盒子设置成溢出隐藏。而咱们的移动块也是,不在阴影块中的内容,你均可以认为是溢出隐藏掉了。css
当小图盒子中的移动块移动时,根据移动的距离去计算右侧大图盒子中图片移动的坐标。此时方便理解,你能够想像成阴影块是静止的,是阴影块下面的图片在移动。因此,咱们须要计算出图片向x轴、y轴移动了多少,根据等比例公式换算出右侧大图盒子中的图片须要移动的坐标值。html
htmljava
<!DOCTYPE html> <html> <head> <title>放大镜</title> <meta charset="utf-8"> </head> <body> <div id="small"> <img src="./fangdajing.png"> <p id="move"></p> </div> <div id="big"> <img src="./fangdajing.png" id="look_girl"> </div> </body> </html>
cssthis
<style type="text/css"> *{ margin: 0px; padding: 0px; } body{ width: 960px; margin:40px auto; } #small{ width: 300px; height: 200px; border:1px solid #eee; border-radius: 4px; position: relative; } #small img{ width: 100%; height: 100%; } div { float: left; margin-right: 10px; } #big{ width: 300px; height: 200px; overflow: hidden; position: relative; border:1px solid #eee; border-radius: 4px; display: none; } #look_girl{ position: absolute; left: 0px; top:0px; } #move{ width: 100px; height: 100px; background:#000; opacity: .5; position: absolute; display: none; left: 0px; top: 0px; } </style>
jsspa
<script type="text/javascript"> var move = document.getElementById('move'); var small = document.getElementById('small'); var big = document.getElementById('big'); var look_girl = document.getElementById('look_girl'); small.onmousemove = function(event){ event = event || window.event; this.style.cursor = 'move'; // 计算move移动块的left值 var move_left = event.clientX - this.offsetLeft - move.offsetWidth/2; // 计算move移动块的top值 var move_top = event.clientY - this.offsetTop - move.offsetHeight/2; // 超出左边界赋值为0 move_left = move_left < 0 ? 0 : move_left; // 超出右边界赋值为盒子宽度-移动块的宽度 move_left = move_left > this.offsetWidth - move.offsetWidth ? this.offsetWidth - move.offsetWidth : move_left; // 超出上边界赋值为0 move_top = move_top < 0 ? 0 : move_top; // 超出下边界赋值为盒子高度-移动块高度 move_top = move_top > this.offsetHeight - move.offsetHeight ? this.offsetHeight - move.offsetHeight : move_top; // 修改移动块left、top值 move.style.left = move_left + 'px'; move.style.top = move_top + 'px'; /* 计算图片须要移动的坐标 距离左边left 图片宽度 盒子宽度 距离左边left 图片宽度 盒子宽度 big_x/(look_girl.offsetWidth-big.offsetWidth) = move_left/(small.offsetWidth-move.offsetWidth); big_y/(look_girl.offsetHeight-big.offsetHeight) = move_top/(small.offsetHeight-move.offsetHeight); */ var big_x = move_left/(small.offsetWidth-move.offsetWidth) * (look_girl.offsetWidth-big.offsetWidth); var big_y = move_top/(small.offsetHeight-move.offsetHeight) * (look_girl.offsetHeight-big.offsetHeight); // 修改图片定位 look_girl.style.left = -big_x + 'px'; look_girl.style.top = -big_y + 'px'; } small.onmouseover = function(){ move.style.display = 'block'; big.style.display = 'block'; } small.onmouseout = function(){ move.style.display = 'none'; big.style.display = 'none'; } </script>
当鼠标移动到小图盒子中时,会出现移动块(下图阴影部分)咱们须要作的是移动块位于鼠标的中间,且跟随鼠标移动。固然了也不能溢出边界。看图说话(¬_¬)code
再看看代码:htm
// 计算move移动块的left值 var move_left = event.clientX - this.offsetLeft - move.offsetWidth/2; // 计算move移动块的top值 var move_top = event.clientY - this.offsetTop - move.offsetHeight/2;
ok,完美解决 ʅ(´◔౪◔)ʃseo
上面说过了,移动块模拟的是放大区域。因此此时移动块与放大区域的盒子,移动块中的图片大小与放大区域盒子图片大小应该是成比例的。图片
/* 计算图片须要移动的坐标 big_x/(look_girl.offsetWidth-big.offsetWidth) = move_left/(small.offsetWidth-move.offsetWidth); big_y/(look_girl.offsetHeight-big.offsetHeight) = move_top/(small.offsetHeight-move.offsetHeight); */ var big_x = move_left/(small.offsetWidth-move.offsetWidth) * (look_girl.offsetWidth-big.offsetWidth); var big_y = move_top/(small.offsetHeight-move.offsetHeight) * (look_girl.offsetHeight-big.offsetHeight);
此时小图盒子的宽度(small.offsetWidth
)正好是移动块中图片的宽度(认为不在移动块中显式的图片都溢出隐藏了)。而后就好像没有什么要解释的了...