getScrollParent.mjs 1.0 KB

1234567891011121314151617181920212223242526272829
  1. export function getScrollParent(el) {
  2. let includeHidden = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  3. while (el) {
  4. if (includeHidden ? isPotentiallyScrollable(el) : hasScrollbar(el)) return el;
  5. el = el.parentElement;
  6. }
  7. return document.scrollingElement;
  8. }
  9. export function getScrollParents(el, stopAt) {
  10. const elements = [];
  11. if (stopAt && el && !stopAt.contains(el)) return elements;
  12. while (el) {
  13. if (hasScrollbar(el)) elements.push(el);
  14. if (el === stopAt) break;
  15. el = el.parentElement;
  16. }
  17. return elements;
  18. }
  19. export function hasScrollbar(el) {
  20. if (!el || el.nodeType !== Node.ELEMENT_NODE) return false;
  21. const style = window.getComputedStyle(el);
  22. return style.overflowY === 'scroll' || style.overflowY === 'auto' && el.scrollHeight > el.clientHeight;
  23. }
  24. function isPotentiallyScrollable(el) {
  25. if (!el || el.nodeType !== Node.ELEMENT_NODE) return false;
  26. const style = window.getComputedStyle(el);
  27. return ['scroll', 'auto'].includes(style.overflowY);
  28. }
  29. //# sourceMappingURL=getScrollParent.mjs.map