Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

jQuery: Position from top of window

How can I position my element from the top of the window instead of the top of its parent?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Demo</title>

<style type="text/css">

#xyz {
position: relative;
top: 100px;
left: 100px;
width: 300px;
height: 300px;
border: 1px solid red;
padding: 20px;
text-align: center;
}

#xyz h2 {
position: absolute;
background: #ffeeee;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

</head>
<body>

<div id="xyz">

<h2>Hello</h1>

Hi There!

</div>

<script type="text/javascript">
/* <![CDATA[ */

$('#xyz h2').css('top','10px');
alert('I want the h2 to be positioned 10px from the top of the window, not 10px from the top of its parent.'); 

/* ]]> */
</script>

</body>
</html>

Open in new window

Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Take out the

top: 100px;
left: 100px;

in your #xyz style
I think you want

position: absolute;

instead of

position: relative;



Avatar of hankknight

ASKER

Thanks, but I think you both misunderstood my question.  

I cannot change anything about #xyz

I want to control the top position of $("#xyz h2" ) without making any changes to its parent.
Avatar of David S.
The "position:relative" declaration given to "#xyz" creates a new positioning context. If you can't remove that declaration, then you would need to give a negative value to the top property of "#xyz h2", e.g. "-90px".
OK, then how can I find out what negative number to use to get my element to 10px from the top of the window?
SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried using offset but I must be doing something wrong.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Demo</title>

<style type="text/css">

#xyz {
position: relative;
top: 100px;
left: 100px;
width: 300px;
height: 300px;
border: 1px solid red;
padding: 20px;
text-align: center;
}

#xyz h2 {
position: absolute;
background: #ffeeee;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

</head>
<body>

<div id="xyz">

<h2>Hello</h1>

Hi There!

</div>

<script type="text/javascript">
/* <![CDATA[ */

$('#xyz h2').css('top', (0 - $(window).offset.top + 10) + 'px' );
alert( (0 - $(window).offset.top + 10) + 'px'); 

/* ]]> */
</script>

</body>
</html>

Open in new window

Change

$(window).offset.top

to

$('#xyz h2').offset().top
Thanks.  Now it positions the h2 slightly above the window.  I want it 10px below the top of the window.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Demo</title>

<style type="text/css">

#xyz {
position: relative;
top: 100px;
left: 100px;
width: 300px;
height: 300px;
border: 1px solid red;
padding: 20px;
text-align: center;
}

#xyz h2 {
position: absolute;
background: #ffeeee;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

</head>
<body>

<div id="xyz">

<h2>Hello</h1>

Hi There!

</div>

<script type="text/javascript">
/* <![CDATA[ */

$('#xyz h2').css('top', (0 - $('#xyz h2').offset().top + 10) + 'px' );

/* ]]> */
</script>

</body>
</html>

Open in new window

what about :
var topParent = $("#xyz").css("top").replace(/\D/g,"");
	$('#xyz h2').css({ "top": (-topParent + 10 ) + "px" });

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the points!