dimensions.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 }, function( defaultExtra, funcName ) {
  9. // Margin is only for outerHeight, outerWidth
  10. jQuery.fn[ funcName ] = function( margin, value ) {
  11. var chainable = arguments.length && ( defaultExtra || typeof margin !== "boolean" ),
  12. extra = defaultExtra || ( margin === true || value === true ? "margin" : "border" );
  13. return access( this, function( elem, type, value ) {
  14. var doc;
  15. if ( jQuery.isWindow( elem ) ) {
  16. // As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
  17. // isn't a whole lot we can do. See pull request at this URL for discussion:
  18. // https://github.com/jquery/jquery/pull/764
  19. return elem.document.documentElement[ "client" + name ];
  20. }
  21. // Get document width or height
  22. if ( elem.nodeType === 9 ) {
  23. doc = elem.documentElement;
  24. // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height],
  25. // whichever is greatest
  26. return Math.max(
  27. elem.body[ "scroll" + name ], doc[ "scroll" + name ],
  28. elem.body[ "offset" + name ], doc[ "offset" + name ],
  29. doc[ "client" + name ]
  30. );
  31. }
  32. return value === undefined ?
  33. // Get width or height on the element, requesting but not forcing parseFloat
  34. jQuery.css( elem, type, extra ) :
  35. // Set width or height on the element
  36. jQuery.style( elem, type, value, extra );
  37. }, type, chainable ? margin : undefined, chainable, null );
  38. };
  39. });
  40. });
  41. return jQuery;
  42. });