@whitesev/domutils 1.9.2 → 1.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -58
- package/dist/index.amd.js +40 -22
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +40 -22
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +40 -22
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +40 -22
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +40 -22
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +40 -22
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/types/DOMUtilsCSSProperty.d.ts +36 -36
- package/dist/types/src/types/DOMUtilsEvent.d.ts +420 -420
- package/dist/types/src/types/WindowApi.d.ts +14 -14
- package/dist/types/src/types/env.d.ts +10 -10
- package/dist/types/src/types/global.d.ts +1 -1
- package/dist/types/src/types/gm.d.ts +2 -2
- package/index.ts +3 -3
- package/package.json +21 -22
- package/src/CommonUtils.ts +163 -163
- package/src/ElementAnimate.ts +290 -290
- package/src/ElementEvent.ts +1569 -1569
- package/src/ElementHandler.ts +43 -43
- package/src/ElementSelector.ts +307 -289
- package/src/ElementWait.ts +742 -742
- package/src/GlobalData.ts +5 -5
- package/src/OriginPrototype.ts +5 -5
- package/src/Utils.ts +388 -388
- package/src/WindowApi.ts +59 -59
- package/src/index.ts +2052 -2052
- package/src/types/DOMUtilsCSSProperty.d.ts +36 -36
- package/src/types/DOMUtilsEvent.d.ts +420 -420
- package/src/types/WindowApi.d.ts +14 -14
- package/src/types/env.d.ts +10 -10
- package/src/types/global.d.ts +1 -1
- package/src/types/gm.d.ts +2 -2
package/src/ElementAnimate.ts
CHANGED
|
@@ -1,290 +1,290 @@
|
|
|
1
|
-
import { CommonUtils } from "./CommonUtils";
|
|
2
|
-
import { elementSelector } from "./ElementSelector";
|
|
3
|
-
import { ElementWait } from "./ElementWait";
|
|
4
|
-
import type { DOMUtilsTargetElementType } from "./types/global";
|
|
5
|
-
import type { WindowApiOption } from "./types/WindowApi";
|
|
6
|
-
import { WindowApi } from "./WindowApi";
|
|
7
|
-
|
|
8
|
-
class ElementAnimate extends ElementWait {
|
|
9
|
-
windowApi: typeof WindowApi.prototype;
|
|
10
|
-
constructor(windowApiOption?: WindowApiOption) {
|
|
11
|
-
super(windowApiOption);
|
|
12
|
-
this.windowApi = new WindowApi(windowApiOption);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* 在一定时间内改变元素的样式属性,实现动画效果
|
|
16
|
-
* @param element 需要进行动画的元素
|
|
17
|
-
* @param styles 动画结束时元素的样式属性
|
|
18
|
-
* @param duration 动画持续时间,单位为毫秒
|
|
19
|
-
* @param callback 动画结束后执行的函数
|
|
20
|
-
* @example
|
|
21
|
-
* // 监听元素a.xx的从显示变为隐藏
|
|
22
|
-
* DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
|
|
23
|
-
* console.log("已往上位移100px")
|
|
24
|
-
* })
|
|
25
|
-
*/
|
|
26
|
-
animate(
|
|
27
|
-
element: DOMUtilsTargetElementType,
|
|
28
|
-
styles: CSSStyleDeclaration,
|
|
29
|
-
duration: number = 1000,
|
|
30
|
-
callback: (() => void) | undefined | null = null
|
|
31
|
-
) {
|
|
32
|
-
const context = this;
|
|
33
|
-
if (typeof element === "string") {
|
|
34
|
-
element = elementSelector.selectorAll(element);
|
|
35
|
-
}
|
|
36
|
-
if (element == null) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
if (CommonUtils.isNodeList(element)) {
|
|
40
|
-
// 设置
|
|
41
|
-
element.forEach(($ele) => {
|
|
42
|
-
context.animate($ele as HTMLElement, styles, duration, callback);
|
|
43
|
-
});
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
if (typeof duration !== "number" || duration <= 0) {
|
|
47
|
-
throw new TypeError("duration must be a positive number");
|
|
48
|
-
}
|
|
49
|
-
if (typeof callback !== "function" && callback !== void 0) {
|
|
50
|
-
throw new TypeError("callback must be a function or null");
|
|
51
|
-
}
|
|
52
|
-
if (typeof styles !== "object" || styles === void 0) {
|
|
53
|
-
throw new TypeError("styles must be an object");
|
|
54
|
-
}
|
|
55
|
-
if (Object.keys(styles).length === 0) {
|
|
56
|
-
throw new Error("styles must contain at least one property");
|
|
57
|
-
}
|
|
58
|
-
const start = performance.now();
|
|
59
|
-
const from: {
|
|
60
|
-
[prop: string]: any;
|
|
61
|
-
} = {};
|
|
62
|
-
const to: {
|
|
63
|
-
[prop: string]: any;
|
|
64
|
-
} = {};
|
|
65
|
-
for (const prop in styles) {
|
|
66
|
-
from[prop] = element.style[prop] || context.windowApi.globalThis.getComputedStyle(element)[prop];
|
|
67
|
-
to[prop] = styles[prop];
|
|
68
|
-
}
|
|
69
|
-
const timer = setInterval(function () {
|
|
70
|
-
const timePassed = performance.now() - start;
|
|
71
|
-
let progress = timePassed / duration;
|
|
72
|
-
if (progress > 1) {
|
|
73
|
-
progress = 1;
|
|
74
|
-
}
|
|
75
|
-
for (const prop in styles) {
|
|
76
|
-
element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
|
|
77
|
-
}
|
|
78
|
-
if (progress === 1) {
|
|
79
|
-
clearInterval(timer);
|
|
80
|
-
if (callback) {
|
|
81
|
-
callback();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}, 10);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* 显示元素
|
|
88
|
-
* @param target 当前元素
|
|
89
|
-
* @param checkVisiblie 是否检测元素是否显示
|
|
90
|
-
* + true (默认)如果检测到还未显示,则强制使用display: unset !important;
|
|
91
|
-
* + false 不检测,直接设置display属性为空
|
|
92
|
-
* @example
|
|
93
|
-
* // 显示a.xx元素
|
|
94
|
-
* DOMUtils.show(document.querySelector("a.xx"))
|
|
95
|
-
* DOMUtils.show(document.querySelectorAll("a.xx"))
|
|
96
|
-
* DOMUtils.show("a.xx")
|
|
97
|
-
*/
|
|
98
|
-
show(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
|
|
99
|
-
const context = this;
|
|
100
|
-
if (target == null) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
if (typeof target === "string") {
|
|
104
|
-
target = elementSelector.selectorAll(target);
|
|
105
|
-
}
|
|
106
|
-
if (target instanceof NodeList || target instanceof Array) {
|
|
107
|
-
target = target as HTMLElement[];
|
|
108
|
-
for (const element of target) {
|
|
109
|
-
context.show(element, checkVisiblie);
|
|
110
|
-
}
|
|
111
|
-
} else {
|
|
112
|
-
target = target as HTMLElement;
|
|
113
|
-
target.style.display = "";
|
|
114
|
-
if (checkVisiblie) {
|
|
115
|
-
if (!CommonUtils.isShow(target)) {
|
|
116
|
-
/* 仍然是不显示,尝试使用强覆盖 */
|
|
117
|
-
target.style.setProperty("display", "unset", "important");
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* 隐藏元素
|
|
124
|
-
* @param target 当前元素
|
|
125
|
-
* @param checkVisiblie 是否检测元素是否显示
|
|
126
|
-
* + true (默认)如果检测到显示,则强制使用display: none !important;
|
|
127
|
-
* + false 不检测,直接设置display属性为none
|
|
128
|
-
* @example
|
|
129
|
-
* // 隐藏a.xx元素
|
|
130
|
-
* DOMUtils.hide(document.querySelector("a.xx"))
|
|
131
|
-
* DOMUtils.hide(document.querySelectorAll("a.xx"))
|
|
132
|
-
* DOMUtils.hide("a.xx")
|
|
133
|
-
*/
|
|
134
|
-
hide(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
|
|
135
|
-
const context = this;
|
|
136
|
-
if (target == null) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (typeof target === "string") {
|
|
140
|
-
target = elementSelector.selectorAll(target);
|
|
141
|
-
}
|
|
142
|
-
if (target instanceof NodeList || target instanceof Array) {
|
|
143
|
-
target = target as HTMLElement[];
|
|
144
|
-
for (const element of target) {
|
|
145
|
-
context.hide(element, checkVisiblie);
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
target = target as HTMLElement;
|
|
149
|
-
target.style.display = "none";
|
|
150
|
-
if (checkVisiblie) {
|
|
151
|
-
if (CommonUtils.isShow(target)) {
|
|
152
|
-
/* 仍然是显示,尝试使用强覆盖 */
|
|
153
|
-
target.style.setProperty("display", "none", "important");
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* 淡入元素
|
|
160
|
-
* @param element 当前元素
|
|
161
|
-
* @param duration 动画持续时间(毫秒),默认400毫秒
|
|
162
|
-
* @param callback 动画结束的回调
|
|
163
|
-
* @example
|
|
164
|
-
* // 元素a.xx淡入
|
|
165
|
-
* DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
|
|
166
|
-
* console.log("淡入完毕");
|
|
167
|
-
* })
|
|
168
|
-
* DOMUtils.fadeIn("a.xx",undefined,()=>{
|
|
169
|
-
* console.log("淡入完毕");
|
|
170
|
-
* })
|
|
171
|
-
*/
|
|
172
|
-
fadeIn(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
|
|
173
|
-
if (element == null) {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const context = this;
|
|
177
|
-
if (typeof element === "string") {
|
|
178
|
-
element = elementSelector.selectorAll(element);
|
|
179
|
-
}
|
|
180
|
-
if (CommonUtils.isNodeList(element)) {
|
|
181
|
-
// 设置
|
|
182
|
-
element.forEach(($ele) => {
|
|
183
|
-
context.fadeIn($ele as HTMLElement, duration, callback);
|
|
184
|
-
});
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
element.style.opacity = "0";
|
|
188
|
-
element.style.display = "";
|
|
189
|
-
let start: number = null as any;
|
|
190
|
-
let timer: number = null as any;
|
|
191
|
-
function step(timestamp: number) {
|
|
192
|
-
if (!start) start = timestamp;
|
|
193
|
-
const progress = timestamp - start;
|
|
194
|
-
element = element as HTMLElement;
|
|
195
|
-
element.style.opacity = Math.min(progress / duration, 1).toString();
|
|
196
|
-
if (progress < duration) {
|
|
197
|
-
context.windowApi.window.requestAnimationFrame(step);
|
|
198
|
-
} else {
|
|
199
|
-
if (callback && typeof callback === "function") {
|
|
200
|
-
callback();
|
|
201
|
-
}
|
|
202
|
-
context.windowApi.window.cancelAnimationFrame(timer);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
timer = context.windowApi.window.requestAnimationFrame(step);
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* 淡出元素
|
|
209
|
-
* @param element 当前元素
|
|
210
|
-
* @param duration 动画持续时间(毫秒),默认400毫秒
|
|
211
|
-
* @param callback 动画结束的回调
|
|
212
|
-
* @example
|
|
213
|
-
* // 元素a.xx淡出
|
|
214
|
-
* DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
|
|
215
|
-
* console.log("淡出完毕");
|
|
216
|
-
* })
|
|
217
|
-
* DOMUtils.fadeOut("a.xx",undefined,()=>{
|
|
218
|
-
* console.log("淡出完毕");
|
|
219
|
-
* })
|
|
220
|
-
*/
|
|
221
|
-
fadeOut(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
|
|
222
|
-
const context = this;
|
|
223
|
-
if (element == null) {
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
if (typeof element === "string") {
|
|
227
|
-
element = elementSelector.selectorAll(element);
|
|
228
|
-
}
|
|
229
|
-
if (CommonUtils.isNodeList(element)) {
|
|
230
|
-
// 设置
|
|
231
|
-
element.forEach(($ele) => {
|
|
232
|
-
context.fadeOut($ele as HTMLElement, duration, callback);
|
|
233
|
-
});
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
element.style.opacity = "1";
|
|
237
|
-
let start: number = null as any;
|
|
238
|
-
let timer: number = null as any;
|
|
239
|
-
function step(timestamp: number) {
|
|
240
|
-
if (!start) start = timestamp;
|
|
241
|
-
const progress = timestamp - start;
|
|
242
|
-
element = element as HTMLElement;
|
|
243
|
-
element.style.opacity = Math.max(1 - progress / duration, 0).toString();
|
|
244
|
-
if (progress < duration) {
|
|
245
|
-
context.windowApi.window.requestAnimationFrame(step);
|
|
246
|
-
} else {
|
|
247
|
-
element.style.display = "none";
|
|
248
|
-
if (typeof callback === "function") {
|
|
249
|
-
callback();
|
|
250
|
-
}
|
|
251
|
-
context.windowApi.window.cancelAnimationFrame(timer);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
timer = context.windowApi.window.requestAnimationFrame(step);
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* 切换元素的显示和隐藏状态
|
|
258
|
-
* @param element 当前元素
|
|
259
|
-
* @param checkVisiblie 是否检测元素是否显示
|
|
260
|
-
* @example
|
|
261
|
-
* // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
|
|
262
|
-
* DOMUtils.toggle(document.querySelector("a.xx"))
|
|
263
|
-
* DOMUtils.toggle("a.xx")
|
|
264
|
-
*/
|
|
265
|
-
toggle(element: DOMUtilsTargetElementType, checkVisiblie?: boolean) {
|
|
266
|
-
const context = this;
|
|
267
|
-
if (typeof element === "string") {
|
|
268
|
-
element = elementSelector.selectorAll(element);
|
|
269
|
-
}
|
|
270
|
-
if (element == null) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
if (CommonUtils.isNodeList(element)) {
|
|
274
|
-
// 设置
|
|
275
|
-
element.forEach(($ele) => {
|
|
276
|
-
context.toggle($ele as HTMLElement);
|
|
277
|
-
});
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
if (context.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
|
|
281
|
-
context.show(element, checkVisiblie);
|
|
282
|
-
} else {
|
|
283
|
-
context.hide(element, checkVisiblie);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const elementAnimate = new ElementAnimate();
|
|
289
|
-
|
|
290
|
-
export { elementAnimate, ElementAnimate };
|
|
1
|
+
import { CommonUtils } from "./CommonUtils";
|
|
2
|
+
import { elementSelector } from "./ElementSelector";
|
|
3
|
+
import { ElementWait } from "./ElementWait";
|
|
4
|
+
import type { DOMUtilsTargetElementType } from "./types/global";
|
|
5
|
+
import type { WindowApiOption } from "./types/WindowApi";
|
|
6
|
+
import { WindowApi } from "./WindowApi";
|
|
7
|
+
|
|
8
|
+
class ElementAnimate extends ElementWait {
|
|
9
|
+
windowApi: typeof WindowApi.prototype;
|
|
10
|
+
constructor(windowApiOption?: WindowApiOption) {
|
|
11
|
+
super(windowApiOption);
|
|
12
|
+
this.windowApi = new WindowApi(windowApiOption);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 在一定时间内改变元素的样式属性,实现动画效果
|
|
16
|
+
* @param element 需要进行动画的元素
|
|
17
|
+
* @param styles 动画结束时元素的样式属性
|
|
18
|
+
* @param duration 动画持续时间,单位为毫秒
|
|
19
|
+
* @param callback 动画结束后执行的函数
|
|
20
|
+
* @example
|
|
21
|
+
* // 监听元素a.xx的从显示变为隐藏
|
|
22
|
+
* DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
|
|
23
|
+
* console.log("已往上位移100px")
|
|
24
|
+
* })
|
|
25
|
+
*/
|
|
26
|
+
animate(
|
|
27
|
+
element: DOMUtilsTargetElementType,
|
|
28
|
+
styles: CSSStyleDeclaration,
|
|
29
|
+
duration: number = 1000,
|
|
30
|
+
callback: (() => void) | undefined | null = null
|
|
31
|
+
) {
|
|
32
|
+
const context = this;
|
|
33
|
+
if (typeof element === "string") {
|
|
34
|
+
element = elementSelector.selectorAll(element);
|
|
35
|
+
}
|
|
36
|
+
if (element == null) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (CommonUtils.isNodeList(element)) {
|
|
40
|
+
// 设置
|
|
41
|
+
element.forEach(($ele) => {
|
|
42
|
+
context.animate($ele as HTMLElement, styles, duration, callback);
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (typeof duration !== "number" || duration <= 0) {
|
|
47
|
+
throw new TypeError("duration must be a positive number");
|
|
48
|
+
}
|
|
49
|
+
if (typeof callback !== "function" && callback !== void 0) {
|
|
50
|
+
throw new TypeError("callback must be a function or null");
|
|
51
|
+
}
|
|
52
|
+
if (typeof styles !== "object" || styles === void 0) {
|
|
53
|
+
throw new TypeError("styles must be an object");
|
|
54
|
+
}
|
|
55
|
+
if (Object.keys(styles).length === 0) {
|
|
56
|
+
throw new Error("styles must contain at least one property");
|
|
57
|
+
}
|
|
58
|
+
const start = performance.now();
|
|
59
|
+
const from: {
|
|
60
|
+
[prop: string]: any;
|
|
61
|
+
} = {};
|
|
62
|
+
const to: {
|
|
63
|
+
[prop: string]: any;
|
|
64
|
+
} = {};
|
|
65
|
+
for (const prop in styles) {
|
|
66
|
+
from[prop] = element.style[prop] || context.windowApi.globalThis.getComputedStyle(element)[prop];
|
|
67
|
+
to[prop] = styles[prop];
|
|
68
|
+
}
|
|
69
|
+
const timer = setInterval(function () {
|
|
70
|
+
const timePassed = performance.now() - start;
|
|
71
|
+
let progress = timePassed / duration;
|
|
72
|
+
if (progress > 1) {
|
|
73
|
+
progress = 1;
|
|
74
|
+
}
|
|
75
|
+
for (const prop in styles) {
|
|
76
|
+
element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
|
|
77
|
+
}
|
|
78
|
+
if (progress === 1) {
|
|
79
|
+
clearInterval(timer);
|
|
80
|
+
if (callback) {
|
|
81
|
+
callback();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}, 10);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 显示元素
|
|
88
|
+
* @param target 当前元素
|
|
89
|
+
* @param checkVisiblie 是否检测元素是否显示
|
|
90
|
+
* + true (默认)如果检测到还未显示,则强制使用display: unset !important;
|
|
91
|
+
* + false 不检测,直接设置display属性为空
|
|
92
|
+
* @example
|
|
93
|
+
* // 显示a.xx元素
|
|
94
|
+
* DOMUtils.show(document.querySelector("a.xx"))
|
|
95
|
+
* DOMUtils.show(document.querySelectorAll("a.xx"))
|
|
96
|
+
* DOMUtils.show("a.xx")
|
|
97
|
+
*/
|
|
98
|
+
show(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
|
|
99
|
+
const context = this;
|
|
100
|
+
if (target == null) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (typeof target === "string") {
|
|
104
|
+
target = elementSelector.selectorAll(target);
|
|
105
|
+
}
|
|
106
|
+
if (target instanceof NodeList || target instanceof Array) {
|
|
107
|
+
target = target as HTMLElement[];
|
|
108
|
+
for (const element of target) {
|
|
109
|
+
context.show(element, checkVisiblie);
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
target = target as HTMLElement;
|
|
113
|
+
target.style.display = "";
|
|
114
|
+
if (checkVisiblie) {
|
|
115
|
+
if (!CommonUtils.isShow(target)) {
|
|
116
|
+
/* 仍然是不显示,尝试使用强覆盖 */
|
|
117
|
+
target.style.setProperty("display", "unset", "important");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* 隐藏元素
|
|
124
|
+
* @param target 当前元素
|
|
125
|
+
* @param checkVisiblie 是否检测元素是否显示
|
|
126
|
+
* + true (默认)如果检测到显示,则强制使用display: none !important;
|
|
127
|
+
* + false 不检测,直接设置display属性为none
|
|
128
|
+
* @example
|
|
129
|
+
* // 隐藏a.xx元素
|
|
130
|
+
* DOMUtils.hide(document.querySelector("a.xx"))
|
|
131
|
+
* DOMUtils.hide(document.querySelectorAll("a.xx"))
|
|
132
|
+
* DOMUtils.hide("a.xx")
|
|
133
|
+
*/
|
|
134
|
+
hide(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
|
|
135
|
+
const context = this;
|
|
136
|
+
if (target == null) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (typeof target === "string") {
|
|
140
|
+
target = elementSelector.selectorAll(target);
|
|
141
|
+
}
|
|
142
|
+
if (target instanceof NodeList || target instanceof Array) {
|
|
143
|
+
target = target as HTMLElement[];
|
|
144
|
+
for (const element of target) {
|
|
145
|
+
context.hide(element, checkVisiblie);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
target = target as HTMLElement;
|
|
149
|
+
target.style.display = "none";
|
|
150
|
+
if (checkVisiblie) {
|
|
151
|
+
if (CommonUtils.isShow(target)) {
|
|
152
|
+
/* 仍然是显示,尝试使用强覆盖 */
|
|
153
|
+
target.style.setProperty("display", "none", "important");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* 淡入元素
|
|
160
|
+
* @param element 当前元素
|
|
161
|
+
* @param duration 动画持续时间(毫秒),默认400毫秒
|
|
162
|
+
* @param callback 动画结束的回调
|
|
163
|
+
* @example
|
|
164
|
+
* // 元素a.xx淡入
|
|
165
|
+
* DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
|
|
166
|
+
* console.log("淡入完毕");
|
|
167
|
+
* })
|
|
168
|
+
* DOMUtils.fadeIn("a.xx",undefined,()=>{
|
|
169
|
+
* console.log("淡入完毕");
|
|
170
|
+
* })
|
|
171
|
+
*/
|
|
172
|
+
fadeIn(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
|
|
173
|
+
if (element == null) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const context = this;
|
|
177
|
+
if (typeof element === "string") {
|
|
178
|
+
element = elementSelector.selectorAll(element);
|
|
179
|
+
}
|
|
180
|
+
if (CommonUtils.isNodeList(element)) {
|
|
181
|
+
// 设置
|
|
182
|
+
element.forEach(($ele) => {
|
|
183
|
+
context.fadeIn($ele as HTMLElement, duration, callback);
|
|
184
|
+
});
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
element.style.opacity = "0";
|
|
188
|
+
element.style.display = "";
|
|
189
|
+
let start: number = null as any;
|
|
190
|
+
let timer: number = null as any;
|
|
191
|
+
function step(timestamp: number) {
|
|
192
|
+
if (!start) start = timestamp;
|
|
193
|
+
const progress = timestamp - start;
|
|
194
|
+
element = element as HTMLElement;
|
|
195
|
+
element.style.opacity = Math.min(progress / duration, 1).toString();
|
|
196
|
+
if (progress < duration) {
|
|
197
|
+
context.windowApi.window.requestAnimationFrame(step);
|
|
198
|
+
} else {
|
|
199
|
+
if (callback && typeof callback === "function") {
|
|
200
|
+
callback();
|
|
201
|
+
}
|
|
202
|
+
context.windowApi.window.cancelAnimationFrame(timer);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
timer = context.windowApi.window.requestAnimationFrame(step);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* 淡出元素
|
|
209
|
+
* @param element 当前元素
|
|
210
|
+
* @param duration 动画持续时间(毫秒),默认400毫秒
|
|
211
|
+
* @param callback 动画结束的回调
|
|
212
|
+
* @example
|
|
213
|
+
* // 元素a.xx淡出
|
|
214
|
+
* DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
|
|
215
|
+
* console.log("淡出完毕");
|
|
216
|
+
* })
|
|
217
|
+
* DOMUtils.fadeOut("a.xx",undefined,()=>{
|
|
218
|
+
* console.log("淡出完毕");
|
|
219
|
+
* })
|
|
220
|
+
*/
|
|
221
|
+
fadeOut(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
|
|
222
|
+
const context = this;
|
|
223
|
+
if (element == null) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (typeof element === "string") {
|
|
227
|
+
element = elementSelector.selectorAll(element);
|
|
228
|
+
}
|
|
229
|
+
if (CommonUtils.isNodeList(element)) {
|
|
230
|
+
// 设置
|
|
231
|
+
element.forEach(($ele) => {
|
|
232
|
+
context.fadeOut($ele as HTMLElement, duration, callback);
|
|
233
|
+
});
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
element.style.opacity = "1";
|
|
237
|
+
let start: number = null as any;
|
|
238
|
+
let timer: number = null as any;
|
|
239
|
+
function step(timestamp: number) {
|
|
240
|
+
if (!start) start = timestamp;
|
|
241
|
+
const progress = timestamp - start;
|
|
242
|
+
element = element as HTMLElement;
|
|
243
|
+
element.style.opacity = Math.max(1 - progress / duration, 0).toString();
|
|
244
|
+
if (progress < duration) {
|
|
245
|
+
context.windowApi.window.requestAnimationFrame(step);
|
|
246
|
+
} else {
|
|
247
|
+
element.style.display = "none";
|
|
248
|
+
if (typeof callback === "function") {
|
|
249
|
+
callback();
|
|
250
|
+
}
|
|
251
|
+
context.windowApi.window.cancelAnimationFrame(timer);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
timer = context.windowApi.window.requestAnimationFrame(step);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 切换元素的显示和隐藏状态
|
|
258
|
+
* @param element 当前元素
|
|
259
|
+
* @param checkVisiblie 是否检测元素是否显示
|
|
260
|
+
* @example
|
|
261
|
+
* // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
|
|
262
|
+
* DOMUtils.toggle(document.querySelector("a.xx"))
|
|
263
|
+
* DOMUtils.toggle("a.xx")
|
|
264
|
+
*/
|
|
265
|
+
toggle(element: DOMUtilsTargetElementType, checkVisiblie?: boolean) {
|
|
266
|
+
const context = this;
|
|
267
|
+
if (typeof element === "string") {
|
|
268
|
+
element = elementSelector.selectorAll(element);
|
|
269
|
+
}
|
|
270
|
+
if (element == null) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (CommonUtils.isNodeList(element)) {
|
|
274
|
+
// 设置
|
|
275
|
+
element.forEach(($ele) => {
|
|
276
|
+
context.toggle($ele as HTMLElement);
|
|
277
|
+
});
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (context.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
|
|
281
|
+
context.show(element, checkVisiblie);
|
|
282
|
+
} else {
|
|
283
|
+
context.hide(element, checkVisiblie);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const elementAnimate = new ElementAnimate();
|
|
289
|
+
|
|
290
|
+
export { elementAnimate, ElementAnimate };
|