dimensions.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. define( [
  2. "./core",
  3. "./core/access",
  4. "./css"
  5. ], function( jQuery, access ) {
  6. "use strict";
  7. // Create innerHeight, innerWidth, height, width, outerHeight and outerWidth methods
  8. jQuery.each( { Height: "height", Width: "width" }, function( name, type ) {
  9. jQuery.each( { padding: "inner" + name, content: type, "": "outer" + name },
  10. function( defaultExtra, funcName ) {
  11. // Margin is only for outerHeight, outerWidth
  12. jQuery.fn[ funcName ] = function( margin, value ) {
  13. var chainable = arguments.length && ( defaultExtra || typeof margin !== "boolean" ),
  14. extra = defaultExtra || ( margin === true || value === true ? "margin" : "border" );
  15. return access( this, function( elem, type, value ) {
  16. var doc;
  17. if ( jQuery.isWindow( elem ) ) {
  18. // $( window ).outerWidth/Height return w/h including scrollbars (gh-1729)
  19. return funcName.indexOf( "outer" ) === 0 ?
  20. elem[ "inner" + name ] :
  21. elem.document.documentElement[ "client" + name ];
  22. }
  23. // Get document width or height
  24. if ( elem.nodeType === 9 ) {
  25. doc = elem.documentElement;
  26. // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height],
  27. // whichever is greatest
  28. return Math.max(
  29. elem.body[ "scroll" + name ], doc[ "scroll" + name ],
  30. elem.body[ "offset" + name ], doc[ "offset" + name ],
  31. doc[ "client" + name ]
  32. );
  33. }
  34. return value === undefined ?
  35. // Get width or height on the element, requesting but not forcing parseFloat
  36. jQuery.css( elem, type, extra ) :
  37. // Set width or height on the element
  38. jQuery.style( elem, type, value, extra );
  39. }, type, chainable ? margin : undefined, chainable );
  40. };
  41. } );
  42. } );
  43. return jQuery;
  44. } );