zoukankan      html  css  js  c++  java
  • 【居中】一

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div id="btn" style="margin-top:1000px;">btn</div>
        <div id="box" style="display:none;position:absolute;100px;height:100px;background:#f00"></div>
        <script type="text/javascript">
        function scrollX () {
            var de = document.documentElement;
            return self.pageXOffset ||
                (de && de.scrollLeft) ||
                document.body.scrollLeft;
        }
        function scrollY () {
            var de = document.documentElement;
            return self.pageYOffset ||
                (de && de.scrollTop) ||
                document.body.scrollTop;
        }
        function windowHeight () {
            var de = document.documentElement;
            return self.innerHeight ||
                (de && de.clientHeight) ||
                document.body.clientHeight;
        }
        function windowWidth () {
            var de = document.documentElement;
            return self.innerWidth ||
                (de && de.clientWidth) ||
                document.body.clientWidth;
        }
        function setPos () {
            var t = (windowHeight() - 100)/2 + scrollY(),
                l = (windowWidth() - 100)/2 + scrollX();
            document.getElementById('box').style.top = t + 'px';
            document.getElementById('box').style.left = l + 'px';
            document.getElementById('box').style.display = 'block';
        }
        document.getElementById('btn').onclick = function () {
            setPos();
        };    
        window.onresize = function(){
            setPos();
        }
        </script>
    </body>
    </html>

    by 《精通JavaScript》

  • 相关阅读:
    python中的编码与解码
    python中的迭代器
    python中高阶函数与装饰器(3)
    python中高阶函数与装饰器(2)
    python中高阶函数与装饰器
    python中几种常用的数据类型
    python 启航
    Python [习题] 字典排序
    SQL-TSQL
    SQL基础语句
  • 原文地址:https://www.cnblogs.com/jzm17173/p/2526656.html
Copyright © 2011-2022 走看看