forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-scroll.js
More file actions
45 lines (40 loc) · 1.26 KB
/
Copy pathget-scroll.js
File metadata and controls
45 lines (40 loc) · 1.26 KB
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
45
/**
* Get the scroll position of given element
* @method getScroll
* @memberof axe.utils
* @param {Element} elm
* @param {buffer} (Optional) allowed negligence in overflow
* @returns {Object | undefined}
*/
export default function getScroll(elm, buffer) {
// assigned in the body rather than as a default parameter: default
// parameters are excluded from fn.length, which memoize uses to decide how
// many arguments to cache on, so `buffer` would be left out of the key
buffer ??= 0;
const overflowX = elm.scrollWidth > elm.clientWidth + buffer;
const overflowY = elm.scrollHeight > elm.clientHeight + buffer;
/**
* if there is neither `overflow-x` or `overflow-y`
* -> return
*/
if (!(overflowX || overflowY)) {
return;
}
const style = window.getComputedStyle(elm);
const scrollableX = isScrollable(style, 'overflow-x');
const scrollableY = isScrollable(style, 'overflow-y');
/**
* check direction of `overflow` and `scrollable`
*/
if ((overflowX && scrollableX) || (overflowY && scrollableY)) {
return {
elm,
top: elm.scrollTop,
left: elm.scrollLeft
};
}
}
function isScrollable(style, prop) {
const overflowProp = style.getPropertyValue(prop);
return ['scroll', 'auto'].includes(overflowProp);
}