ph-utils 0.11.3 → 0.11.4

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.
Files changed (3) hide show
  1. package/lib/dom.d.ts +11 -11
  2. package/lib/dom.js +18 -29
  3. package/package.json +1 -1
package/lib/dom.d.ts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * web(浏览器端) DOM 文件操作
3
+ * 现今不推荐在使用这种方式,现在开发前端的时候,推荐使用一些成熟的框架例如:React、Preact、Vue、Angular、Svelte、Ember、Knockout等
4
+ * 在使用这些框架的时候额外的一些不可避免的 dom 操作时才使用这个工具;如果确实需要使用原生开发推荐使用 jquery 或者想要更精简的话可以使用 https://github.com/finom/bala 封装
5
+ */
1
6
  /**
2
7
  * 根据选择器获取节点
3
8
  * @param {string} selector 选择器
@@ -37,17 +42,12 @@ export declare function removeClass(elem: HTMLElement, clazz: string): void;
37
42
  */
38
43
  export declare function hasClass(elem: HTMLElement, clazz: string): boolean;
39
44
  /**
40
- * 为节点添加 transition 属性,包括浏览器前缀
41
- * @param {HTMLElement} element 需要添加 css Transition 属性的节点
42
- * @param {string} value css transition
43
- */
44
- export declare function transition(element: HTMLElement, value: string): void;
45
- /**
46
- * 为节点添加 transform 属性,包括浏览器前缀
47
- * @param {HTMLElement} element 需要添加 css transform 属性的节点
48
- * @param {string} value css transform 值
45
+ * 切换指定元素的类名。
46
+ * 如果元素已包含该类名,则移除它;否则添加它。
47
+ * @param el - 要操作的 HTML 元素。
48
+ * @param clazz - 要切换的类名。
49
49
  */
50
- export declare function transform(element: HTMLElement, value: string): void;
50
+ export declare function toggleClass(el: HTMLElement, clazz: string): void;
51
51
  /**
52
52
  * 为节点添加事件处理
53
53
  * @param {HTMLElement} element 添加事件的节点
@@ -173,7 +173,7 @@ export declare function formatStyle(styleObj: (string | undefined | null | "")[]
173
173
  ]);
174
174
  * ```
175
175
  */
176
- export declare function startTransition(target: HTMLElement, properties: [string, string][], duration?: string): void;
176
+ export declare function startTransition(target: HTMLElement, properties: [string, string][], duration?: string | undefined | null): void;
177
177
  /**
178
178
  * 结束元素的过渡效果,并在过渡结束后隐藏元素。
179
179
  * @param target - 要操作的 HTML 元素。
package/lib/dom.js CHANGED
@@ -3,7 +3,6 @@
3
3
  * 现今不推荐在使用这种方式,现在开发前端的时候,推荐使用一些成熟的框架例如:React、Preact、Vue、Angular、Svelte、Ember、Knockout等
4
4
  * 在使用这些框架的时候额外的一些不可避免的 dom 操作时才使用这个工具;如果确实需要使用原生开发推荐使用 jquery 或者想要更精简的话可以使用 https://github.com/finom/bala 封装
5
5
  */
6
- const vendorPrefix = ["", "-webkit", "-moz-"];
7
6
  /**
8
7
  * 根据选择器获取节点
9
8
  * @param {string} selector 选择器
@@ -60,26 +59,18 @@ export function hasClass(elem, clazz) {
60
59
  return elem.classList.contains(clazz);
61
60
  }
62
61
  /**
63
- * 为节点添加 transition 属性,包括浏览器前缀
64
- * @param {HTMLElement} element 需要添加 css Transition 属性的节点
65
- * @param {string} value css transition
62
+ * 切换指定元素的类名。
63
+ * 如果元素已包含该类名,则移除它;否则添加它。
64
+ * @param el - 要操作的 HTML 元素。
65
+ * @param clazz - 要切换的类名。
66
66
  */
67
- export function transition(element, value) {
68
- vendorPrefix.forEach((prefix) => {
69
- let t = prefix === "" ? "transition" : prefix + "Transition";
70
- element.style[t] = value;
71
- });
72
- }
73
- /**
74
- * 为节点添加 transform 属性,包括浏览器前缀
75
- * @param {HTMLElement} element 需要添加 css transform 属性的节点
76
- * @param {string} value css transform 值
77
- */
78
- export function transform(element, value) {
79
- vendorPrefix.forEach((prefix) => {
80
- let t = prefix === "" ? "transform" : prefix + "Transform";
81
- element.style[t] = value;
82
- });
67
+ export function toggleClass(el, clazz) {
68
+ if (hasClass(el, clazz)) {
69
+ removeClass(el, clazz);
70
+ }
71
+ else {
72
+ addClass(el, clazz);
73
+ }
83
74
  }
84
75
  /**
85
76
  * 为节点添加事件处理
@@ -356,7 +347,7 @@ export function formatStyle(styleObj) {
356
347
  ]);
357
348
  * ```
358
349
  */
359
- export function startTransition(target, properties, duration) {
350
+ export function startTransition(target, properties, duration = "0.3s") {
360
351
  target.style.setProperty("display", "none");
361
352
  const trans = [];
362
353
  for (let i = 0, len = properties.length; i < len; i++) {
@@ -367,14 +358,12 @@ export function startTransition(target, properties, duration) {
367
358
  if (duration) {
368
359
  target.style.setProperty("transition", trans.join(", "));
369
360
  }
370
- queueMicrotask(() => {
371
- target.style.removeProperty("display");
372
- queueMicrotask(() => {
373
- for (let i = 0, len = properties.length; i < len; i++) {
374
- const rec = properties[i];
375
- target.style.removeProperty(rec[0]);
376
- }
377
- });
361
+ target.style.removeProperty("display");
362
+ requestAnimationFrame(() => {
363
+ for (let i = 0, len = properties.length; i < len; i++) {
364
+ const rec = properties[i];
365
+ target.style.removeProperty(rec[0]);
366
+ }
378
367
  });
379
368
  }
380
369
  /**
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.11.3",
71
+ "version": "0.11.4",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",