@@ -205,49 +205,52 @@ function countFlags(value) {
205
205
return count ;
206
206
}
207
207
208
+ /**
209
+ * Check whether element has CSS position attribute other than static
210
+ * @private
211
+ * @param {jQuery } element Element to check
212
+ * @return {boolean } indicating whether position attribute is non-static.
213
+ */
214
+ function isPositionNotStatic ( element ) {
215
+ return element . css ( 'position' ) !== 'static' ;
216
+ }
217
+
218
+ /**
219
+ * Get element offsets
220
+ * @private
221
+ * @param {jQuery } el Element to check
222
+ * @param {number } windowWidth Window width in pixels.
223
+ * @param {number } windowHeight Window height in pixels.
224
+ * @return {Object } The top, left, right, bottom offset in pixels
225
+ */
226
+ function getElementOffsets ( el , windowWidth , windowHeight ) {
227
+ // jquery offset returns top and left relative to document in pixels.
228
+ var offsets = el . offset ( ) ;
229
+ // right and bottom offset relative to window width/height
230
+ offsets . right = windowWidth - el . outerWidth ( ) - offsets . left ;
231
+ offsets . bottom = windowHeight - el . outerHeight ( ) - offsets . top ;
232
+ return offsets ;
233
+ }
234
+
208
235
/**
209
236
* Compute compensating position offsets if body or html element has non-static position attribute.
210
237
* @private
211
238
* @param {number } windowWidth Window width in pixels.
212
239
* @param {number } windowHeight Window height in pixels.
213
- * @return {Offsets } The top, left, right, bottom offset in pixels
240
+ * @return {Object } The top, left, right, bottom offset in pixels
214
241
*/
215
242
function computePositionCompensation ( windowWidth , windowHeight ) {
216
243
// Check if the element is "positioned". A "positioned" element has a CSS
217
244
// position value other than static. Whether the element is positioned is
218
245
// relevant because absolutely positioned elements are positioned relative
219
246
// to the first positioned ancestor rather than relative to the doc origin.
220
- var isPositioned = function ( el ) {
221
- return el . css ( 'position' ) !== 'static' ;
222
- } ;
223
-
224
- var getElementOffsets = function ( el ) {
225
- var elWidthWithMargin ,
226
- elHeightWithMargin ,
227
- elPositionPx ,
228
- offsets ;
229
- // jquery offset and position functions return top and left
230
- // offset function computes position + margin
231
- offsets = el . offset ( ) ;
232
- elPositionPx = el . position ( ) ;
233
-
234
- // Compute the far margins based off the outerWidth values.
235
- elWidthWithMargin = el . outerWidth ( true ) ;
236
- elHeightWithMargin = el . outerHeight ( true ) ;
237
-
238
- // right offset = right margin + body right position
239
- offsets . right = ( elWidthWithMargin - el . outerWidth ( ) - ( offsets . left - elPositionPx . left ) ) + ( windowWidth - elWidthWithMargin - elPositionPx . left ) ;
240
- // bottom offset = bottom margin + body bottom position
241
- offsets . bottom = ( elHeightWithMargin - el . outerHeight ( ) - offsets . top ) + ( windowHeight - elHeightWithMargin - elPositionPx . top ) ;
242
- return offsets ;
243
- } ;
244
247
245
248
var offsets ;
246
249
247
- if ( isPositioned ( $body ) ) {
248
- offsets = getElementOffsets ( $body ) ;
249
- } else if ( isPositioned ( $html ) ) {
250
- offsets = getElementOffsets ( $html ) ;
250
+ if ( isPositionNotStatic ( $body ) ) {
251
+ offsets = getElementOffsets ( $body , windowWidth , windowHeight ) ;
252
+ } else if ( isPositionNotStatic ( $html ) ) {
253
+ offsets = getElementOffsets ( $html , windowWidth , windowHeight ) ;
251
254
} else {
252
255
// even though body may have offset, no compensation is required
253
256
offsets = { top : 0 , bottom : 0 , left : 0 , right : 0 } ;
0 commit comments