Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Created December 27, 2012 15:05
Show Gist options
  • Save rainyjune/4388960 to your computer and use it in GitHub Desktop.
Save rainyjune/4388960 to your computer and use it in GitHub Desktop.
Querying the scrollbar positions of a window
// Return the current scrollbar offsets as the x and y properties of an object
function getScrollOffsets(w) {
// Use the specified window or the current window if no argument
w = w || window;
// This works for all browsers except IE versions 8 and before
if (w.pageXOffset != null) return {x: w.pageXOffset, y:w.pageYOffset};
// For IE (or any browser) in Standards mode
var d = w.document;
if (document.compatMode == "CSS1Compat") return {x:d.documentElement.scrollLeft, y:d.documentElement.scrollTop};
// For browsers in Quirks mode
return { x: d.body.scrollLeft, y: d.body.scrollTop };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment