width, height, scrollLeft, and scrollTop now work with windows and documents other than just the one it was loaded in (like iframes and popups)

This commit is contained in:
Brandon Aaron 2009-04-22 00:55:44 +00:00
parent a0d079f430
commit 2adb9b2a0f
2 changed files with 27 additions and 17 deletions

View file

@ -19,24 +19,26 @@ jQuery.each([ "Height", "Width" ], function(i, name){
jQuery.fn[ type ] = function( size ) { jQuery.fn[ type ] = function( size ) {
// Get window width or height // Get window width or height
return this[0] == window ? var elem = this[0];
if ( !elem ) return null;
return ("scrollTo" in elem && elem.document) ? // does it walk and quack like a window?
// Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
document.compatMode == "CSS1Compat" && document.documentElement[ "client" + name ] || elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
document.body[ "client" + name ] : elem.document.body[ "client" + name ] :
// Get document width or height // Get document width or height
this[0] == document ? (elem.nodeName === "#document") ? // is it a document
// Either scroll[Width/Height] or offset[Width/Height], whichever is greater // Either scroll[Width/Height] or offset[Width/Height], whichever is greater
Math.max( Math.max(
document.documentElement["client" + name], elem.documentElement["client" + name],
document.body["scroll" + name], document.documentElement["scroll" + name], elem.body["scroll" + name], elem.documentElement["scroll" + name],
document.body["offset" + name], document.documentElement["offset" + name] elem.body["offset" + name], elem.documentElement["offset" + name]
) : ) :
// Get or set width or height on the element // Get or set width or height on the element
size === undefined ? size === undefined ?
// Get width or height on the element // Get width or height on the element
(this.length ? jQuery.css( this[0], type ) : null) : jQuery.css( elem, type ) :
// Set the width or height on the element (default to pixels if value is unitless) // Set the width or height on the element (default to pixels if value is unitless)
this.css( type, typeof size === "string" ? size : size + "px" ); this.css( type, typeof size === "string" ? size : size + "px" );

View file

@ -133,24 +133,32 @@ jQuery.each( ['Left', 'Top'], function(i, name) {
jQuery.fn[ method ] = function(val) { jQuery.fn[ method ] = function(val) {
if ( !this[0] ) return null; if ( !this[0] ) return null;
var elem = this[0], win = ("scrollTo" in elem && elem.document) ? elem :
(elem.nodeName === "#document") ? elem.defaultView || elem.parentWindow :
false;
return val !== undefined ? return val !== undefined ?
// Set the scroll offset // Set the scroll offset
this.each(function() { this.each(function() {
this == window || this == document ? win = ("scrollTo" in this && this.document) ? this :
window.scrollTo( (this.nodeName === "#document") ? this.defaultView || this.parentWindow :
!i ? val : jQuery(window).scrollLeft(), false;
i ? val : jQuery(window).scrollTop()
win ?
win.scrollTo(
!i ? val : jQuery(win).scrollLeft(),
i ? val : jQuery(win).scrollTop()
) : ) :
this[ method ] = val; this[ method ] = val;
}) : }) :
// Return the scroll offset // Return the scroll offset
this[0] == window || this[0] == document ? win ?
self[ i ? 'pageYOffset' : 'pageXOffset' ] || win[ i ? 'pageYOffset' : 'pageXOffset' ] ||
jQuery.support.boxModel && document.documentElement[ method ] || jQuery.support.boxModel && win.document.documentElement[ method ] ||
document.body[ method ] : win.document.body[ method ] :
this[0][ method ]; elem[ method ];
}; };
}); });