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》

  • 相关阅读:
    倒计时模块(获取的是服务器上的时间——适合购物网站的抢购模块)
    javascript 双色球选号器
    最短AJAX创建代码
    poj2387 Til the Cows Come Home
    POJ2236 Wireless Network
    HDU2102 A计划
    HDU2028 Lowest Common Multiple Plus
    HDU2612 Find a way
    HDU1495 非常可乐
    UVa10603 Fill
  • 原文地址:https://www.cnblogs.com/jzm17173/p/2526656.html
Copyright © 2011-2022 走看看