...

View Full Version : Screen Size, Scrolling, viewport size



Mikebert4
03-21-2012, 12:13 PM
I've been using these for ages, wanted to share:



/**
* returns the [x,y] scroll-state of the document in px
*/
function getScrollXY() {
var scrOfX = 0, scrOfY = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
//Netscape compliant
scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
//DOM compliant
scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
//IE6 standards compliant mode
scrOfY = document.documentElement.scrollTop;
scrOfX = document.documentElement.scrollLeft;
}
return [ scrOfX, scrOfY ];
}

/**
* Returns [x,y] dimensions of the document in px
*/
function getDocDimensions() {
var D = document;
return [
Math.max(
Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
Math.max(D.body.clientWidth, D.documentElement.clientWidth)
)
,
Math.max(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
)
];
}

/**
* Returns [x,y] viewport size in px
*/
function viewport(){
var e = window, a = 'inner';
if (!( 'innerWidth' in window)){
a = 'client';
e = document.documentElement || document.body;
}
return [e[ a+'Width' ], e[ a+'Height' ]];
}


M



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum