dimensions.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. define( [
  2. "./core",
  3. "./core/access",
  4. "./css"
  5. ], function( jQuery, access ) {
  6. // Create innerHeight, innerWidth, height, width, outerHeight and outerWidth methods
  7. jQuery.each( { Height: "height", Width: "width" }, function( name, type ) {
  8. jQuery.each( { padding: "inner" + name, content: type, "": "outer" + name },
  9. function( defaultExtra, funcName ) {
  10. // margin is only for outerHeight, outerWidth
  11. jQuery.fn[ funcName ] = function( margin, value ) {
  12. var chainable = arguments.length && ( defaultExtra || typeof margin !== "boolean" ),
  13. extra = defaultExtra || ( margin === true || value === true ? "margin" : "border" );
  14. return access( this, function( elem, type, value ) {
  15. var doc;
  16. if ( jQuery.isWindow( elem ) ) {
  17. // As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
  18. // isn't a whole lot we can do. See pull request at this URL for discussion:
  19. // https://github.com/jquery/jquery/pull/764
  20. return elem.document.documentElement[ "client" + name ];
  21. }
  22. // Get document width or height
  23. if ( elem.nodeType === 9 ) {
  24. doc = elem.documentElement;
  25. // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height],
  26. // whichever is greatest
  27. // unfortunately, this causes bug #3838 in IE6/8 only,
  28. // but there is currently no good, small way to fix it.
  29. return Math.max(
  30. elem.body[ "scroll" + name ], doc[ "scroll" + name ],
  31. elem.body[ "offset" + name ], doc[ "offset" + name ],
  32. doc[ "client" + name ]
  33. );
  34. }
  35. return value === undefined ?
  36. // Get width or height on the element, requesting but not forcing parseFloat
  37. jQuery.css( elem, type, extra ) :
  38. // Set width or height on the element
  39. jQuery.style( elem, type, value, extra );
  40. }, type, chainable ? margin : undefined, chainable, null );
  41. };
  42. } );
  43. } );
  44. return jQuery;
  45. } );