<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
#__bs_notify__ {
display: none !important;
}
nav {
display: flex;
}
.xt {
width: 400px;
height: 400px;
border: 1px solid;
position: relative;
}
.xt>img {
width: 190px;
height: 257px;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
}
.datu {
width: 400px;
height: 400px;
margin-left: 50px;
border: 1px solid;
position: relative;
overflow: hidden;
display: none;
}
.datu>p {
position: absolute;
left: 0;
top: 0;
width: 700px;
height: 700px;
z-index: 3;
}
.datu>p>img {
width: 380px;
height: 514px;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
}
.mask {
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: 0;
background-color: pink;
opacity: .5;
display: none;
}
</style>
</head>
<body>
<nav>
<div class="xt">
<img src="https://www.apple.com.cn/v/iphone/home/bk/images/overview/compare/compare_iphone_14_pro__cjmfbiggqhpy_large.jpg"
alt="">
<p class="mask"></p>
</div>
<div class="datu">
<p>
<img src="https://www.apple.com.cn/v/iphone/home/bk/images/overview/compare/compare_iphone_14_pro__cjmfbiggqhpy_large.jpg"
alt="">
</p>
</div>
</nav>
<script>
let mask = document.querySelector(".mask");
let datu = document.querySelector(".datu");
let xt = document.querySelector(".xt");
let p = document.querySelector(".datu>p")
let nav = document.querySelector("nav")
xt.addEventListener("mouseover", function () {
mask.style.display = "block"
datu.style.display = "block"
})
xt.addEventListener("mouseout", function () {
mask.style.display = "none"
datu.style.display = "none"
})
xt.addEventListener("mousemove", function (e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
var maskx = x - mask.offsetWidth / 2
var masky = y - mask.offsetHeight / 2
var max = xt.offsetWidth - mask.offsetWidth
if (maskx <= 0) {
maskx = 0
} else if (maskx >= max) {
maskx = max;
}
if (masky <= 0) {
masky = 0
} else if (masky >= max) {
masky = max;
}
mask.style.left = maskx + "px"
mask.style.top = masky + "px"
var imgs = datu.offsetWidth - p.offsetWidth
var bigx = maskx * imgs / max
var bigy = masky * imgs / max
p.style.left = bigx + "px"
p.style.top = bigy + "px"
})
</script>
</body>
</html>