/    Sign up×
Community /Pin to ProfileBookmark

Positioning a div relative to screen

I want little ‘help divs’ to appear when people click on icons dotted about a page.

Is there a way of making them appear relative to the screen?
position:absolute makes them appear relative to the left of the page (which is constant) but also relative to the top of the page (which is not constant because it might have been scrolled)

I want the divs to appear next to where the person clicked but I want them half way between the current top and bottom of the page (i.e. the screen).

At the moment I have:
function showhelp(whichdiv,e,divheight)
{
var divleft;
var divtop;
e = e || event;
if (e.clientX > (screen.availWidth/2))
{ divleft = e.clientX – 330; }
else
{ divleft = e.clientX + 30; }
divtop = (screen.availHeight – divheight)/2 + document.body.scrollTop – 50;
document.getElementById(whichdiv).style.display = ‘block’;
document.getElementById(whichdiv).style.left = divleft + ‘px’;
document.getElementById(whichdiv).style.top = divtop + ‘px’;
}

This works but I am having to use ‘scrollTop’ and not sure this will work with all browsers. Is there another way?

Thanks for any help.

to post a comment
CSS

5 Comments(s)

Copy linkTweet thisAlerts:
@JPnycJan 15.2006 — I would grab the mouse coordinates and position the div based on that.
Copy linkTweet thisAlerts:
@WebskaterauthorJan 15.2006 — I would grab the mouse coordinates and position the div based on that.[/QUOTE]

I am no expert but I am under the impression that, to make the div appear where I want it to appear - relative to the page/screen (as opposed to relative to its parent) I have to use position:absolute

If I use position:absolute - detect that clientY is, for example, 600 and set the div's top to be 600 then, if the page has been scrolled a long way, the div will appear off the screen i.e. not visible.
Copy linkTweet thisAlerts:
@JPnycJan 15.2006 — You will be using absolute positioning, you'll simply take the mouse X and Y coords to set the value of that absolute postioning. w3schools.com has a very simple script for grabbing the coords, then you just set them to your top and left values, like you did above.
Copy linkTweet thisAlerts:
@WebskaterauthorJan 16.2006 — Thanks for your reply.

But absolute positioning seems to relate the position of the element to the absolute position of the 'page'. e.g. you click on the screen somewhere near the bottom and client.Y = say 645.

If I pass this to my function to position the top of the div - and the page has been scrolled a long way - the div is off the top of the screen - i.e. cannot be seen without scrolling up. I am adding document.body.scrollTop to add this to client.Y but I am not sure this will work with all browsers. Is there a better way?
×

Success!

Help @Webskater 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.28,
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,
)...