jquery.dimensions.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // TODO: check if it is really need, and update to the latest version at least
  2. /* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com) and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  3. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  4. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  5. *
  6. * $LastChangedDate: 2007-12-20 15:43:48 +0100 (Do, 20 Dez 2007) $
  7. * $Rev: 4257 $
  8. *
  9. * Version: @VERSION
  10. *
  11. * Requires: jQuery 1.2+
  12. */
  13. (function($){
  14. $.dimensions = {
  15. version: '@VERSION'
  16. };
  17. // Create innerHeight, innerWidth, outerHeight and outerWidth methods
  18. $.each( [ 'Height', 'Width' ], function(i, name){
  19. // innerHeight and innerWidth
  20. $.fn[ 'inner' + name ] = function() {
  21. if (!this[0]) return;
  22. var torl = name == 'Height' ? 'Top' : 'Left', // top or left
  23. borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
  24. return this.is(':visible') ? this[0]['client' + name] : num( this, name.toLowerCase() ) + num(this, 'padding' + torl) + num(this, 'padding' + borr);
  25. };
  26. // outerHeight and outerWidth
  27. $.fn[ 'outer' + name ] = function(options) {
  28. if (!this[0]) return;
  29. var torl = name == 'Height' ? 'Top' : 'Left', // top or left
  30. borr = name == 'Height' ? 'Bottom' : 'Right'; // bottom or right
  31. options = $.extend({ margin: false }, options || {});
  32. var val = this.is(':visible') ?
  33. this[0]['offset' + name] :
  34. num( this, name.toLowerCase() )
  35. + num(this, 'border' + torl + 'Width') + num(this, 'border' + borr + 'Width')
  36. + num(this, 'padding' + torl) + num(this, 'padding' + borr);
  37. return val + (options.margin ? (num(this, 'margin' + torl) + num(this, 'margin' + borr)) : 0);
  38. };
  39. });
  40. // Create scrollLeft and scrollTop methods
  41. $.each( ['Left', 'Top'], function(i, name) {
  42. $.fn[ 'scroll' + name ] = function(val) {
  43. if (!this[0]) return;
  44. return val != undefined ?
  45. // Set the scroll offset
  46. this.each(function() {
  47. this == window || this == document ?
  48. window.scrollTo(
  49. name == 'Left' ? val : $(window)[ 'scrollLeft' ](),
  50. name == 'Top' ? val : $(window)[ 'scrollTop' ]()
  51. ) :
  52. this[ 'scroll' + name ] = val;
  53. }) :
  54. // Return the scroll offset
  55. this[0] == window || this[0] == document ?
  56. self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
  57. $.boxModel && document.documentElement[ 'scroll' + name ] ||
  58. document.body[ 'scroll' + name ] :
  59. this[0][ 'scroll' + name ];
  60. };
  61. });
  62. $.fn.extend({
  63. position: function() {
  64. var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
  65. if (elem) {
  66. // Get *real* offsetParent
  67. offsetParent = this.offsetParent();
  68. // Get correct offsets
  69. offset = this.offset();
  70. parentOffset = offsetParent.offset();
  71. // Subtract element margins
  72. offset.top -= num(elem, 'marginTop');
  73. offset.left -= num(elem, 'marginLeft');
  74. // Add offsetParent borders
  75. parentOffset.top += num(offsetParent, 'borderTopWidth');
  76. parentOffset.left += num(offsetParent, 'borderLeftWidth');
  77. // Subtract the two offsets
  78. results = {
  79. top: offset.top - parentOffset.top,
  80. left: offset.left - parentOffset.left
  81. };
  82. }
  83. return results;
  84. },
  85. offsetParent: function() {
  86. var offsetParent = this[0].offsetParent;
  87. while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && $.css(offsetParent, 'position') == 'static') )
  88. offsetParent = offsetParent.offsetParent;
  89. return $(offsetParent);
  90. }
  91. });
  92. function num(el, prop) {
  93. return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;
  94. };
  95. })(jQuery);