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 19:27:32 浏览: 18
为了在悬浮窗放大图片后实现鼠标在图片上变为手掌状,并且可以拖动图片查看不同位置,您可以在`showPopup`函数中添加一些代码来实现这个功能。请将以下代码添加到您的JavaScript中:
```javascript
var isDragging = false; // 判断是否正在拖动图片
var startX, startY; // 记录鼠标开始拖动的位置
var offsetX = 0, offsetY = 0; // 记录图片偏移量
// 鼠标按下事件,开始拖动图片
img.onmousedown = function (e) {
isDragging = true;
startX = e.clientX;
startY = e.clientY;
};
// 鼠标移动事件,拖动图片
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.onmouseleave = function () {
isDragging = false;
};
// 设置鼠标样式为手掌状
img.style.cursor = "grab";
// 鼠标按下时,更改鼠标样式为抓取状
img.onmousedown = function () {
img.style.cursor = "grabbing";
};
// 鼠标释放时,恢复鼠标样式为手掌状
img.onmouseup = function () {
img.style.cursor = "grab";
};
// 鼠标离开时,恢复鼠标样式为手掌状
img.onmouseleave = function () {
img.style.cursor = "grab";
};
```
这样,当您在悬浮窗中放大图片后,鼠标会变为手掌状,并且可以拖动图片查看不同位置。希望对您有帮助!如果还有其他问题,请随时提问。