凡客放大镜效果
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.left{
width:400px;
height: 400px;
border:1px solid yellow;
background:url("img/f.jpg") no-repeat left top;
background-size:cover;
float:left;
margin-right:10px;
position: relative;
}
.box{width:220px;height:200px;background:yellow;opacity: 0.5;
position: absolute;left:0;top:0;
cursor: crosshair;
display: none;}
#cover{width:400px;height:400px;position: absolute;left:0;top:0;
background:red;opacity: 0.2;}
.right{width:440px;height:400px;border:2px solid red;
position: relative;overflow: hidden;display: none;}
#pic{position: absolute;left:-400px;top:-360px;}
</style>
</head>
<body>
<div class="main clearfix">
<div class="left">
<div class="box"></div>
<div id="cover"></div>
</div>
<div class="right">
<img src="img/f.jpg" alt="style" id="pic"/>
</div>
</div>
</body>
<script>
var leftBox=document.getElementsByClassName("left")[0];
var coverBox=document.getElementById("cover");
var moveBox=document.getElementsByClassName("box")[0];
var rightBox=document.getElementsByClassName("right")[0];
var movePic=document.getElementById("pic")
coverBox.onmousemove=function(e){
var ev=window.event || e;
var mouse_left=ev.offsetX || ev.layerX;
var mouse_top=ev.offsetY || ev.layerY;
var box_left=mouse_left-110;
var box_top=mouse_top-100;
if(box_left>=180){box_left=180}
if(box_left<=0) {box_left=0}
if(box_top>=200) {box_top=200}
if(box_top<=0) {box_top=0}
moveBox.style.left=box_left+"px"
moveBox.style.top=box_top+"px"
var Pic_left=box_left*(-2)
var Pic_top=box_top*(-2)
movePic.style.left=Pic_left+"px"
movePic.style.top=Pic_top+"px"
}
coverBox.onmouseover=function(){
moveBox.style.display="block"
rightBox.style.display="block"
}
coverBox.onmouseout=function(){
moveBox.style.display="none"
rightBox.style.display="none"
}
</script>
</html>
注意大小关系
左边盒子:400x400
移动盒子:220x200
右边盒子:440x400
图片大小:800x400