Skip to content

Commit b6fe987

Browse files
author
Joel Steres
committed
Refactor inlined functions to top level per feedback. Fix bottom calc.
Also simplified both bottom and right offset calculations by removing canceled terms.
1 parent d5df67f commit b6fe987

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/utility.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -205,49 +205,52 @@ function countFlags(value) {
205205
return count;
206206
}
207207

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+
208235
/**
209236
* Compute compensating position offsets if body or html element has non-static position attribute.
210237
* @private
211238
* @param {number} windowWidth Window width in pixels.
212239
* @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
214241
*/
215242
function computePositionCompensation(windowWidth, windowHeight) {
216243
// Check if the element is "positioned". A "positioned" element has a CSS
217244
// position value other than static. Whether the element is positioned is
218245
// relevant because absolutely positioned elements are positioned relative
219246
// 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-
};
244247

245248
var offsets;
246249

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);
251254
} else {
252255
// even though body may have offset, no compensation is required
253256
offsets = { top: 0, bottom: 0, left: 0, right: 0 };

0 commit comments

Comments
 (0)