-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (61 loc) · 1.85 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* return a string: vertical | horizontal | both
* @param {Element} el - A HTML element
*/
const hasScrollbar = el => {
const { parseInt: p, getComputedStyle: c } = window;
const vertical =
el[`offsetWidth`] -
el[`clientWidth`] -
p(c(el).borderLeftWidth) -
p(c(el).borderRightWidth) >=
17;
const horizontal =
el[`offsetHeight`] -
el[`clientHeight`] -
p(c(el).borderTopWidth) -
p(c(el).borderBottomWidth) >=
17;
if (vertical && horizontal) return "both";
if (vertical) return "vertical";
if (horizontal) return "horizontal";
};
/**
* 1. Create a div element(wrapper) to wrap up el
* 2. Set the overflow value of wrapper to hidden to hide scrollbar(s) of el
* 3. Set negative margin on el to push out scrollbar(s) to invisible area
*/
export default (el, width, height) => {
if (!width || !height) throw new Error("width and height are required.");
const oldParent = el.parentElement;
const elIndex = Array.from(oldParent.children).indexOf(el);
const wrapper = document.createElement("div");
wrapper.appendChild(el);
oldParent.insertBefore(wrapper, oldParent.children[elIndex]);
const wStyle = wrapper.style;
wStyle.width = width;
wStyle.height = height;
wStyle.position = "relative";
wStyle.overflow = "hidden";
const eStyle = el.style;
eStyle.width = "100%";
eStyle.height = "100%";
eStyle.position = "absolute";
eStyle.overflow = "auto";
eStyle.top = 0;
eStyle.left = 0;
const direction = hasScrollbar(el);
if (direction === "vertical") {
eStyle.width = "auto";
eStyle.right = "-17px";
} else if (direction === "horizontal") {
eStyle.height = "auto";
eStyle.bottom = "-17px";
} else if (direction === "both") {
eStyle.width = "auto";
eStyle.height = "auto";
eStyle.right = "-17px";
eStyle.bottom = "-17px";
}
};
export { hasScrollbar };