forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.ts
133 lines (115 loc) · 3.58 KB
/
css.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const PIXEL_PATTERN = /margin|padding|width|height|max|min|offset/;
const removePixel = {
left: true,
top: true,
};
const floatMap = {
cssFloat: 1,
styleFloat: 1,
float: 1,
};
function getComputedStyle(node: HTMLElement) {
return node.nodeType === 1 ? node.ownerDocument.defaultView.getComputedStyle(node, null) : {};
}
function getStyleValue(node: HTMLElement, type: string, value: string) {
type = type.toLowerCase();
if (value === 'auto') {
if (type === 'height') {
return node.offsetHeight;
}
if (type === 'width') {
return node.offsetWidth;
}
}
if (!(type in removePixel)) {
removePixel[type] = PIXEL_PATTERN.test(type);
}
return removePixel[type] ? parseFloat(value) || 0 : value;
}
export function get(node: HTMLElement, name: any) {
const length = arguments.length;
const style = getComputedStyle(node);
name = floatMap[name] ? ('cssFloat' in node.style ? 'cssFloat' : 'styleFloat') : name;
return length === 1 ? style : getStyleValue(node, name, style[name] || node.style[name]);
}
export function set(node: HTMLElement, name: any, value: string | number) {
const length = arguments.length;
name = floatMap[name] ? ('cssFloat' in node.style ? 'cssFloat' : 'styleFloat') : name;
if (length === 3) {
if (typeof value === 'number' && PIXEL_PATTERN.test(name)) {
value = `${value}px`;
}
node.style[name as string] = value; // Number
return value;
}
for (const x in name) {
if (name.hasOwnProperty(x)) {
set(node, x, name[x]);
}
}
return getComputedStyle(node);
}
export function getOuterWidth(el: HTMLElement) {
if (el === document.body) {
return document.documentElement.clientWidth;
}
return el.offsetWidth;
}
export function getOuterHeight(el: HTMLElement) {
if (el === document.body) {
return window.innerHeight || document.documentElement.clientHeight;
}
return el.offsetHeight;
}
export function getDocSize() {
const width = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
const height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
return {
width,
height,
};
}
export function getClientSize() {
const width = document.documentElement.clientWidth;
const height = window.innerHeight || document.documentElement.clientHeight;
return {
width,
height,
};
}
export function getScroll() {
return {
scrollLeft: Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
scrollTop: Math.max(document.documentElement.scrollTop, document.body.scrollTop),
};
}
export function getOffset(node: any) {
const box = node.getBoundingClientRect();
const docElem = document.documentElement;
return {
left:
box.left +
(window.scrollX || docElem.scrollLeft) -
(docElem.clientLeft || document.body.clientLeft || 0),
top:
box.top +
(window.scrollY || docElem.scrollTop) -
(docElem.clientTop || document.body.clientTop || 0),
};
}
export function styleToString(style: CSSStyleDeclaration) {
// There are some different behavior between Firefox & Chrome.
// We have to handle this ourself.
const styleNames = Array.prototype.slice.apply(style);
return styleNames.map(name => `${name}: ${style.getPropertyValue(name)};`).join('');
}
export function styleObjectToString(style: Record<string, string>) {
return Object.keys(style).reduce((acc, name) => {
const styleValue = style[name];
if (typeof styleValue === 'undefined' || styleValue === null) {
return acc;
}
acc += `${name}: ${style[name]};`;
return acc;
}, '');
}