<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>图片放大镜</title>
<style>
#d1{position: relative;cursor: pointer;width: 349px;height: 220px;float: left;}
#img1{position: relative;}
span{display:none;width:50px;height:50px;background: yellow;opacity:.3;position: absolute;top:0;left:0;}
#big{width:400px;height:400px;overflow: hidden;float: left;position: relative;display: none;}
#img2{position: absolute;}
</style>
</head>
<body>
<div id="d1">
<img src="img/78.jpg" alt="" id="img1" />
<span id="span1"></span>
</div>
<div id="big">
<img src="img/78.jpg" alt="" id="img2" width="1745px" height="1100px"/>
</div>
var big=document.getElementById("big");
var img1=document.getElementById("img1");
var img2=document.getElementById("img2");
var top=event.clientY-25;
if(top>img1.height-50){
top=img1.height-50;
}
if(top<0){
top=0;
}
if(left<0){
left=0;
}
if(left>img1.width-50){
left=img1.width-50;
}
span.style.left=left+"px";
span.style.top=top+"px";
span.style.display="block";
img2.style.left=-left*5+"px";
img2.style.top=-top*5+"px";
big.style.display="block";
};
d1.οnmοuseleave= function () {
span.style.display="none";
big.style.display="none";
};
</script>
</body>
</html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>图片放大镜</title>
<style>
#d1{position: relative;cursor: pointer;width: 349px;height: 220px;float: left;}
#img1{position: relative;}
span{display:none;width:50px;height:50px;background: yellow;opacity:.3;position: absolute;top:0;left:0;}
#big{width:400px;height:400px;overflow: hidden;float: left;position: relative;display: none;}
#img2{position: absolute;}
</style>
</head>
<body>
<div id="d1">
<img src="img/78.jpg" alt="" id="img1" />
<span id="span1"></span>
</div>
<div id="big">
<img src="img/78.jpg" alt="" id="img2" width="1745px" height="1100px"/>
</div>
<script>
var big=document.getElementById("big");
var img1=document.getElementById("img1");
var img2=document.getElementById("img2");
var span = document.getElementById("span1");
img1.onmousemove = function () {
var top=event.clientY-25;
if(top>img1.height-50){
top=img1.height-50;
}
if(top<0){
top=0;
}
if(left<0){
left=0;
}
if(left>img1.width-50){
left=img1.width-50;
}
span.style.left=left+"px";
span.style.top=top+"px";
span.style.display="block";
img2.style.left=-left*5+"px";
img2.style.top=-top*5+"px";
big.style.display="block";
};
d1.οnmοuseleave= function () {
span.style.display="none";
big.style.display="none";
};
</script>
</body>
</html>