ph-utils 0.15.0 → 0.15.2
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/lib/dom.d.ts +3 -3
- package/lib/dom.js +35 -17
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
@@ -82,7 +82,7 @@ export declare function toggleClass(el: HTMLElement, clazz: string): void;
|
|
82
82
|
* @param {function} event 事件处理函数
|
83
83
|
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
84
84
|
*/
|
85
|
-
export declare function on(element: HTMLElement | ShadowRoot | Document, listener: string, fn: EventListener, option?: boolean | (AddEventListenerOptions & {
|
85
|
+
export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | (AddEventListenerOptions & {
|
86
86
|
eventFlag?: string;
|
87
87
|
})): void;
|
88
88
|
/**
|
@@ -91,7 +91,7 @@ export declare function on(element: HTMLElement | ShadowRoot | Document, listene
|
|
91
91
|
* @param listener - 事件名称。
|
92
92
|
* @param fn - 要移除的事件监听器函数。
|
93
93
|
*/
|
94
|
-
export declare function off(el: HTMLElement | ShadowRoot | Document, listener: string, fn: EventListener, option?: boolean | EventListenerOptions): void;
|
94
|
+
export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | EventListenerOptions): void;
|
95
95
|
/**
|
96
96
|
* 判断事件是否应该继续传递。
|
97
97
|
* 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
|
@@ -124,7 +124,7 @@ export declare function text(element: HTMLElement, textstr?: string): string | n
|
|
124
124
|
* @param elems
|
125
125
|
* @param fn 遍历到节点时的回调,回调第一个参数为遍历到的节点,第2个参数为 index;如果回调函数返回 true,则会终止遍历(break)
|
126
126
|
*/
|
127
|
-
export declare function iterate(elems: NodeList |
|
127
|
+
export declare function iterate<T>(elems: NodeList | HTMLCollection | T[], fn: (el: HTMLElement | T, index: number) => any): void;
|
128
128
|
/**
|
129
129
|
* 设置或获取节点 data-* 属性
|
130
130
|
* @param elem
|
package/lib/dom.js
CHANGED
@@ -133,7 +133,14 @@ export function on(element, listener, fn, option) {
|
|
133
133
|
element.setAttribute(option.eventFlag, "__stop__");
|
134
134
|
}
|
135
135
|
}
|
136
|
-
element.
|
136
|
+
if (element.length) {
|
137
|
+
iterate(element, (elem) => {
|
138
|
+
elem.addEventListener(listener, fn, option);
|
139
|
+
});
|
140
|
+
}
|
141
|
+
else if (element) {
|
142
|
+
element.addEventListener(listener, fn, option);
|
143
|
+
}
|
137
144
|
}
|
138
145
|
/**
|
139
146
|
* 移除指定元素的事件监听器。
|
@@ -142,7 +149,14 @@ export function on(element, listener, fn, option) {
|
|
142
149
|
* @param fn - 要移除的事件监听器函数。
|
143
150
|
*/
|
144
151
|
export function off(el, listener, fn, option) {
|
145
|
-
el.
|
152
|
+
if (el.length) {
|
153
|
+
iterate(el, (elem) => {
|
154
|
+
elem.removeEventListener(listener, fn, option);
|
155
|
+
});
|
156
|
+
}
|
157
|
+
else if (el) {
|
158
|
+
el.removeEventListener(listener, fn, option);
|
159
|
+
}
|
146
160
|
}
|
147
161
|
/**
|
148
162
|
* 判断事件是否应该继续传递。
|
@@ -439,6 +453,8 @@ function toggleCssProperty(el, properties, method = "set") {
|
|
439
453
|
export function transition(el, nameOrProperties, dir = "enter", finish) {
|
440
454
|
const p = dir === "enter" ? "from" : "to";
|
441
455
|
let nameClass = "", activeClass = "";
|
456
|
+
/** 动画状态, -1 - 准备, 0 - 进行中, 1 - 完成 */
|
457
|
+
let status = -1;
|
442
458
|
const trans = [];
|
443
459
|
if (typeof nameOrProperties === "string") {
|
444
460
|
nameClass = `${nameOrProperties}-${dir}-${p}`;
|
@@ -452,6 +468,7 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
|
|
452
468
|
}
|
453
469
|
}
|
454
470
|
}
|
471
|
+
status = 0;
|
455
472
|
if (dir === "enter") {
|
456
473
|
if (nameClass) {
|
457
474
|
el.classList.add(nameClass);
|
@@ -490,25 +507,26 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
|
|
490
507
|
});
|
491
508
|
}
|
492
509
|
}
|
493
|
-
el.addEventListener("transitionend", () => {
|
494
|
-
if (
|
495
|
-
|
496
|
-
|
497
|
-
el.classList.remove(
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
if (trans) {
|
502
|
-
el.style.removeProperty("transition");
|
510
|
+
el.addEventListener("transitionend", (e) => {
|
511
|
+
if (status === 0) {
|
512
|
+
status = 1;
|
513
|
+
if (nameClass) {
|
514
|
+
el.classList.remove(activeClass);
|
515
|
+
requestAnimationFrame(() => {
|
516
|
+
el.classList.remove(nameClass);
|
517
|
+
});
|
503
518
|
}
|
504
|
-
|
519
|
+
else {
|
520
|
+
if (trans) {
|
521
|
+
el.style.removeProperty("transition");
|
522
|
+
}
|
505
523
|
requestAnimationFrame(() => {
|
506
524
|
toggleCssProperty(el, nameOrProperties, "remove");
|
507
525
|
});
|
508
|
-
}
|
509
|
-
|
510
|
-
|
511
|
-
|
526
|
+
}
|
527
|
+
if (finish) {
|
528
|
+
finish();
|
529
|
+
}
|
512
530
|
}
|
513
531
|
}, { once: true });
|
514
532
|
}
|