[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

Show top/left div corner in the middle of the browser screen?

I'm trying to show a "div" element to the user and i want it to show in the center of is browser screen.

I want to position the div top/left corner at the top/left/center of the user screen..

Anybody can help?
Thanks..
0
jabcoco
Asked:
jabcoco
1 Solution
 
Repus024Commented:
Hello jabcoco,

Please look at the attached code snippet.  This should be a simple example of what you are trying to accomplish.  This takes the div and places it in the center of the user's browser window.

Please note that the div has "position:absolute" specified.  This is what allows it to be positioned anywhere dynamically.

Please let me know if you have trouble with this script or if it does not do what you are trying to accomplish.

Hope this helps!
<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript">
function toggleDiv()
{
	var div = document.getElementById("floatDiv");
	if (div.style.display == "none")
	{
		div.style.display = "inline";
		var IE=document.all;
		var iXPos;
		var iYPos;
		if (!IE)
		{
			iXPos = self.innerWidth / 2;
			iYPos = self.innerHeight / 2;
			alert("self.innerWidth: " + self.innerWidth
			+ "\n self.innerHeight: " + self.innerHeight
			+ "\n iXPos: " + iXPos + "\n iYPos: " + iYPos);
		}
		else
		{
			iXPos = (document.body.clientWidth) / 2;
			iYPos = (document.body.clientHeight) / 2;
			alert("document.body.clientWidth: " + document.body.clientWidth
			+ "\n document.body.clientHeight: " + document.body.clientHeight
			+ "\n iXPos: " + iXPos + "\n iYPos: " + iYPos);
		}
		div.style.left=iXPos;
  		div.style.top=iYPos;
	}
	else
	{
		div.style.display = "none";
	}
}
</script>
</HEAD>
 
<BODY>
<form>
Form Field One: <input name="ff1" type="text"/>
<br/>
Form Field Two: <input name="ff2" type="text"/>
<br/><input type="button" value="Toggle Div" onclick="toggleDiv()"/>
<div id="floatDiv" style="position:absolute;display:none">
Floating Div
</div>
</form>
</BODY>
 
</HTML>

Open in new window

0
 
jabcocoAuthor Commented:
Work's perfectly!
Exactly what I'm looking for! :)

Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now