<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0px;
padding: 0px;
}
#small {
width: 322px;
height: 480px;
position: absolute;
left: 100px;
top: 100px;
border: 1px solid black;
}
#small img {
width: 100%;
height: 100%;
}
#mark {
width: 200px;
height: 200px;
background-color: black;
position: absolute;
opacity: 0.5;
filter: aplha(opacity=50);
left: 0px;
top: 0px;
display: none;
}
#big {
width: 400px;
height: 400px;
border: 1px solid black;
position: absolute;
top: 100px;
left: 500px;
display: none;
overflow: hidden;
}
#big img {
width: 644px;
height: 960px;
position: absolute;
}
</style>
<script>
/*
JS版本的放大镜
*/
window.onload = function(){
var oSmall = document.getElementById("small");
var oBig = document.getElementById("big");
var oMark = document.getElementById("mark");
var oBigImg = oBig.getElementsByTagName("img")[0];
oSmall.onmouseover = function(){
oMark.style.display = 'block';
oBig.style.display = 'block';
}
oSmall.onmouseout = function(){
oMark.style.display = 'none';
oBig.style.display = 'none';
}
//添加鼠标移动事件
oSmall.onmousemove = function(ev){
var e = ev || window.event;
var l = e.clientX - oSmall.offsetLeft - 100;
var t = e.clientY - oSmall.offsetTop - 100;
//限制遮罩层出界
if(l <= 0){
l = 0;
}
if(l >= 122){
l = 122;
}
if(t <= 0){
t = 0;
}
if(t >= 280){
t = 280;
}
oMark.style.left = l + 'px';
oMark.style.top = t + 'px';
//大图片方向移动对应放大倍数的坐标
oBigImg.style.left = -2 * l + 'px';
oBigImg.style.top = -2 * t + 'px';
}
}
</script>
</head>
<body>
<div id='small'>
<img src="img/WaterF6.jpg" alt="">
<div id='mark'></div>
</div>
<div id='big'>
<img src="img/WaterF6.jpg" alt="">
</div>
</body>
</html>