Julien
Julien

Reputation:

JavaScript: scroll position (Webkit engine)

I'm currently trying to use JavaScript to find out how far down the page the user has scrolled; for Firefox 8.0, the keyword is pageYOffset.

To say things mechanically:

  1. The page has a certain height. In Firefox, the useful object is document.documentElement.scrollHeight.

  2. The browser's visible area also has a certain height. In Firefox, the object is window.innerHeight; in IE8, document.documentElement.clientHeight.

  3. I need to know where the user is in the page vertically; in other words, how many pixels down the page the user has scrolled.

Does Webkit have a DOM object that refers to the current scroll position?

Thank you.

Upvotes: 1

Views: 2142

Answers (2)

Latin1
Latin1

Reputation: 93

Ah, there's the IE problem solved.

Also, it turns out that in the case of Chrome and Safari, as well as IE, the scrollbar is brought back to the top for a split second when the page is reloaded.

The window.onload event which I used for testing was called before the bar was brought back to the right position.

Thank you, I was worried about what I was going to do with IE.

Upvotes: 0

Gavin Brock
Gavin Brock

Reputation: 5087

I think all you need is:

var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
var scrolLeft = document.body.scrollLeft || document.documentElement.scrollLeft;

The first half covers webkit and mozilla, the latter for IE.

Upvotes: 1

Related Questions