how do I center javascript css popup div, no matter what the screen resolution?

后端 未结 7 1334
一生所求
一生所求 2021-02-07 08:14

I have the following code that opens a new popup window while disabling the background, the problem is that I have to position this so that it\'s 100px from the top (already got

7条回答
  •  花落未央
    2021-02-07 08:54

    What you need is called a light-box.

    To create it you should modify HTML,CSS and JS code.

    Let's say your lightbox consist only of the string "login form". (You can put everything you want there) The HTML code should look like this:

    login form

    Now, we need to hide it with CSS:

    div#loginBox {
        display: none;
    }
    

    Now our box is not visible. Lets modify our box as you want it to be 100px from the top:

    div#loginBox {
        display: none;
        top: 100px;
    }
    

    We will worry about disabling the background later.

    Our next job is to make a button that will display the box when we need it. Easy-peasy:

    login form
    login

    Note that we don't need the "href" attribute, because that will move the screen on clicking and other unwanted behavior.

    Let's attach event handler on the button via JS:

    var IE = document.all ? true : false; // obligatory "browser sniffing"
    
    function display_box() {
        document.getElementsById("loginBox").style.display = "inline-block"; // or "inline" 
    }
    
    window.onload = function() {
        var login_box = document.getElementsById("loginBox");
        if (!IE) {
            login_box.addEventListener( "click", display_box , false );
        }
        else {
            login_box.attachEvent( "onclick", display_box );
        }
    }
    

    But you want it to be in the center of the screen? Then the function goes like this:

    function display_box() {
        var theBox = document.getElementsById("loginBox").style,
            left = document.width / 2 - 50;   // 150 because it is 300 / 2
    
        theBox.display = "inline-block";
        theBox.left = left.toString() + "px";
    }
    

    I would guess that you will want to close the window at some point and make the "disabled background" effect. To do so you can create a div class that extends on the whole screen, attach a "display" event on it, put some z-index in the css to be sure the loginBox is over the "disabled background", and attach a "close the loginBox" event on the "background" div. And now the final code looks like this:

    Note that we care only about the placement of the login-button, because the other are hidden from view, and then modified by JS:

    HTML:

    login
    login

    CSS:

    div#loginBox {
        display: none;
        top: 100px;
        width: 300px; #it is important to know the width of the box, to center it correctly
        z-index: 2;
    }
    div#backgroundDarkener {
        background: #000;
        display: none;
        position: fixed;
        top: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
        z-index: 1;
        opacity: 0.8; 
        # needless to say, you should play with opacity or if you want your
        # css to validate - background image (because I suspect it won't 
        # validate for old versions of IE, Safari, etc.) This is just a suggestion
    }
    

    JS:

    var IE = document.all ? true : false; // obligatory "browser sniffing"
    
    function display_box() {
        var theBox = document.getElementsById("loginBox").style,
            background = document.getElementsById("loginBox").style,
            left = document.width / 2 - 150;   // 150 is 300 / 2
    
        theBox.display = "inline-block";
        theBox.left = left.toString() + "px";
        background.display = "inline-block";
    }
    function hide_box() {
        document.getElementsById("loginBox").style.display = "none";
        document.getElementsById("backgroundDarkener").style.display = "none"; 
    }
    
    window.onload = function() {
        var login_box = document.getElementsById("loginBox"),
            background = document.getElementsById("backgroundDarkener");
    
        if (!IE) {
            login_box.addEventListener( "click", display_box , false );
            background.addEventListener( "click", hide_box , false );
        }
        else {
            login_box.attachEvent( "onclick", display_box );
            background.attachEvent( "onclick", hide_box );
        }
    }
    

提交回复
热议问题