vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
36 lines (35 loc) • 1.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isDOMChildNode_1 = __importDefault(require("./isDOMChildNode"));
var viewport_1 = __importDefault(require("./viewport"));
/**
* Get the parent element that has scrolling
*
* @param elm - The element whose scroll parent is determined
* @return The scroll parent or the viewport
*/
function scrollParent(elm) {
var vp = viewport_1.default(elm);
if (!isDOMChildNode_1.default(elm) || elm === vp) {
return vp;
}
var elmPosition = getComputedStyle(elm).position;
if (elmPosition === 'fixed') {
return vp;
}
var noStaticParent = elmPosition === 'absolute';
var parent = elm.parentElement;
while (parent && parent !== document.body) {
var _a = getComputedStyle(parent), position = _a.position, overflow = _a.overflow, overflowX = _a.overflowX, overflowY = _a.overflowY;
if (!(noStaticParent && position === 'static')
&& /(auto|scroll)/.test(overflow + overflowY + overflowX)) {
return parent;
}
parent = parent.parentElement;
}
return vp;
}
exports.default = scrollParent;