Skip to content

Instantly share code, notes, and snippets.

@jbail
Created November 29, 2010 21:42
Show Gist options
  • Save jbail/720683 to your computer and use it in GitHub Desktop.
Save jbail/720683 to your computer and use it in GitHub Desktop.
Accessing scroll properties in JavaScript
var square = document.getElementById("square");
var content = document.getElementById("content");
/*
* Vertical y-axis properties
*/
square.offsetHeight; //the height in pixels of the scrollable element (e.g. 150px)
square.scrollTop; //the number of pixels from the top that the element is scrolled (e.g. 350px)
content.scrollHeight; //the total height in pixels of the scrollable content (e.g. 500px)
/*
* Horizontal x-axis properties
*/
square.offsetWidth; //the width in pixels of the scrollable element (e.g. 150px)
square.scrollLeft; //the number of pixels from the left that the element is scrolled (e.g. 350px)
content.scrollWidth; //the total width in pixels of the scrollable content (e.g. 500px)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment