michaelp0730
1/19/2015 - 6:33 PM

John Resig - Get Scrollbar Position

// A function for determining how far horizontally the browser is scrolled
function scrollX() {
	// A shortcut, in case we're using Internet Explorer 6 in Strict Mode
	var de = document.documentElement;

	// If the pageXOffset of the browser is available, use that
	return self.pageXOffset ||

		// Otherwise, try to get the scroll left off of the root node
		( de && de.scrollLeft ) ||

		// Finally, try to get the scroll left off of the body element
		document.body.scrollLeft;
}

// A function for determining how far vertically the browser is scrolled
function scrollY() {
	// A shortcut, in case we're using Internet Explorer 6 in Strict Mode
	var de = document.documentElement;

	// If the pageYOffset of the browser is available, use that
	return self.pageYOffset ||

		// Otherwise, try to get the scroll top off of the root node
		( de && de.scrollTop ) ||

		// Finally, try to get the scroll top off of the body element
		document.body.scrollTop;
}


// If you wanted to scroll the browser up to the top of the browser, you could do:
window.scrollTo(0,0);


// If you wanted to scroll to the position of a specific element, you could do:
window.scrollTo( 0, pageY( document.getElementById("body") ) );