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


    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';
            };
    
        };
  • 相关阅读:
    BZOJ 1221 [HNOI2001] 软件开发 费用流_建模
    BZOJ 1180 / 2843 LCT模板题 + 双倍经验
    bzoj 4372: 烁烁的游戏 动态点分治+树链剖分+线段树
    bzoj 3730: 震波 动态点分治+树链剖分+线段树
    luogu P2634 [国家集训队]聪聪可可 点分治
    bzoj 1468: Tree 点分治
    bzoj 1296: [SCOI2009]粉刷匠 动态规划
    bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化
    BZOJ 1042: [HAOI2008]硬币购物 容斥原理+背包
    matplotlib模块
  • 原文地址:https://www.cnblogs.com/bokebi520/p/4334485.html
Copyright © 2020-2023  润新知