@whitesev/utils 2.2.4 → 2.2.6

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.
@@ -430,6 +430,20 @@ declare class Utils {
430
430
  * > 456
431
431
  */
432
432
  getMaxValue(val: UtilsOwnObject<number>, handler: (key: any, value: any) => number): number;
433
+ /**
434
+ * 获取页面中最大的z-index的元素信息
435
+ * @param deviation 获取最大的z-index值的偏移,默认是+1
436
+ * @example
437
+ * Utils.getMaxZIndexNodeInfo();
438
+ * > {
439
+ * node: ...,
440
+ * zIndex: 1001
441
+ * }
442
+ **/
443
+ getMaxZIndexNodeInfo(deviation?: number): {
444
+ node: Element;
445
+ zIndex: number;
446
+ };
433
447
  /**
434
448
  * 获取页面中最大的z-index
435
449
  * @param deviation 获取最大的z-index值的偏移,默认是+1
@@ -1034,6 +1048,10 @@ declare class Utils {
1034
1048
  * @param target 目标元素
1035
1049
  * @param callback 触发的回调
1036
1050
  * @param options 观察器配置
1051
+ * @example
1052
+ * Utils.mutationVisible(document.querySelector("div.xxxx"),(entries,observer)=>{
1053
+ * console.log("该元素出现在视图内");
1054
+ * }))
1037
1055
  */
1038
1056
  mutationVisible(target: Element | Element[], callback: (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void, options?: IntersectionObserverInit): void;
1039
1057
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "2.2.4",
3
+ "version": "2.2.6",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -44,6 +44,7 @@
44
44
  "typescript": "^5.5.4"
45
45
  },
46
46
  "scripts": {
47
- "build": "rollup --config"
47
+ "build": "rollup --config",
48
+ "build:all": "rollup --config"
48
49
  }
49
50
  }
package/src/Utils.ts CHANGED
@@ -52,7 +52,7 @@ class Utils {
52
52
  this.windowApi = new WindowApi(option);
53
53
  }
54
54
  /** 版本号 */
55
- version = "2024.9.4";
55
+ version = "2024.9.10";
56
56
 
57
57
  /**
58
58
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -1225,43 +1225,90 @@ class Utils {
1225
1225
  }
1226
1226
  }
1227
1227
  /**
1228
- * 获取页面中最大的z-index
1228
+ * 获取页面中最大的z-index的元素信息
1229
1229
  * @param deviation 获取最大的z-index值的偏移,默认是+1
1230
1230
  * @example
1231
- * Utils.getMaxZIndex();
1232
- * > 1001
1231
+ * Utils.getMaxZIndexNodeInfo();
1232
+ * > {
1233
+ * node: ...,
1234
+ * zIndex: 1001
1235
+ * }
1233
1236
  **/
1234
- getMaxZIndex(deviation?: number): number;
1235
- getMaxZIndex(deviation = 1): number {
1237
+ getMaxZIndexNodeInfo(deviation?: number): {
1238
+ node: Element;
1239
+ zIndex: number;
1240
+ };
1241
+ getMaxZIndexNodeInfo(deviation = 1): {
1242
+ node: Element;
1243
+ zIndex: number;
1244
+ } {
1236
1245
  deviation = Number.isNaN(deviation) ? 1 : deviation;
1246
+ const UtilsContext = this;
1237
1247
  // 最大值2147483647
1238
- let maxZIndex = Math.pow(2, 31) - 1;
1248
+ const maxZIndex = Math.pow(2, 31) - 1;
1239
1249
  // 比较值2000000000
1240
- let maxZIndexCompare = 2 * Math.pow(10, 9);
1250
+ const maxZIndexCompare = 2 * Math.pow(10, 9);
1241
1251
  // 当前页面最大的z-index
1242
1252
  let zIndex = 0;
1243
1253
  // 当前的最大z-index的元素,调试使用
1244
1254
  // @ts-ignore
1245
- let maxZIndexNode = null;
1246
- this.windowApi.document.querySelectorAll("*").forEach(($ele, index) => {
1247
- let nodeStyle = this.windowApi.window.getComputedStyle($ele);
1255
+ let maxZIndexNode: Element = null;
1256
+ /**
1257
+ * 元素是否可见
1258
+ * @param $css
1259
+ */
1260
+ function isVisibleNode($css: CSSStyleDeclaration): boolean {
1261
+ return $css.position !== "static" && $css.display !== "none";
1262
+ }
1263
+ /**
1264
+ * 查询元素的z-index
1265
+ * 并比较值是否是已获取的最大值
1266
+ * @param $ele
1267
+ */
1268
+ function queryMaxZIndex($ele: Element) {
1269
+ /** 元素的样式 */
1270
+ const nodeStyle = UtilsContext.windowApi.window.getComputedStyle($ele);
1248
1271
  /* 不对position为static和display为none的元素进行获取它们的z-index */
1249
- if (nodeStyle.position !== "static" && nodeStyle.display !== "none") {
1272
+ if (isVisibleNode(nodeStyle)) {
1250
1273
  let nodeZIndex = parseInt(nodeStyle.zIndex);
1251
1274
  if (!isNaN(nodeZIndex)) {
1252
1275
  if (nodeZIndex > zIndex) {
1276
+ // 赋值到全局
1253
1277
  zIndex = nodeZIndex;
1254
1278
  maxZIndexNode = $ele;
1255
1279
  }
1256
1280
  }
1281
+ // 判断shadowRoot
1282
+ if ($ele.shadowRoot != null && $ele instanceof ShadowRoot) {
1283
+ $ele.shadowRoot.querySelectorAll("*").forEach(($shadowEle) => {
1284
+ queryMaxZIndex($shadowEle);
1285
+ });
1286
+ }
1257
1287
  }
1288
+ }
1289
+ this.windowApi.document.querySelectorAll("*").forEach(($ele, index) => {
1290
+ queryMaxZIndex($ele);
1258
1291
  });
1259
1292
  zIndex += deviation;
1260
1293
  if (zIndex >= maxZIndexCompare) {
1261
1294
  // 最好不要超过最大值
1262
1295
  zIndex = maxZIndex;
1263
1296
  }
1264
- return zIndex;
1297
+ return {
1298
+ node: maxZIndexNode,
1299
+ zIndex: zIndex,
1300
+ };
1301
+ }
1302
+ /**
1303
+ * 获取页面中最大的z-index
1304
+ * @param deviation 获取最大的z-index值的偏移,默认是+1
1305
+ * @example
1306
+ * Utils.getMaxZIndex();
1307
+ * > 1001
1308
+ **/
1309
+ getMaxZIndex(deviation?: number): number;
1310
+ getMaxZIndex(deviation = 1): number {
1311
+ return this.getMaxZIndexNodeInfo(deviation).zIndex;
1265
1312
  }
1266
1313
  /**
1267
1314
  * 获取最小值
@@ -2524,6 +2571,10 @@ class Utils {
2524
2571
  * @param target 目标元素
2525
2572
  * @param callback 触发的回调
2526
2573
  * @param options 观察器配置
2574
+ * @example
2575
+ * Utils.mutationVisible(document.querySelector("div.xxxx"),(entries,observer)=>{
2576
+ * console.log("该元素出现在视图内");
2577
+ * }))
2527
2578
  */
2528
2579
  mutationVisible(
2529
2580
  target: Element | Element[],