window.onload = () => { let wrap = document.querySelector(".wrap"); let min = document.querySelector(".min"); let min_img = document.querySelector(".min img"); let min_wrap = document.querySelector(".min_wrap"); let max_img = document.querySelector(".max_wrap img"); let max_wrap = document.querySelector(".max_wrap");
// 鼠标移动事件 wrap.onmousemove = e => { // 兼容写法 var e = e || window.event; // 获取鼠标的移动位置 let x = e.clientX - wrap.offsetLeft - min_wrap.offsetWidth / 2; let y = e.clientY - wrap.offsetTop - min_wrap.offsetHeight / 2;
// 判断不要超出框架 if (x < 0) { x = 0; } elseif (x > min_img.offsetWidth - min_wrap.offsetWidth) { x = min_img.offsetWidth - min_wrap.offsetWidth; }
if (y < 0) { y = 0; } elseif (y > min_img.offsetHeight - min_wrap.offsetHeight) { y = min_img.offsetHeight - min_wrap.offsetHeight; }
// 设置遮罩移动 min_wrap.style.left = x + "px"; min_wrap.style.top = y + "px";