When the web page is larger than the browser viewport (which all but the smallest web pages usually will be for at least some visitors) your visitors will need to scroll the page in the browser in order to see all of the content. The scripts that you have in your page may need to take into account how far the page has been scrolled in order to interact correctly with the page. Unfortunately as with most browser related interactivity there is no standard way of determining the current scroll position.
The following example code creates a function called scrollpos that returns an object with two properties x and y that will contain the distance from the left top corner that the page has been scrolled. We can then call this function at any time to return an object containing the current scroll position at that time.
var pos, txt;
scrollpos = function() {
return {
'x': window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft, 'y': window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop
}};
pos = scrollpos();
if (pos.x > 0) txt = 'scrolled to the right';
