JS弹出与关闭页面:代码示例

4星 · 超过85%的资源 需积分: 50 36 下载量 132 浏览量 更新于2024-07-29 收藏 92KB DOC 举报
"JavaScript弹出与关闭页面窗口技术详解" JavaScript是一种强大的客户端脚本语言,广泛应用于网页交互和用户界面操作。在网页开发中,我们有时需要弹出新的窗口或者关闭已打开的窗口,以实现特定的功能,如打开新窗口显示详细信息、弹出对话框确认操作等。本篇将详细介绍JavaScript中弹出页面窗口和关闭页面窗口的方法。 1. **弹出新窗口** - `window.open(url, name, features)` 是JavaScript中用于打开新窗口的函数。 - `url` 参数定义了要打开的页面URL。 - `name` 参数定义了新窗口的名称,可以为空,也可以用于后续的引用。 - `features` 参数是一个字符串,包含新窗口的各种属性,如宽度、高度、位置、是否显示滚动条等,例如:`'width=500,height=300,menubar=yes,toolbar=yes'`。 2. **关闭当前窗口** - `window.close()` 是用来关闭当前窗口的函数。但需要注意,只有由JavaScript本身打开的窗口才能被JavaScript关闭,否则会因为浏览器的安全策略而无法执行。 3. **JS定时自动关闭窗口** 可以通过设置定时器(`setTimeout`)来实现页面的自动关闭,如上述代码所示。首先定义一个计时器函数,每次减少1秒,当时间归零时调用`closewin()`函数关闭窗口。 4. **点击链接关闭窗口** 在链接的`href`属性中直接使用JavaScript语句,如`<a href="javascript:self.close()">关闭窗口</a>`,点击链接时会关闭当前窗口。 5. **关闭窗口不提示** 在某些情况下,我们可能希望关闭窗口时不出现浏览器的确认提示。这可以通过设置`window.opener`为`null`来实现,例如: ```javascript window.opener = null; window.close(); ``` 对于IE6-7,还需要额外的处理方式,如上述代码中的`CloseWin()`函数。 6. **不同浏览器的兼容性处理** 不同的浏览器可能对窗口关闭有不同的处理方式,例如IE6和IE7可能需要特殊的处理来避免关闭提示。例如`closeie6()`和`closeie7()`函数。 7. **`window.open()`详解** `window.open()` 方法还有更复杂的用法,可以指定新窗口是否在现有窗口中打开( `_self`、`_blank`、`_parent`、`_top` 等),以及窗口的特性。`_self`表示在当前窗口中打开,`_blank`表示在新窗口打开,`_parent`和`_top`则涉及到框架结构的处理。 JavaScript提供了丰富的功能来控制页面窗口的打开和关闭,开发者可以根据需求灵活运用。在实际应用中,需要注意浏览器的兼容性和用户体验,合理使用这些技术。

function showPopup(imageSrc) { var popup = document.createElement("div"); var popupImg = document.createElement("img"); var scale = 1; var isDragging = false; var startX, startY, translateX, translateY; // 设置弹出窗口中的图片 popupImg.src = imageSrc; popupImg.style.transform = `scale(${scale})`; // 加载完成后计算图片的宽高比例 popupImg.onload = function() { var imgWidth = popupImg.width; var imgHeight = popupImg.height; // 计算图片的缩放比例 var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 var widthScale = windowWidth / imgWidth; var heightScale = windowHeight / imgHeight; scale = Math.min(widthScale, heightScale); // 设置弹出窗口中的图片样式 popupImg.style.transform = `scale(${scale})`; popupImg.style.display = "block"; popupImg.style.margin = "auto"; // 居中弹出窗口 function centerPopup() { var windowHeight = window.innerHeight; var popupHeight = popup.offsetHeight; var scrollTop = window.pageYOffset || document.documentElement.scrollTop; var offset = (windowHeight - (imgHeight * scale)) / 2 + scrollTop; // 设置弹出窗口的垂直位置 popup.style.top = offset + "px"; } // 设置悬浮窗样式 popup.style.position = "fixed"; popup.style.top = "10%"; popup.style.left = "10%"; popup.style.width = windowWidth + "px"; popup.style.height = windowHeight + "px"; popup.style.backgroundColor = "transparent"; popup.style.zIndex = "9999"; // 添加图片到悬浮窗 popup.appendChild(popupImg); document.body.appendChild(popup); // 初始化居中弹出窗口 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(); }; };需要不管第几行第几列的图片放大时,放大的悬浮窗始终在当前页面最中心

2023-07-14 上传

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(); };我在添加图片后需要图片始终显示在当前页面中间位置,并且能够随着滚动条滑动,始终在当前页面中间显示

2023-07-13 上传