Requires

Provides

Contains methods to work with size, scroll, or positioning of Elements and the window object.

License:
MIT-style license.
  1. 22
  2. 23
  3. 24
  4. 25
  5. 26
  6. 27
  7. 28
  8. 29
  9. 30
  10. 31
  11. 32
  12. 33
  13. 34
  14. 35
  15. 36
  16. 37
  17. 38
  18. 39
  19. 40
  20. 41
  21. 42
  22. 43
  23. 44
  24. 45
  25. 46
  26. 47
  27. 48
  28. 49
  29. 50
  30. 51
  31. 52
  32. 53
  33. 54
  34. 55
  35. 56
  36. 57
  37. 58
  38. 59
  39. 60
  40. 61
  41. 62
  42. 63
  43. 64
  44. 65
  45. 66
  46. 67
  47. 68
  48. 69
  49. 70
  50. 71
  51. 72
  52. 73
  53. 74
  54. 75
  55. 76
  56. 77
  57. 78
  58. 79
  59. 80
  60. 81
  61. 82
  62. 83
  63. 84
  64. 85
  65. 86
  66. 87
  67. 88
  68. 89
  69. 90
  70. 91
  71. 92
  72. 93
  73. 94
  74. 95
  75. 96
  76. 97
  77. 98
  78. 99
  79. 100
  80. 101
  81. 102
  82. 103
  83. 104
  84. 105
  85. 106
  86. 107
  87. 108
  88. 109
  89. 110
  90. 111
  91. 112
  92. 113
  93. 114
  94. 115
  95. 116
  96. 117
  97. 118
  98. 119
  99. 120
  100. 121
  101. 122
  102. 123
  103. 124
  104. 125
  105. 126
  106. 127
  107. 128
  108. 129
  109. 130
  110. 131
  111. 132
  112. 133
  113. 134
  114. 135
  115. 136
  116. 137
  117. 138
  118. 139
  119. 140
  120. 141
  121. 142
  122. 143
  123. 144
  124. 145
  125. 146
  126. 147
  127. 148
  128. 149
  129. 150
  130. 151
  131. 152
  132. 153
  133. 154
  134. 155
  135. 156
  136. 157
  137. 158
  138. 159
  139. 160
  140. 161
  141. 162
  142. 163
  143. 164
  144. 165
  145. 166
  146. 167
  147. 168
  148. 169
  149. 170
  150. 171
  151. 172
  152. 173
  153. 174
  154. 175
  155. 176
  156. 177
  157. 178
  158. 179
  159. 180
  160. 181
  161. 182
  162. 183
  163. 184
  164. 185
  165. 186
  166. 187
  167. 188
  168. 189
  169. 190
  170. 191
  171. 192
  172. 193
  173. 194
  174. 195
  175. 196
  176. 197
  177. 198
  178. 199
  179. 200
  180. 201
  181. 202
  182. 203
  183. 204
  184. 205
  185. 206
  186. 207
  187. 208
  188. 209
  189. 210
  190. 211
  191. 212
  192. 213
  193. 214
