SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to create an overlay to be used under a pop up

I am trying to add a overlay which does a semi block of the background page while the popup in in view. So far this is what I have...

HTML:

a href="#" id="loginButton"><span>Add Item</span><em></em></a>
<div id="loginBox">                
    <form id="loginForm">
        <fieldset id="body">
            <fieldset>
                <label for="item">Item:</label>
                <select id="itemChoice">
                    <option value="Wine" selected>Wine</option>
                    <option value="Shot">Shot</option>
                    <option value="Beer">Beer</option>
                </select>
                <br /><span id="spanPrice" />
            </fieldset>
            <fieldset>
                <label for="quantity">Quantity:</label>
                <input type="text" name="quantity" id="quantity" maxlength="3" />
            </fieldset>
            <fieldset>
                <label for="price">Price:</label>
                <input type="text" name="price" id="price" />
            </fieldset>
            <input type="button" id="add" value="Add to Cart" />
        </fieldset>
    </form>
</div>
<div id="overlay"></div>

CSS:

* Login Button */
#loginButton { 
    display:inline-block;
    float:right;
    background:#d2e0ea url(images/buttonbg.png) repeat-x; 
    border:1px solid #899caa; 
    border-radius:3px;
    -moz-border-radius:3px;
    position:relative;
    z-index:30;
    cursor:pointer;
}

/* Login Button Text */
#loginButton span {
    color:#445058; 
    font-size:14px; 
    font-weight:bold; 
    text-shadow:1px 1px #fff; 
    padding:7px 29px 9px 10px;
    display:block;
}

/* Login Box */
#loginBox {
    position:absolute;
    top:50%;
    right:50%;
    margin-top: -157px;
    margin-right: -131px;
    display:none;
    z-index:29;
}

#overlay {
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: none;
    z-index: 28;
    background: url('overlay_transparent.png') repeat 0 0;
}

/* If the Login Button has been clicked */    
#loginButton.active {
    border-radius:3px 3px 0 0;
}

#loginButton.active span {
    background-position:53px -76px;
}

/* A Line added to overlap the border */
#loginButton.active em {
    position:absolute;
    width:100%;
    height:1px;
    background:#d2e0ea;
    bottom:-1px;
}

/* Login Form */
#loginForm {
    width:248px; 
    border:1px solid #899caa;
    border-radius:3px 0 3px 3px;
    -moz-border-radius:3px 0 3px 3px;
    margin-top:-1px;
    background:#d2e0ea;
    padding:6px;
}

#loginForm fieldset {
    margin:0 0 12px 0;
    display:block;
    border:0;
    padding:0;
}

fieldset#body {
    background:#fff;
    border-radius:3px;
    -moz-border-radius:3px;
    padding:10px 13px;
    margin:0;
}

#loginForm #checkbox {
    width:auto;
    margin:1px 9px 0 0;
    float:left;
    padding:0;
    border:0;
    *margin:-3px 9px 0 0; /* IE7 Fix */
}

#body label {
    color:#3a454d;
    margin:9px 0 0 0;
    display:block;
    float:left;
}

#loginForm #body fieldset label {
    display:block;
    float:none;
    margin:0 0 6px 0;
}

/* Default Input */
#loginForm input {
    width:92%;
    border:1px solid #899caa;
    border-radius:3px;
    -moz-border-radius:3px;
    color:#3a454d;
    font-weight:bold;
    padding:8px 8px;
    box-shadow:inset 0px 1px 3px #bbb;
    -webkit-box-shadow:inset 0px 1px 3px #bbb;
    -moz-box-shadow:inset 0px 1px 3px #bbb;
    font-size:12px;
}

/* Sign In Button */
#loginForm #login {
    width:auto;
    float:left;
    background:#339cdf url(images/loginbuttonbg.png) repeat-x;
    color:#fff;
    padding:7px 10px 8px 10px;
    text-shadow:0px -1px #278db8;
    border:1px solid #339cdf;
    box-shadow:none;
    -moz-box-shadow:none;
    -webkit-box-shadow:none;
    margin:0 12px 0 0;
    cursor:pointer;
    *padding:7px 2px 8px 2px; /* IE7 Fix */
}

/* Forgot your password */
#loginForm span {
    text-align:center;
    display:block;
    padding:7px 0 4px 0;
}

#loginForm span a {
    color:#3a454d;
    text-shadow:1px 1px #fff;
    font-size:12px;
}

input:focus {
    outline:none;
}

JQuery:

var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    button.mouseup(function(login) {
        box.toggle();
        button.toggleClass('active');
        $("#overlay").css('display');
    });
    form.mouseup(function() { 
        return false;
    });
    $(this).mouseup(function(login) {
        if(!($(login.target).parent('#loginButton').length > 0)) {
            button.removeClass('active');
            box.hide();
        }
    });

When I click on the button, the pop up appears but the overlay that is supposed to appear below the pop up is not shown. How can I fix the code so it works as needed?

Upvotes: 2

Views: 598

Answers (1)

Alex Char
Alex Char

Reputation: 33218

Make the following changes to overlay:

css

#overlay {
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: none;
    z-index: 28;
    background: gray;
    position: fixed; /*Add fixed position */
    opacity:0.3; /*Add opacity optional  */
}

js

var button = $('#loginButton');
var box = $('#loginBox');
var form = $('#loginForm');
button.removeAttr('href');
button.mouseup(function(login) {
    box.toggle();
    button.toggleClass('active');
    $("#overlay").css('display','block'); //add block
});
form.mouseup(function() { 
    return false;
});
$(this).mouseup(function(login) {
    if(!($(login.target).parent('#loginButton').length > 0)) {
        button.removeClass('active');
        box.hide();
        $("#overlay").css('display','none'); //hide overlay when close modal
    }
});

fiddle

Upvotes: 2

Related Questions