-
Notifications
You must be signed in to change notification settings - Fork 888
Expand file tree
/
Copy pathget-scroll.js
More file actions
44 lines (38 loc) · 1.09 KB
/
get-scroll.js
File metadata and controls
44 lines (38 loc) · 1.09 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
import memoize from './memoize';
/**
* 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}
*/
function getScroll(elm, 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);
}
export default memoize(getScroll);