(function(){ var element = document.createElement('div'), child = document.createElement('div'); element.style.height = '0'; element.appendChild(child); var brokenOffsetParent = (child.offsetParent === element); element = child = null; var isOffset = function(el){ return styleString(el, 'position') != 'static' || isBody(el); }; var isOffsetStatic = function(el){ return isOffset(el) || (/^(?:table|td|th)$/i).test(el.tagName); }; Element.implement({ scrollTo: function(x, y){ if (isBody(this)){ this.getWindow().scrollTo(x, y); } else { this.scrollLeft = x; this.scrollTop = y; } return this; }, getSize: function(){ if (isBody(this)) return this.getWindow().getSize(); return {x: this.offsetWidth, y: this.offsetHeight}; }, getScrollSize: function(){ if (isBody(this)) return this.getWindow().getScrollSize(); return {x: this.scrollWidth, y: this.scrollHeight}; }, getScroll: function(){ if (isBody(this)) return this.getWindow().getScroll(); return {x: this.scrollLeft, y: this.scrollTop}; }, getScrolls: function(){ var element = this.parentNode, position = {x: 0, y: 0}; while (element && !isBody(element)){ position.x += element.scrollLeft; position.y += element.scrollTop; element = element.parentNode; } return position; }, getOffsetParent: brokenOffsetParent ? function(){ var element = this; if (isBody(element) || styleString(element, 'position') == 'fixed') return null; var isOffsetCheck = (styleString(element, 'position') == 'static') ? isOffsetStatic : isOffset; while ((element = element.parentNode)){ if (isOffsetCheck(element)) return element; } return null; } : function(){ var element = this; if (isBody(element) || styleString(element, 'position') == 'fixed') return null; try { return element.offsetParent; } catch(e) {} return null; }, getOffsets: function(){ if (this.getBoundingClientRect && !Browser.Platform.ios){ var bound = this.getBoundingClientRect(), html = document.id(this.getDocument().documentElement), htmlScroll = html.getScroll(), elemScrolls = this.getScrolls(), isFixed = (styleString(this, 'position') == 'fixed'); return { x: bound.left.toInt() + elemScrolls.x + ((isFixed) ? 0 : htmlScroll.x) - html.clientLeft, y: bound.top.toInt() + elemScrolls.y + ((isFixed) ? 0 : htmlScroll.y) - html.clientTop }; } var element = this, position = {x: 0, y: 0}; if (isBody(this)) return position; while (element && !isBody(element)){ position.x += element.offsetLeft; position.y += element.offsetTop; if (Browser.firefox){ if (!borderBox(element)){ position.x += leftBorder(element); position.y += topBorder(element); } var parent = element.parentNode; if (parent && styleString(parent, 'overflow') != 'visible'){ position.x += leftBorder(parent); position.y += topBorder(parent); } } else if (element != this && Browser.safari){ position.x += leftBorder(element); position.y += topBorder(element); } element = element.offsetParent; } if (Browser.firefox && !borderBox(this)){ position.x -= leftBorder(this); position.y -= topBorder(this); } return position; }, getPosition: function(relative){ if (isBody(this)) return {x: 0, y: 0}; var offset = this.getOffsets(), scroll = this.getScrolls(); var position = { x: offset.x - scroll.x, y: offset.y - scroll.y }; if (relative && (relative = document.id(relative))){ var relativePosition = relative.getPosition(); return {x: position.x - relativePosition.x - leftBorder(relative), y: position.y - relativePosition.y - topBorder(relative)}; } return position; }, getCoordinates: function(element){ if (isBody(this)) return this.getWindow().getCoordinates(); var position = this.getPosition(element), size = this.getSize(); var obj = { left: position.x, top: position.y, width: size.x, height: size.y }; obj.right = obj.left + obj.width; obj.bottom = obj.top + obj.height; return obj; }, computePosition: function(obj){ return { left: obj.x - styleNumber(this, 'margin-left'), top: obj.y - styleNumber(this, 'margin-top') }; }, setPosition: function(obj){ return this.setStyles(this.computePosition(obj)); } }); [Document, Window].invoke('implement', { getSize: function(){ var doc = getCompatElement(this); return {x: doc.clientWidth, y: doc.clientHeight}; }, getScroll: function(){ var win = this.getWindow(), doc = getCompatElement(this); return {x: win.pageXOffset || doc.scrollLeft, y: win.pageYOffset || doc.scrollTop}; }, getScrollSize: function(){ var doc = getCompatElement(this), min = this.getSize(), body = this.getDocument().body; return {x: Math.max(doc.scrollWidth, body.scrollWidth, min.x), y: Math.max(doc.scrollHeight, body.scrollHeight, min.y)}; }, getPosition: function(){ return {x: 0, y: 0}; }, getCoordinates: function(){ var size = this.getSize(); return {top: 0, left: 0, bottom: size.y, right: size.x, height: size.y, width: size.x}; } });

private methods

  1. 218
  2. 219
  3. 220
  4. 221
  5. 222
  6. 223
  7. 224
  8. 225
  9. 226
  10. 227
  11. 228
  12. 229
  13. 230
  14. 231
  15. 232
  16. 233
  17. 234
  18. 235
  19. 236
  20. 237
  21. 238
  22. 239
  23. 240
  24. 241
  25. 242
  26. 243
  27. 244
  28. 245
var styleString = Element.getComputedStyle; function styleNumber(element, style){ return styleString(element, style).toInt() || 0; }; function borderBox(element){ return styleString(element, '-moz-box-sizing') == 'border-box'; }; function topBorder(element){ return styleNumber(element, 'border-top-width'); }; function leftBorder(element){ return styleNumber(element, 'border-left-width'); }; function isBody(element){ return (/^(?:body|html)$/i).test(element.tagName); }; function getCompatElement(element){ var doc = element.getDocument(); return (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body; }; })();

aliases

  1. 248
  2. 249
  3. 250
  4. 251
  5. 252
  6. 253
  7. 254
  8. 255
  9. 256
  10. 257
  11. 258
  12. 259
  13. 260
  14. 261
  15. 262
  16. 263
  17. 264
  18. 265
  19. 266
  20. 267
  21. 268
  22. 269
  23. 270
  24. 271
  25. 272
  26. 273
  27. 274
  28. 275
  29. 276
  30. 277
  31. 278
  32. 279
  33. 280
  34. 281
  35. 282
  36. 283
  37. 284
Element.alias({position: 'setPosition'}); //compatability [Window, Document, Element].invoke('implement', { getHeight: function(){ return this.getSize().y; }, getWidth: function(){ return this.getSize().x; }, getScrollTop: function(){ return this.getScroll().y; }, getScrollLeft: function(){ return this.getScroll().x; }, getScrollHeight: function(){ return this.getScrollSize().y; }, getScrollWidth: function(){ return this.getScrollSize().x; }, getTop: function(){ return this.getPosition().y; }, getLeft: function(){ return this.getPosition().x; } });