so_returnScrollDimensions

Summary

Returns the offset of the document for positioning elements when the top left of the browser window is no longer 0,0.

Arguments:

int which
If 0, returns the y offset, otherwise the x offset.

Usage:

if(mObj.offsetTop < so_returnScrollDimensions(1)) {
	mObj.style.top = returnScrollDimensions(1)+"px";
}

Examples:

The function can be used to dynamically position an element in the viewable pane once the user has scrolled. It is used in the Mouseover DOM Inspector.

For example, if you always need the element to be 10 pixels over and 10 pixels down from the browser chrome, regardless of how far the user has scrolled, this will allow it.

Code

function so_returnScrollDimensions(which) {
	var d = document;
	if(which) {
		if(d.body.scrollTop != 0)return d.body.scrollTop;
		if(d.documentElement.scrollTop != 0)return d.documentElement.scrollTop;
	} else {
		if(d.body.scrollLeft != 0)return d.body.scrollTop;
		if(d.documentElement.scrollLeft != 0)return d.documentElement.scrollLeft;
	}
	return 0;
}	

License

This code is issued under a Creative Commons license. You do not need my permission to use it, though I'm always interested in seeing where my code has ended up if you want to drop me a line.

slayeroffice main