NH25-0024
洞洞格肌理+四面弹力+高透气+防水
NH25-0024
NH25-0024
NH25-0020
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".product-image-zoom").forEach(function (container) {
const image = container.querySelector(".product-zoom-image"); const lens = container.querySelector(".product-zoom-lens");
if (!image || !lens) { return; }
const fullImage = container.getAttribute("data-full-image");
if (!fullImage) { return; }
// 设置放大图片 lens.style.backgroundImage = "url(" + fullImage + ")";
container.addEventListener("mousemove", function (event) {
const rect = container.getBoundingClientRect();
const x = event.clientX - rect.left; const y = event.clientY - rect.top;
const containerWidth = rect.width; const containerHeight = rect.height;
// 放大倍数 const zoom = 2.5;
// 放大镜大小 const lensWidth = lens.offsetWidth; const lensHeight = lens.offsetHeight;
// 放大镜跟随鼠标 let left = x - lensWidth / 2; let top = y - lensHeight / 2;
// 防止放大镜超出图片 if (left < 0) { left = 0; } if (top < 0) { top = 0; } if (left + lensWidth > containerWidth) { left = containerWidth - lensWidth; }
if (top + lensHeight > containerHeight) { top = containerHeight - lensHeight; }
lens.style.left = left + "px"; lens.style.top = top + "px";
// 设置放大背景尺寸 lens.style.backgroundSize = (containerWidth * zoom) + "px " + (containerHeight * zoom) + "px";
// 计算背景位置 const bgX = -(x * zoom - lensWidth / 2);
const bgY = -(y * zoom - lensHeight / 2);
lens.style.backgroundPosition = bgX + "px " + bgY + "px";
});
});
});

