/    Sign up×
Community /Pin to ProfileBookmark

JQuery modal window positioning

Does anyone know how I can set the position of a jquery modal window? Right now, it works perfectly but only displays in the center of the browser window. I want to be able to just position it a set amount from the left margin. I can do it with the close div, but not the modal window div. I’m sure it’s something simple that I am an idiot and missing. Any help would be appreciated!

the styles

#mask{
position:absolute; /* important */
top:0px; /* start from top */
left:0px; /* start from left */
height:100%; /* cover the whole page */
width:100%; /* cover the whole page */
display:none; /* don’t show it ‘*/

/* styling bellow */
background-color: white;
}

.modal_window{
position:absolute; /* important so we can position it on center later */
display:none; /* don’t show it */
z-index:99;
width:900px; height:830px;
/* styling bellow */
color:#000;
background-color:#fff;
margin-left:10px;
}

/* style a specific modal window */
#modal_window{
padding:50px;
border:1px solid gray;
background: #246493;
color:black;
}

and the script

$(document).ready(function(){

//get the height and width of the page
var window_width = $(window).width();
var window_height = $(window).height();

//vertical and horizontal centering of modal window(s)
/*we will use each function so if we have more then 1
modal window we center them all*/
$(‘.modal_window’).each(function(){

//get the height and width of the modal
var modal_height = $(this).outerHeight();
var modal_width = $(this).outerWidth();

//calculate top and left offset needed for centering
var top = (window_height-modal_height)/1.3;
var left = (window_width-modal_width)/3;

//apply new top and left css values
$(this).css({‘top’ : top , ‘left’ : left});

});

$(‘.activate_modal’).click(function(){

//get the id of the modal window stored in the name of the activating element
var modal_id = $(this).attr(‘name’);

//use the function to show it
show_modal(modal_id);

});

$(‘.close_modal’).click(function(){

//use the function to close it
close_modal();

});

});

//THE FUNCTIONS

function close_modal(){

//hide the mask
$(‘#mask’).fadeOut(500);

//hide modal window(s)
$(‘.modal_window’).fadeOut(500);

}
function show_modal(modal_id){

//set display to block and opacity to 0 so we can use fadeTo
$(‘#mask’).css({ ‘display’ : ‘block’, opacity : 0});

//fade in the mask to opacity 0.8
$(‘#mask’).fadeTo(500,0.8);

//show the modal window
$(‘#’+modal_id).fadeIn(500);

}

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Alex1969 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...