UNPKG

@nutui/nutui

Version:

京东风格的轻量级移动端 Vue2、Vue3 组件库(支持小程序开发)

32 lines (31 loc) 850 B
import { ref, onMounted } from "vue"; const overflowScrollReg = /scroll|auto|overlay/i; const defaultRoot = window; function isElement(node) { const ELEMENT_NODE_TYPE = 1; return node.tagName !== "HTML" && node.tagName !== "BODY" && node.nodeType === ELEMENT_NODE_TYPE; } function getScrollParent(el, root = defaultRoot) { let node = el; while (node && node !== root && isElement(node)) { const { overflowY } = window.getComputedStyle(node); if (overflowScrollReg.test(overflowY)) { return node; } node = node.parentNode; } return root; } function useScrollParent(el, root = defaultRoot) { const scrollParent = ref(); onMounted(() => { if (el.value) { scrollParent.value = getScrollParent(el.value, root); } }); return scrollParent; } export { getScrollParent as g, useScrollParent as u };