function previewImage(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { var img = document.createElement("img"); img.src = e.target.result; img.alt = "照片"; img.onclick = function () { showPopup(img.src); }; // 隐藏原始的input元素 input.style.display = "none"; // 添加图片到input元素的父节点 input.parentNode.appendChild(img); }; reader.readAsDataURL(input.files[0]); } } function showPopup(imageSrc) { var popup = document.createElement("div"); var popupImg = document.createElement("img"); var scale = 1; // 设置弹出窗口中的图片 popupImg.src = imageSrc; popupImg.style.transform = `scale(${scale})`; // 设置悬浮窗样式 popup.style.position = "fixed"; popup.style.top = "10%"; popup.style.left = "10%"; popup.style.width = "80%"; popup.style.height = "80%"; popup.style.backgroundColor = "transparent"; popup.style.zIndex = "9999"; // 添加图片到悬浮窗 popup.appendChild(popupImg); document.body.appendChild(popup); // 居中弹出窗口 function centerPopup() { var windowHeight = window.innerHeight; var popupHeight = popup.offsetHeight; // 计算弹出窗口的垂直偏移量 var scrollTop = window.pageYOffset || document.documentElement.scrollTop; var offset = (windowHeight - popupHeight) / 2 + scrollTop; // 设置弹出窗口的垂直位置 popup.style.top = offset + "px"; } // 初始化居中弹出窗口 centerPopup(); // 添加滚动和调整窗口大小事件监听器 window.addEventListener("scroll", centerPopup); window.addEventListener("resize", centerPopup); // 添加点击事件监听器,点击其他区域时隐藏弹出窗口 popup.onclick = function () { popup.style.display = "none"; } // 移除滚动和调整窗口大小事件监听器 function removeListeners() { window.removeEventListener("scroll", centerPopup); window.removeEventListener("resize", centerPopup); } // 在窗口关闭时移除事件监听器 window.onbeforeunload = function () { removeListeners(); };我在添加图片后需要图片始终显示在当前页面中间位置,并且能够随着滚动条滑动,始终在当前页面中间显示
时间: 2024-04-12 09:31:27 浏览: 21
您可以将`centerPopup`函数从`showPopup`函数中提取出来,并且在滚动和调整窗口大小时重新计算并设置弹出窗口的位置。同时,您还需要在图片点击事件中重新计算并设置弹出窗口的位置。以下是修改后的代码:
```javascript
function previewImage(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
var img = document.createElement("img");
img.src = e.target.result;
img.alt = "照片";
img.onclick = function () {
showPopup(img.src);
};
// 隐藏原始的input元素
input.style.display = "none";
// 添加图片到input元素的父节点
input.parentNode.appendChild(img);
};
reader.readAsDataURL(input.files[0]);
}
}
function showPopup(imageSrc) {
var popup = document.createElement("div");
var popupImg = document.createElement("img");
var scale = 1;
// 设置弹出窗口中的图片
popupImg.src = imageSrc;
popupImg.style.transform = `scale(${scale})`;
// 设置悬浮窗样式
popup.style.position = "fixed";
popup.style.width = "80%";
popup.style.height = "80%";
popup.style.backgroundColor = "transparent";
popup.style.zIndex = "9999";
// 添加图片到悬浮窗
popup.appendChild(popupImg);
document.body.appendChild(popup);
// 居中弹出窗口
function centerPopup() {
var windowHeight = window.innerHeight;
var popupHeight = popup.offsetHeight;
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
var offset = (windowHeight - popupHeight) / 2 + scrollTop;
// 设置弹出窗口的垂直位置
popup.style.top = offset + "px";
}
// 初始化居中弹出窗口
centerPopup();
// 添加滚动和调整窗口大小事件监听器
window.addEventListener("scroll", centerPopup);
window.addEventListener("resize", centerPopup);
// 点击事件监听器,点击其他区域时隐藏弹出窗口
popup.onclick = function () {
popup.style.display = "none";
};
// 移除滚动和调整窗口大小事件监听器
function removeListeners() {
window.removeEventListener("scroll", centerPopup);
window.removeEventListener("resize", centerPopup);
}
// 在窗口关闭时移除事件监听器
window.onbeforeunload = function () {
removeListeners();
};
// 图片点击事件,重新计算并设置弹出窗口的位置
popupImg.onclick = function () {
centerPopup();
};
}
```
通过将`centerPopup`函数提取出来,并在滚动、调整窗口大小以及图片点击事件中重新计算并设置弹出窗口的位置,您可以实现图片始终显示在当前页面中间位置,并且能够随着滚动条滑动而保持在中间位置。
希望这对您有所帮助!如果您还有其他问题,请随时提问。