使用js做出放大镜效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>放大镜</title>
<style>
*{
padding: 0;
margin: 0;
}
#small{
width: 300px;
height: 225px;
float: left;
position: relative;
}
#mark{
width: 100px;
height: 100px;
opacity: 0.3;
background-color: black;
position: absolute;
left: 0;
top: 0;
}
#big{
overflow: hidden;
width: 300px;
height: 300px;
float: left;
margin-left: 30px;
position: relative;
}
#tu{
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div id="box">
<div id="small">
<div><img src="images/smallpic.jpg" alt=""></div>
<span id="mark"></span>
</div>
<div id="big">
<img src="images/bigpic.jpg" alt="" id="tu">
</div>
</div>
<script>
var small = document.getElementById("small");
var mark = document.getElementById("mark");
var tu = document.getElementById("tu");
small.onmouseover = function(e){
var event = e||event;
//鼠标距离浏览器的距离 event.clientX
//小图片距离浏览器的距离 small.offsetLeft
var left = event.clientX -small.offsetLeft-(mark.offsetWidth/2);
var top = event.clientY -small.offsetTop-(mark.offsetHeight/2);
var maxW = small.offsetWidth-mark.offsetWidth;
var maxH = small.offsetHeight-mark.offsetHeight;
if(left<0){left = 0};
if(top<0){top =0}
if(left>maxW){
left = maxW;
}
if(top>maxH){
top= maxH;
}
// console.log(left);
mark.style.left = left+"px";
mark.style.top = top+"px";
tu.style.left = -left*(tu.offsetWidth/small.offsetWidth)+"px";
tu.style.top = -top*(tu.offsetHeight/small.offsetHeight)+"px";
}
</script>
</body>
</html>