页面滚动时div窗口始终居中

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/JavaScript">
        function fn1() {
            var _div = document.getElementById("div1");
            fnShowDiv(_div);
        }
        //显示指定DIV 
        function fnShowDiv(_div) {
            var iWidth = document.documentElement.clientWidth;
            var iHeight = document.documentElement.clientHeight + document.documentElement.scrollTop * 2;
            var h = 70; //div 高
            var w = 260; //div 宽

            _div.style.top = (iHeight - h) / 2 + "px";
            _div.style.left = (iWidth - w) / 2 + "px";
            _div.style.display = "block";
            _div.style.position = "absolute";
            _div.style.width = w + "px";
            _div.style.height = h + "px";
            _div.style.textAlign = "center";
            _div.style.margin = "0 auto";
            _div.style.background = "red";
            
        }
    </script>
</head>
<body>
<div id="div1" style=""></div>
<input type="button" id="btn1" style=" margin:700px auto" value="测试" οnclick="fn1()" />
</body>
</html>

转载于:https://www.cnblogs.com/liwentao/p/7124207.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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(); };我在添加图片后需要图片始终显示在当前页面中间位置,并且能够随着滚动条滑动,始终在当前页面中间显示
最新发布
07-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值