Skip to content

Commit 2d41b1e

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 5bf6a63 commit 2d41b1e

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
@@ -206,49 +206,52 @@ function countFlags(value) {
206206
return count;
207207
}
208208

209+
/**
210+
* Check whether element has CSS position attribute other than static
211+
* @private
212+
* @param {jQuery} element Element to check
213+
* @return {boolean} indicating whether position attribute is non-static.
214+
*/
215+
function isPositionNotStatic(element) {
216+
return element.css('position') !== 'static';
217+
}
218+
219+
/**
220+
* Get element offsets
221+
* @private
222+
* @param {jQuery} el Element to check
223+
* @param {number} windowWidth Window width in pixels.
224+
* @param {number} windowHeight Window height in pixels.
225+
* @return {Object} The top, left, right, bottom offset in pixels
226+
*/
227+
function getElementOffsets(el, windowWidth, windowHeight) {
228+
// jquery offset returns top and left relative to document in pixels.
229+
var offsets = el.offset();
230+
// right and bottom offset relative to window width/height
231+
offsets.right = windowWidth - el.outerWidth() - offsets.left;
232+
offsets.bottom = windowHeight - el.outerHeight() - offsets.top;
233+
return offsets;
234+
}
235+
209236
/**
210237
* Compute compensating position offsets if body or html element has non-static position attribute.
211238
* @private
212239
* @param {number} windowWidth Window width in pixels.
213240
* @param {number} windowHeight Window height in pixels.
214-
* @return {Offsets} The top, left, right, bottom offset in pixels
241+
* @return {Object} The top, left, right, bottom offset in pixels
215242
*/
216243
function computePositionCompensation(windowWidth, windowHeight) {
217244
// Check if the element is "positioned". A "positioned" element has a CSS
218245
// position value other than static. Whether the element is positioned is
219246
// relevant because absolutely positioned elements are positioned relative
220247
// to the first positioned ancestor rather than relative to the doc origin.
221-
var isPositioned = function(el) {
222-
return el.css('position') !== 'static';
223-
};
224-
225-
var getElementOffsets = function(el) {
226-
var elWidthWithMargin,
227-
elHeightWithMargin,
228-
elPositionPx,
229-
offsets;
230-
// jquery offset and position functions return top and left
231-
// offset function computes position + margin
232-
offsets = el.offset();
233-
elPositionPx = el.position();
234-
235-
// Compute the far margins based off the outerWidth values.
236-
elWidthWithMargin = el.outerWidth(true);
237-
elHeightWithMargin = el.outerHeight(true);
238-
239-
// right offset = right margin + body right position
240-
offsets.right = (elWidthWithMargin - el.outerWidth() - (offsets.left - elPositionPx.left)) + (windowWidth - elWidthWithMargin - elPositionPx.left);
241-
// bottom offset = bottom margin + body bottom position
242-
offsets.bottom = (elHeightWithMargin - el.outerHeight() - offsets.top) + (windowHeight - elHeightWithMargin - elPositionPx.top);
243-
return offsets;
244-
};
245248

246249
var offsets;
247250

248-
if (isPositioned($body)) {
249-
offsets = getElementOffsets($body);
250-
} else if (isPositioned($html)) {
251-
offsets = getElementOffsets($html);
251+
if (isPositionNotStatic($body)) {
252+
offsets = getElementOffsets($body, windowWidth, windowHeight);
253+
} else if (isPositionNotStatic($html)) {
254+
offsets = getElementOffsets($html, windowWidth, windowHeight);
252255
} else {
253256
// even though body may have offset, no compensation is required
254257
offsets = { top: 0, bottom: 0, left: 0, right: 0 };

0 commit comments

Comments
 (0)