* 开始监听屏幕尺寸变化
(element: HTMLElement)
| 30 | * 开始监听屏幕尺寸变化 |
| 31 | */ |
| 32 | startWatching(element: HTMLElement): void { |
| 33 | this.observedElement = element; |
| 34 | this.updateDeviceInfo(); |
| 35 | |
| 36 | // 使用 ResizeObserver 监听元素尺寸变化 |
| 37 | this.resizeObserver = new ResizeObserver(() => { |
| 38 | const oldIsMobile = this.isMobileView; |
| 39 | const oldIsSmallScreen = this.isSmallScreen; |
| 40 | |
| 41 | this.updateDeviceInfo(); |
| 42 | |
| 43 | // 如果设备信息发生变化,触发回调 |
| 44 | if (oldIsMobile !== this.isMobileView || oldIsSmallScreen !== this.isSmallScreen) { |
| 45 | if (this.onDeviceChangeCallback) { |
| 46 | this.onDeviceChangeCallback(this.getDeviceInfo()); |
| 47 | } |
| 48 | } |
| 49 | }); |
| 50 | |
| 51 | this.resizeObserver.observe(element); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * 停止监听 |
no test coverage detected