@whitesev/utils 2.2.2 → 2.2.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.
@@ -1029,6 +1029,13 @@ declare class Utils {
1029
1029
  */
1030
1030
  callback: MutationCallback;
1031
1031
  }): MutationObserver;
1032
+ /**
1033
+ * 使用观察器观察元素出现在视图内,出现的话触发回调
1034
+ * @param target 目标元素
1035
+ * @param callback 触发的回调
1036
+ * @param options 观察器配置
1037
+ */
1038
+ mutationVisible(target: Element | Element[], callback: (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void, options?: IntersectionObserverInit): void;
1032
1039
  /**
1033
1040
  * 去除全局window下的Utils,返回控制权
1034
1041
  * @example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
package/src/Utils.ts CHANGED
@@ -2519,6 +2519,47 @@ class Utils {
2519
2519
  }
2520
2520
  return mutationObserver;
2521
2521
  }
2522
+ /**
2523
+ * 使用观察器观察元素出现在视图内,出现的话触发回调
2524
+ * @param target 目标元素
2525
+ * @param callback 触发的回调
2526
+ * @param options 观察器配置
2527
+ */
2528
+ mutationVisible(
2529
+ target: Element | Element[],
2530
+ callback: (
2531
+ entries: IntersectionObserverEntry[],
2532
+ observer: IntersectionObserver
2533
+ ) => void,
2534
+ options?: IntersectionObserverInit
2535
+ ) {
2536
+ if (typeof IntersectionObserver === "undefined") {
2537
+ throw new TypeError("IntersectionObserver is not defined");
2538
+ }
2539
+ if (target == null) {
2540
+ throw new TypeError("mutatuinVisible target is null");
2541
+ }
2542
+ let defaultOptions: IntersectionObserverInit = {
2543
+ root: null,
2544
+ rootMargin: "0px 0px 0px 0px",
2545
+ threshold: [0.01, 0.99],
2546
+ };
2547
+ defaultOptions = this.assign(defaultOptions, options || {});
2548
+ let intersectionObserver = new IntersectionObserver((entries, observer) => {
2549
+ if (entries[0].isIntersecting) {
2550
+ if (typeof callback === "function") {
2551
+ callback(entries, observer);
2552
+ }
2553
+ }
2554
+ }, defaultOptions);
2555
+ if (Array.isArray(target)) {
2556
+ target.forEach((item) => {
2557
+ intersectionObserver.observe(item);
2558
+ });
2559
+ } else {
2560
+ intersectionObserver.observe(target);
2561
+ }
2562
+ }
2522
2563
  /**
2523
2564
  * 去除全局window下的Utils,返回控制权
2524
2565
  * @example