Welcome to WebmasterWorld Guest from 54.234.38.8

Forum Moderators: open

Message Too Old, No Replies

Find Current Browser Window Position

The y value for the top of current user position in browser window

     
6:39 pm on Aug 6, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Jan 5, 2004
posts:202
votes: 0


I have a div that is hidden until the user clicks on one of the links to display it through a javascript function to toggle the css display value.

Right now it will display at the top left corner which is fine on shorter pages where the user doesn't scroll down.

But if the user has scrolled down the page several times, and then clicks on the link, it will display the div way at the top of the page where it can't be seen (because the user has scrolled down so far).

Is there any javascript that allows you to extract the current y value for the content that is currently being displayed at the top of the browser window?

So, if the user had scrolled down 1500px from the top of the page, then I could get this y value of 1500 so I could then set things up to place the div right at the top based on where the user is currently viewing the page?

Or is there a way to get a div to always display at the top of the browser window currently being viewed regardless of how far down the user has scrolled?

7:18 pm on Aug 6, 2007 (gmt 0)

Full Member

5+ Year Member

joined:July 13, 2007
posts:235
votes: 0


Try:

.pageTop
{
position: fixed;
top: 0px;
left: 0px;
}

<div class="pageTop">
I'm stuck to the top of the page!
</div>

8:48 pm on Aug 7, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Jan 5, 2004
posts:202
votes: 0


Sorry, I didn't explain it well enough. I want the div to show up at the location where the user has scrolled down to. I found the following to get the value for how far down the user has scrolled:

Microsoft Internet Explorer:
document.body.scrollTop

Other Browsers:
window.pageYOffset

8:53 pm on Aug 7, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:662
votes: 0


That's what position:fixed does.
5:38 pm on Aug 9, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Jan 5, 2004
posts:202
votes: 0


Thanks. I see now that it does work for most browsers but IE6. So I have to put together something using document.body.scrollTop with javascript specifically for IE6 to get around this. Thanks very much for your help.