Jump to content

[SOLVED] Center a popup div (horizontally and vertically)


c_shelswell

Recommended Posts

Hi I've got a bit of code that seems to work fine in another site i've made but I can't seem to integrate it to a new site I'm working on. I had initially copied the code so I could have missed something.

 

When a user clicks the link (in this case "test") the popup div should center itself vertically on the page. It seems to be horizontally doing it just fine. The function that should achieve this is "centerPopup". I've pasted all the code below this should be able to be loaded in to a browser fully. Can anyone see where i'm going wrong?

 

Many thanks

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

		<head>
			<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

		<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

			<style type="text/css">
			#popupBackground{
			display:none;
			position:fixed;
			_position:absolute; /* hack for internet explorer 6*/
			height:100%;
			width:100%;
			top:0;
			left:0;
			background:#000000;
			border:1px solid #cecece;
			z-index:1;
		}
		#popupForm{
			display:none;
			position:fixed;
			_position:absolute; /* hack for internet explorer 6*/
			height: 200px;
			width:200px;
			background:#000;
			border:1px solid #555;
			z-index:2;
			padding:5px;
		}
		</style>
	<title>TEST</title>
		</head>
	<body>

<script type="text/javascript">
$(document).ready(function()
{
var popupStatus = 0;

function loadPopup()
{

	if(popupStatus==0)
	{
		$("#popupBackground").css(
		{
			"opacity": "0.5"
		});
		$("#popupBackground").fadeIn("slow");
		$("#popupForm").fadeIn("slow");
		popupStatus = 1;
	}

}

function disablePopup()
{
	if(popupStatus == 1)
	{
		$("#popupBackground").fadeOut("slow");
		$("#popupForm").fadeOut("slow")
		popupStatus = 0;
	}
}

function centrePopup()
{
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;

	var popupHeight = $("#popupForm").height();
	var popupWidth = $("#popupForm").width();

	$("#popupForm").css(
	{
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});

	$("#popupBackground").css({
		"height": windowHeight
	});
}

$(".popupClick").click(function()
{
	$("#popupForm").append("Got it");
	centrePopup();
	loadPopup();
});

$("#closePopup").click(function()
{
	disablePopup();
});

$("#popupBackground").click(function()
{
	disablePopup();
});

$(document).keypress(function(e)
{
	if(e.keyCode==27 && popupStatus==1)
	{
		disablePopup();
	}
});

});


</script>

<div class='title'></div>
<h1>NEWS ITEMS </h1>


<table id="table-1" cellspacing="4" cellpadding="4" border='0' width='100%'>
<tr>
	<td style='border: 1px solid #333;' valign='top' height="100px">
		<a href='#nogo' class="popupClick">Test</a>
	</td>
</tr>
<tr>
	<td style='border: 1px solid #333;' valign='top' height="100px">
		<a href='#nogo' class="popupClick">Test</a>
	</td>
</tr>
<tr>
	<td style='border: 1px solid #333;' valign='top' height="100px">
		<a href='#nogo' class="popupClick">Test</a>
	</td>
</tr>
<tr>
	<td style='border: 1px solid #333;' valign='top' height="100px">
		<a href='#nogo' class="popupClick">Test</a>
	</td>
</tr>
<tr>
	<td style='border: 1px solid #333;' valign='top' height="100px">
		<a href='#nogo' class="popupClick">Test</a>
	</td>
</tr>
<tr>
	<td style='border: 1px solid #333;' valign='top' height="100px">
		<a href='#nogo' class="popupClick">Test</a>
	</td>
</tr>
<tr>
	<td style='border: 1px solid #333;' valign='top' height="100px">
		<a href='#nogo' class="popupClick">Test</a>
	</td>
</tr>
</table>

<div id="popupForm"></div>
<div id='popupBackground'></div>

	</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.