rubber.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Rubber - Very Very Simple Popup Resize Function
  3. */
  4. (function(){
  5. var targetWindow;
  6. try {
  7. targetWindow = top;
  8. } catch(e) {
  9. targetWindow = _WIN;
  10. }
  11. var getScrollBarSize = function() {
  12. var scrollDiv = _DOC.createElement('div');
  13. scrollDiv.style.width = '100px';
  14. scrollDiv.style.height = '100px';
  15. scrollDiv.style.overflow = 'scroll';
  16. scrollDiv.style.position = 'absolute';
  17. scrollDiv.style.top = '-9999px';
  18. _DOC.body.appendChild(scrollDiv);
  19. var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
  20. var scrollbarHeight = scrollDiv.offsetHeight - scrollDiv.clientHeight;
  21. _DOC.body.removeChild(scrollDiv);
  22. return {
  23. width: scrollbarWidth,
  24. height: scrollbarHeight
  25. }
  26. };
  27. var getScreenMargin = function() {
  28. if(!$tx.msie) {
  29. return {left: 0, top: 0};
  30. }
  31. var prevLeft = (_WIN.screenLeft) ? _WIN.screenLeft : _WIN.screenX;
  32. var prevTop = (_WIN.screenTop) ? _WIN.screenTop : _WIN.screenY;
  33. targetWindow.moveTo(0, 0);
  34. var marginLeft = (_WIN.screenLeft) ? _WIN.screenLeft : _WIN.screenX;
  35. var marginTop = (_WIN.screenTop) ? _WIN.screenTop : _WIN.screenY;
  36. targetWindow.moveTo(prevLeft - marginLeft, prevTop - marginTop);
  37. return {
  38. left: marginLeft,
  39. top: marginTop
  40. }
  41. };
  42. var Rubber = function() {
  43. var _docEl = _DOC.documentElement;
  44. var _screenHeight = _WIN.screen.availHeight;
  45. var _screenWidth = _WIN.screen.availWidth;
  46. var _scrollbarSize = getScrollBarSize();
  47. var _screenMargin = getScreenMargin();
  48. this.resize = function(wrapper) {
  49. if ($tx.msie) {
  50. _DOC.body.scroll = "no";
  51. }
  52. var popLeft = (_WIN.screenLeft) ? _WIN.screenLeft : _WIN.screenX;
  53. var popTop = (_WIN.screenTop) ? _WIN.screenTop : _WIN.screenY;
  54. var deltaHeight = 0, deltaWidth = 0;
  55. //content size
  56. if (targetWindow.outerHeight === 0) {
  57. setTimeout(function () {
  58. _rubber.resize(wrapper);
  59. }, 100);
  60. return;
  61. }
  62. else if (targetWindow.outerHeight) {
  63. deltaWidth = targetWindow.outerWidth - targetWindow.innerWidth;
  64. deltaHeight = targetWindow.outerHeight - targetWindow.innerHeight;
  65. }
  66. else if(_docEl.clientWidth) {
  67. var fakeOuterWidth = _docEl.clientWidth;
  68. var fakeOuterHeight = _docEl.clientHeight;
  69. targetWindow.resizeTo(fakeOuterWidth, fakeOuterHeight);
  70. var fakeInnerWidth = _docEl.clientWidth;
  71. var fakeInnerHeight = _docEl.clientHeight;
  72. deltaWidth = fakeOuterWidth - fakeInnerWidth;
  73. deltaHeight = fakeOuterHeight - fakeInnerHeight;
  74. }
  75. else {
  76. throw 'browser does not support';
  77. }
  78. var contentWidth = wrapper.clientWidth + deltaWidth;
  79. var contentHeight = wrapper.clientHeight + deltaHeight;
  80. //scrollbar
  81. if (contentWidth > _screenWidth) {
  82. if ($tx.msie) {
  83. _DOC.body.scroll = "yes";
  84. }
  85. contentWidth = _screenWidth;
  86. contentHeight += _scrollbarSize.height;
  87. }
  88. if(contentHeight > _screenHeight) {
  89. if ($tx.msie) {
  90. _DOC.body.scroll = "yes";
  91. }
  92. contentHeight = _screenHeight;
  93. contentWidth += _scrollbarSize.width;
  94. if (contentWidth > _screenWidth) {
  95. contentWidth = _screenWidth;
  96. }
  97. }
  98. //position
  99. if (contentWidth + popLeft > _screenWidth) {
  100. popLeft = 0;
  101. }
  102. if (contentHeight + popTop > _screenHeight) {
  103. popTop = 0;
  104. }
  105. targetWindow.moveTo(popLeft - _screenMargin.left, popTop - _screenMargin.top);
  106. targetWindow.resizeTo(contentWidth, contentHeight);
  107. };
  108. };
  109. var _rubber;
  110. _WIN.resizeHeight = function(width, wrapper) {
  111. if(!_rubber) {
  112. _rubber = new Rubber(0);
  113. }
  114. _rubber.resize(wrapper);
  115. };
  116. })();