-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollElement.js
44 lines (40 loc) · 1013 Bytes
/
scrollElement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict'
function getParentNodes(node, _parents) {
_parents =_parents || []
let parent = node.parentNode
if (!parent) {
return _parents
}
_parents.push(parent)
// end when match body
if (parent === document.body) return _parents
return getParentNodes(parent, _parents)
}
function isOverflow(node) {
if (!node) return ''
let style
if (window.getComputedStyle) {
try {
style = window.getComputedStyle(node)
} catch(e) {}
} else if (node.currentStyle) {
style = node.currentStyle
}
if (!style) return ''
return style["overflow-x"] || style["overflow"]
}
function isScrollElement(node) {
return /(auto|scroll)/.test(isOverflow(node))
}
export default function (node) {
let parents = getParentNodes(node)
let scrollParent = document.body
for (let i = 0;i < parents.length;i ++) {
let p = parents[i]
if (p !== document.body && isScrollElement(p)) {
scrollParent = p
break
}
}
return scrollParent
}