放大镜
<!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: 0;
padding: 0;
}
#img1 {
height: 200px;
position: relative;
}
#img2 {
height: 400px;
width: 640px;
position: relative;
}
#yellow {
height: 100px;
width: 100px;
opacity: 0.2;
background-color: yellow;
display: none;
position: absolute;
z-index: 4;
}
#show{
height: 200px;
width: 320px;
display: none;
background-color: black;
position: absolute;
overflow: hidden;
}
</style>
</head>
<body>
<img src="https://th.bing.com/th/id/OIP.wvYWXhrPa5hqIOi2dvE73QHaEo?w=290&h=181&c=7&r=0&o=5&dpr=1.5&pid=1.7"
id="img1" alt="">
<div id="yellow"></div>
<div id="show"><img src="https://th.bing.com/th/id/OIP.wvYWXhrPa5hqIOi2dvE73QHaEo?w=290&h=181&c=7&r=0&o=5&dpr=1.5&pid=1.7"
id="img2" alt=""></div>
<script src="./放大镜效果.js"></script>
</body>
</html>
var img1 = document.querySelector("#img1")
var yellow = document.querySelector("#yellow")
var show = document.querySelector("#show")
var img2=document.querySelector('#img2')
var yl=yellow.offsetleft
img1.onmouseover = function() {
yellow.style.display='block'
show.style.display='inline-block'
}
img1.onmousemove = function (sb) {
yellow.style.left= sb.offsetX +'px'
yellow.style.top= sb.offsetY+'px'
var yt=yellow.style.top
img2.style.top= -sb.offsetY*2+'px'
img2.style.left= -sb.offsetX*2+'px'
}