我想写一个放大镜效果,为什么出不来呢?
<!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;
}
#left,#right{
float: left;
}
#left img{
width: 200px;
}
#right{
width: 100px;
height: 100px;
border: 1px solid #000;
overflow: hidden;
margin-left: 20px;
position: relative;
}
#right-img{
position: absolute;
}
</style>
<script>
window.onload=function(){
var leftImg=document.getElementById("left-img");
var rightImg=document.getElementById("right-img");
document.onmousemove=function(e){
var e=e||window.event
// console.log(e.clientX)
var num=rightImg.offsetWidth/leftImg.offsetWidth
rightImg.style.position="absolute"
rightImg.style.left=num*e.clientX;
rightImg.style.top=num*e.clientY;
}
}
</script>
</head>
<body>
<div id="left">
<img id="left-img" src="./images/img-3.jpg" alt="">
</div>
<div id="right">
<img id="right-img" src="./images/img-3.jpg" alt="" style="width: 1000px;">
</div>
</body>
</html>