lyb-js 1.6.28 → 1.6.30

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.
@@ -23,12 +23,8 @@ export class LibJsResizeWatcher {
23
23
  if (mode === "h" || mode === "v")
24
24
  return;
25
25
  //初始化时绑定窗口 resize 事件
26
- window.addEventListener("resize", () => {
27
- requestAnimationFrame(this._handleResize);
28
- });
29
- window.addEventListener("orientationchange", () => {
30
- requestAnimationFrame(this._handleResize);
31
- });
26
+ window.addEventListener("resize", this._handleResize);
27
+ window.addEventListener("orientationchange", this._handleResize);
32
28
  }
33
29
  /**
34
30
  * @description 注册一个窗口尺寸变化的监听器
@@ -41,17 +37,11 @@ export class LibJsResizeWatcher {
41
37
  const h = window.innerHeight;
42
38
  const orientation = w > h ? "h" : "v";
43
39
  if (this._mode === "h") {
44
- immediate &&
45
- requestAnimationFrame(() => {
46
- cb(1920, 1080, Math.min(w / 1920, h / 1080));
47
- });
40
+ immediate && cb(1920, 1080, Math.min(w / 1920, h / 1080));
48
41
  return () => { };
49
42
  }
50
43
  else if (this._mode === "v") {
51
- immediate &&
52
- requestAnimationFrame(() => {
53
- cb(1080, 1920, Math.min(w / 1080, h / 1920));
54
- });
44
+ immediate && cb(1080, 1920, Math.min(w / 1080, h / 1920));
55
45
  return () => { };
56
46
  }
57
47
  let s;
@@ -63,10 +53,7 @@ export class LibJsResizeWatcher {
63
53
  }
64
54
  const item = { cb, immediate };
65
55
  this._listeners.push(item);
66
- immediate &&
67
- requestAnimationFrame(() => {
68
- cb(window.innerWidth, window.innerHeight, s);
69
- });
56
+ immediate && cb(window.innerWidth, window.innerHeight, s);
70
57
  return () => {
71
58
  this._listeners = this._listeners.filter((l) => l !== item);
72
59
  };
@@ -74,10 +61,6 @@ export class LibJsResizeWatcher {
74
61
  /** 新增方法:通过id注册监听器(若id已存在会先移除旧的) */
75
62
  onById(id, cb, immediate = true) {
76
63
  this.offById(id);
77
- const remove = this.on(cb, immediate);
78
- this._listenerMap.set(id, cb);
79
- // 存储移除函数
80
- this._listenerMap.set(`${id}_remove`, remove);
81
64
  }
82
65
  /** 通过id移除监听器 */
83
66
  offById(...ids) {
@@ -1,4 +1,5 @@
1
- export declare const libJsCountdown: (endTime: number) => {
1
+ /** @description 倒计时,keepUnit 为保留单位,不再使用其上的进制 */
2
+ export declare const libJsCountdown: (endTime: number, keepUnit: "year" | "month" | "day" | "hour" | "minute" | "second") => {
2
3
  years: string;
3
4
  months: string;
4
5
  days: string;
@@ -1,18 +1,49 @@
1
1
  import dayjs from "dayjs";
2
2
  import duration from "dayjs/plugin/duration";
3
3
  dayjs.extend(duration);
4
- export const libJsCountdown = (endTime) => {
4
+ /** @description 倒计时,keepUnit 为保留单位,不再使用其上的进制 */
5
+ export const libJsCountdown = (endTime, keepUnit) => {
5
6
  const startTime = dayjs();
6
7
  const diff = dayjs(endTime).diff(startTime);
7
8
  const time = dayjs.duration(diff);
8
9
  const pad = (n) => n.toString().padStart(2, "0");
10
+ if (diff <= 0) {
11
+ return {
12
+ years: "00",
13
+ months: "00",
14
+ days: "00",
15
+ hours: "00",
16
+ minutes: "00",
17
+ seconds: "00",
18
+ ended: true,
19
+ };
20
+ }
21
+ // 先获取原始值
22
+ const years = time.years();
23
+ const months = time.months();
24
+ const days = time.days();
25
+ let hours = time.hours();
26
+ let minutes = time.minutes();
27
+ let seconds = time.seconds();
28
+ // 将整个时长打平成秒
29
+ const totalSec = time.asSeconds();
30
+ // 根据保留单位展开
31
+ if (keepUnit === "hour") {
32
+ hours = Math.floor(totalSec / 3600); // total hours
33
+ minutes = Math.floor((totalSec % 3600) / 60);
34
+ seconds = Math.floor(totalSec % 60);
35
+ }
36
+ else if (keepUnit === "minute") {
37
+ minutes = Math.floor(totalSec / 60); // total minutes
38
+ seconds = Math.floor(totalSec % 60);
39
+ }
9
40
  return {
10
- years: diff <= 0 ? "00" : pad(time.years()),
11
- months: diff <= 0 ? "00" : pad(time.months()),
12
- days: diff <= 0 ? "00" : pad(time.days()),
13
- hours: diff <= 0 ? "00" : pad(time.hours()),
14
- minutes: diff <= 0 ? "00" : pad(time.minutes()),
15
- seconds: diff <= 0 ? "00" : pad(time.seconds()),
16
- ended: diff <= 0,
41
+ years: pad(years),
42
+ months: pad(months),
43
+ days: pad(days),
44
+ hours: pad(hours),
45
+ minutes: pad(minutes),
46
+ seconds: pad(seconds),
47
+ ended: false,
17
48
  };
18
49
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.28",
3
+ "version": "1.6.30",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",