码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-06 01:05:47      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:func   software   head   div   script   get   show   net   xhtml   

<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="测试" onclick="fn1()" />
</body>
</html>

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

标签:func   software   head   div   script   get   show   net   xhtml   

原文地址:http://www.cnblogs.com/liwentao/p/7124207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!