上面你提供的代码需要怎么添加到function showPopup(src) { isPopupVisible = true; // 设置悬浮窗显示状态为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低于悬浮窗,但高于其他元素 document.body.appendChild(overlay); 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 = "100%"; img.style.maxHeight = "100%"; var scale = 1; // 初始缩放比例 var scaleFactor = 0.1; // 每次滚动的缩放因子 // 点击图片放大/缩小 img.onclick = function () { scale += 0.1; img.style.transform = "scale(" + scale + ")"; }; // 鼠标滚轮事件 popup.onwheel = function (e) { e.preventDefault(); // 阻止默认滚轮行为处理页面滚动 scale += e.deltaY > 0 ? -scaleFactor : scaleFactor; // 根据滚轮滚动方向确定缩放比例 scale = Math.max(scale, 0.1); // 最小缩放比例为0.1 img.style.transform = "scale(" + scale + ")"; }; overlay.onclick = function () { document.body.removeChild(popup); document.body.removeChild(overlay); isPopupVisible = false; // 设置悬浮窗显示状态为false }; imgContainer.appendChild(img); popup.appendChild(imgContainer); document.body.appendChild(popup); }
时间: 2024-04-17 07:27:31 浏览: 122
您可以将我之前提供的代码添加到您的`showPopup`函数中的适当位置。具体来说,您可以将代码添加到创建`img`元素后,即在以下代码之后:
```javascript
var img = document.createElement("img");
img.src = src;
img.alt = "放大照片";
img.style.maxWidth = "100%";
img.style.maxHeight = "100%";
```
将我的代码添加到这段代码之后,以保持逻辑的连贯性。此外,请确保将我的代码添加到整个`showPopup`函数中的正确位置,以使其在适当的时机执行。
最终的代码应如下所示:
```javascript
function showPopup(src) {
isPopupVisible = true; // 设置悬浮窗显示状态为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低于悬浮窗,但高于其他元素
document.body.appendChild(overlay);
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 = "100%";
img.style.maxHeight = "100%";
// 添加我的代码开始
var isDragging = false; // 判断是否正在拖动图片
var startX, startY; // 记录鼠标开始拖动的位置
var offsetX = 0, offsetY = 0; // 记录图片偏移量
// 鼠标按下事件,开始拖动图片
img.onmousedown = function (e) {
isDragging = true;
startX = e.clientX;
startY = e.clientY;
img.style.cursor = "grabbing";
};
// 鼠标移动事件,拖动图片
img.onmousemove = function (e) {
if (isDragging) {
var deltaX = e.clientX - startX;
var deltaY = e.clientY - startY;
offsetX += deltaX;
offsetY += deltaY;
img.style.transform = "scale(" + scale + ") translate(" + offsetX + "px, " + offsetY + "px)";
startX = e.clientX;
startY = e.clientY;
}
};
// 鼠标释放事件,停止拖动图片
img.onmouseup = function () {
isDragging = false;
img.style.cursor = "grab";
};
// 鼠标离开事件,停止拖动图片
img.onmouseleave = function () {
isDragging = false;
img.style.cursor = "grab";
};
// 设置鼠标样式为手掌状
img.style.cursor = "grab";
// 点击图片放大/缩小
img.onclick = function () {
scale += 0.1;
img.style.transform = "scale(" + scale + ")";
};
// 鼠标滚轮事件
popup.onwheel = function (e) {
e.preventDefault(); // 阻止默认滚轮行为处理页面滚动
scale += e.deltaY > 0 ? -scaleFactor : scaleFactor; // 根据滚轮滚动方向确定缩放比例
scale = Math.max(scale, 0.1); // 最小缩放比例为0.1
img.style.transform = "scale(" + scale + ")";
};
// 添加我的代码结束
overlay.onclick = function () {
document.body.removeChild(popup);
document.body.removeChild(overlay);
isPopupVisible = false; // 设置悬浮窗显示状态为false
};
imgContainer.appendChild(img);
popup.appendChild(imgContainer);
document.body.appendChild(popup);
}
```
请确保将代码添加到正确的位置,并与现有代码一起工作。希望这次能帮到您!如果还有其他问题,请随时提问。
阅读全文