window.onload = function() {
let wrap = document.getElementById("wrap")
let bigBox = document.getElementsByClassName("big_box")[0]
let smallBox = document.getElementsByClassName("small_box")[0]
let mask = document.getElementById("mask")
let bigImg = document.getElementById("bigImg")
let list = document.getElementsByClassName("list").children
//监听鼠标进入小盒子
smallBox.onmouseover = function() {
mask.style.display = "block"
bigBox.style.display = "block"
}
//监听鼠标移动
smallBox.onmousemove = function() {
let e = window.event || arguments[0]
//求出鼠标的坐标
let pointX = e.clientX - smallBox.offsetParent.offsetLeft
let pointY = e.clientY - smallBox.offsetParent.offsetTop
//让放大镜移动
//让鼠标在mask中心
pointX = pointX - mask.offsetWidth * 0.5
pointY = pointY - mask.offsetHeight * 0.5
//边界检测
if (pointX < 0) {
pointX = 0
} else if (pointX >= smallBox.offsetWidth - mask.offsetWidth) {
pointX = smallBox.offsetWidth - mask.offsetWidth
}
if (pointY < 0) {
pointY = 0
} else if (pointY >= smallBox.offsetHeight - mask.offsetHeight) {
pointY = smallBox.offsetHeight - mask.offsetHeight
}
mask.style.left = pointX + "px"
mask.style.top = pointY + "px"
//大图移动
bigImg.style.left = -pointX / (smallBox.offsetWidth / bigBox.offsetWidth) + "px"
bigImg.style.top = -pointY / (smallBox.offsetHeight / bigBox.offsetHeight) + "px"
}
//监听鼠标离开小盒子
smallBox.onmouseout = function() {
mask.style.display = "none"
bigBox.style.display = "none"
}
}