function showPopup(src) { if (isPopupVisible) { return; // 如果悬浮窗已经显示,则直接返回 } isPopupVisible = true; // 标记悬浮窗为显示状态 var overlay = document.createElement("div"); overlay.style.position = "fixed"; overlay.style.top = "0"; overlay.style.left = "0"; overlay.style.width = "100%"; overlay.style.height = "100%"; overlay.style.backgroundColor = "rgba(0, 0, 0, 0.8)"; overlay.style.zIndex = "9998"; // 设置遮罩层的z-index低于悬浮窗,但高于其他元素 popup.parentNode.insertBefore(overlay, popup); var popup = document.createElement("div"); popup.style.position = "fixed"; popup.style.top = "50%"; popup.style.left = "50%"; popup.style.transform = "translate(-50%, -50%)"; popup.style.backgroundColor = "rgba(0, 0, 0, 0.8)"; popup.style.zIndex = "9999"; popup.style.width = "75%"; // 设置悬浮窗宽度为页面宽度的75% popup.style.height = "75%"; // 设置悬浮窗高度为页面高度的75% popup.style.overflow = "hidden"; // 使图片在悬浮窗居中 var imgContainer = document.createElement("div"); imgContainer.style.display = "flex"; imgContainer.style.justifyContent = "center"; imgContainer.style.alignItems = "center"; imgContainer.style.backgroundColor = "transparent"; imgContainer.style.width = "100%"; imgContainer.style.height = "100%"; var img = document.createElement("img"); img.src = src; img.alt = "放大照片"; img.style.maxWidth = "100vw"; img.style.maxHeight = "100vh"; var scale = 1; // 初始缩放比例 var scaleFactor = 0.1; // 每次滚动的缩放因子 // 鼠标滚轮事件 img.onwheel = function (e) { e.preventDefault(); // 阻止默认滚轮行为处理页面滚动 scale += e.deltaY > 0 ? -scaleFactor : scaleFactor; // 根据滚轮滚动方向确定缩放比例 scale = Math.max(scale, 0.1); // 最小缩放比例为0.1 img.style.transform = `scale(${scale})`; }; // 关闭悬浮窗 popup.onclick = function () { document.body.removeChild(popup); document.body.removeChild(overlay); isPopupVisible = false; // 标记悬浮窗为隐藏状态 }; imgContainer.appendChild(img); popup.appendChild(imgContainer); document.body.appendChild(popup); overlay.onclick = function () { document.body.removeChild(popup); document.body.removeChild(overlay); isPopupVisible = false; // 标记悬浮窗为隐藏状态 }; }当我修改后无法点击放大图片
时间: 2024-04-17 13:26:33 浏览: 138
在您的代码中,我注意到您将遮罩层的创建代码放在了`popup`元素之前,这可能导致遮罩层覆盖在悬浮窗的上方,从而导致无法点击放大图片。
为了解决这个问题,您需要调整代码的顺序。确保先创建悬浮窗元素`popup`,然后创建遮罩层元素`overlay`,并将遮罩层元素插入到悬浮窗元素之前。
请参考以下修改后的代码:
```javascript
function showPopup(src) {
if (isPopupVisible) {
return; // 如果悬浮窗已经显示,则直接返回
}
isPopupVisible = true; // 标记悬浮窗为显示状态
var popup = document.createElement("div");
popup.style.position = "fixed";
popup.style.top = "50%";
popup.style.left = "50%";
popup.style.transform = "translate(-50%, -50%)";
popup.style.backgroundColor = "rgba(0, 0, 0, 0.8)";
popup.style.zIndex = "9999";
popup.style.width = "75%"; // 设置悬浮窗宽度为页面宽度的75%
popup.style.height = "75%"; // 设置悬浮窗高度为页面高度的75%
popup.style.overflow = "hidden";
var overlay = document.createElement("div");
overlay.style.position = "fixed";
overlay.style.top = "0";
overlay.style.left = "0";
overlay.style.width = "100%";
overlay.style.height = "100%";
overlay.style.backgroundColor = "rgba(0, 0, 0, 0.8)";
overlay.style.zIndex = "9998"; // 设置遮罩层的z-index低于悬浮窗,但高于其他元素
popup.parentNode.insertBefore(overlay, popup);
var imgContainer = document.createElement("div");
imgContainer.style.display = "flex";
imgContainer.style.justifyContent = "center";
imgContainer.style.alignItems = "center";
imgContainer.style.backgroundColor = "transparent";
imgContainer.style.width = "100%";
imgContainer.style.height = "100%";
var img = document.createElement("img");
img.src = src;
img.alt = "放大照片";
img.style.maxWidth = "100vw";
img.style.maxHeight = "100vh";
var scale = 1; // 初始缩放比例
var scaleFactor = 0.1; // 每次滚动的缩放因子
// 鼠标滚轮事件
img.onwheel = function (e) {
e.preventDefault(); // 阻止默认滚轮行为处理页面滚动
scale += e.deltaY > 0 ? -scaleFactor : scaleFactor; // 根据滚轮滚动方向确定缩放比例
scale = Math.max(scale, 0.1); // 最小缩放比例为0.1
img.style.transform = `scale(${scale})`;
};
// 关闭悬浮窗
popup.onclick = function () {
document.body.removeChild(popup);
document.body.removeChild(overlay);
isPopupVisible = false; // 标记悬浮窗为隐藏状态
};
imgContainer.appendChild(img);
popup.appendChild(imgContainer);
document.body.appendChild(overlay);
document.body.appendChild(popup);
}
```
请尝试使用以上修改后的代码,并重新加载页面,看看是否能够点击放大图片。如果仍然遇到问题,请提供更多详细信息,以便我能够更好地帮助您解决问题。
阅读全文