码迷,mamicode.com
首页 > Web开发 > 详细

JS弹窗——点击/滚动/缩放居中

时间:2015-03-13 12:11:33      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

CSS代码:

#mask {background: black; opacity: 0.3; filter: alpha(opacity=30); position: absolute; left: 0; top: 0; display: none;}
#box { width: 300px; height: 200px; padding: 10px; background: white; border: 5px solid #333; position: absolute; display: none;}
#close { position: absolute; right: 5px; top: 5px; text-decoration: none; color: black;}
#close:hover {background: #333; color: white;}

HTML代码:

<body style="height:2000px;">
    <input type="button" value="登陆" id="btn" />
    
    <div id="mask"></div>
    <div id="box">
        <a href="javascript:;" id="close">×</a>
        内容显示区域
    </div>
</body>

JS代码:

window.onload = function(){
        var oBtn = document.getElementById(‘btn‘);
        var oMask = document.getElementById(‘mask‘);
        var oBox = document.getElementById(‘box‘);
        var oClose = document.getElementById(‘close‘);

        oClose.onclick = function(){
            oMask.style.display = ‘none‘;
            oBox.style.display = ‘none‘;
        };

        //点击按钮居中显示
        oBtn.onclick = function(){
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            var scrollLeft = document.documentElement.scrollLeft || document.body .scrollLeft;

            oMask.style.display = ‘block‘;
            oMask.style.width = Math.max(document.body.offsetWidth ,document.documentElement.clientWidth) + ‘px‘;
            oMask.style.height = Math.max(document.body.offsetHeight ,document.documentElement.clientHeight) + ‘px‘;

            oBox.style.display = ‘block‘;
            oBox.style.left = (document.documentElement.clientWidth - oBox.offsetWidth)/2 + scrollLeft + ‘px‘;
            oBox.style.top = (document.documentElement.clientHeight - oBox.offsetHeight)/2 + scrollTop + ‘px‘;

        };

        //拖动滚动条居中显示
        window.onscroll = function(){
            if(oBox.style.display == ‘none‘) return;

            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

            oBox.style.left = (document.documentElement.clientWidth - oBox.offsetWidth)/2 + scrollLeft + ‘px‘;
            oBox.style.top = (document.documentElement.clientHeight - oBox.offsetHeight)/2 + scrollTop + ‘px‘;
        };

        //缩放窗口居中显示
        window.onresize = function(){
            if(oMask.style.display == ‘none‘) return;

            oMask.style.left = Math.max(document.body.offsetWidth ,document.documentElement.clientWidth);
            oMask.style.top = Math.max(document.body.offsetHeight,document.documentElement.clientHeight);

            if(oBox.style.display == ‘none‘) return;

            var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

            oBox.style.left = (document.documentElement.clientWidth - oBox.offsetWidth)/2 + scrollLeft + ‘px‘;
            oBox.style.top = (document.documentElement.clientHeight -oBox.offsetHeight)/2 + scrollTop + ‘px‘;
        };

    };

 

JS弹窗——点击/滚动/缩放居中

标签:

原文地址:http://www.cnblogs.com/bokebi520/p/4334485.html

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