|
|
@@ -1,627 +1,190 @@
|
|
1
|
|
-import { Component, Input, Output, EventEmitter, AfterViewInit, OnDestroy, Renderer2, ElementRef, signal, computed, effect } from '@angular/core';
|
|
|
1
|
+import { Component, Input, Output, EventEmitter, OnInit, OnDestroy, ElementRef, AfterViewInit, NgZone } from '@angular/core';
|
|
2
|
2
|
import { CommonModule } from '@angular/common';
|
|
3
|
|
-import { MatButtonModule } from '@angular/material/button';
|
|
4
|
|
-import { MatIconModule } from '@angular/material/icon';
|
|
5
|
|
-import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
|
6
|
|
-
|
|
7
|
|
-export interface StickyHeaderButton {
|
|
8
|
|
- title: string;
|
|
9
|
|
- name: string;
|
|
10
|
|
- icon?: string;
|
|
11
|
|
- color?: 'primary' | 'accent' | 'warn';
|
|
12
|
|
- disabled?: boolean;
|
|
13
|
|
- loading?: boolean;
|
|
14
|
|
-}
|
|
15
|
|
-
|
|
16
|
|
-export interface DynamicSection {
|
|
17
|
|
- id: number;
|
|
18
|
|
- content: string;
|
|
19
|
|
- style?: 'hint' | 'debug-info' | 'debug-urls' | 'custom';
|
|
20
|
|
- customClass?: string;
|
|
21
|
|
-}
|
|
22
|
3
|
|
|
23
|
4
|
@Component({
|
|
24
|
5
|
selector: 'app-sticky-header',
|
|
25
|
6
|
standalone: true,
|
|
26
|
|
- imports: [CommonModule, MatButtonModule, MatIconModule, MatProgressSpinnerModule],
|
|
|
7
|
+ imports: [CommonModule],
|
|
27
|
8
|
templateUrl: './sticky-header.component.html',
|
|
28
|
9
|
styleUrl: './sticky-header.component.scss'
|
|
29
|
10
|
})
|
|
30
|
|
-export class StickyHeaderComponent implements AfterViewInit, OnDestroy {
|
|
|
11
|
+export class StickyHeaderComponent implements OnInit, OnDestroy, AfterViewInit {
|
|
|
12
|
+ /** CSS selector for scroll container. If not provided, will find nearest scrollable parent or window. */
|
|
|
13
|
+ @Input() scrollContainer?: string;
|
|
|
14
|
+ /** Collapse threshold in pixels. Default 50px */
|
|
|
15
|
+ @Input() collapseThreshold = 50;
|
|
|
16
|
+ /** Enable/disable collapse behavior. Default true */
|
|
|
17
|
+ @Input() enabled = true;
|
|
|
18
|
+ /** Emits scroll ratio (0-1) on scroll */
|
|
|
19
|
+ @Output() scrollRatioChange = new EventEmitter<number>();
|
|
|
20
|
+
|
|
|
21
|
+ private scrollListener?: () => void;
|
|
|
22
|
+ private scrollContainerElement?: HTMLElement | Window;
|
|
|
23
|
+ private isCollapsed = false;
|
|
|
24
|
+
|
|
31
|
25
|
constructor(
|
|
32
|
|
- private renderer: Renderer2,
|
|
33
|
|
- private elementRef: ElementRef
|
|
|
26
|
+ private elementRef: ElementRef,
|
|
|
27
|
+ private ngZone: NgZone
|
|
34
|
28
|
) {}
|
|
35
|
29
|
|
|
36
|
|
- // 滚动优化相关属性
|
|
37
|
|
- private scrollRafId: number | null = null;
|
|
38
|
|
- private lastScrollTop = 0;
|
|
39
|
|
-
|
|
40
|
|
- // 尺寸缓存
|
|
41
|
|
- private initialHeight = 0;
|
|
42
|
|
- private initialTop = 0;
|
|
43
|
|
- private maxHeight = 0; // header最大高度
|
|
44
|
|
- private distanceFromTop = 0; // header距离文档顶部距离
|
|
45
|
|
- private minHeight = 40; // 最小高度(与CSS中compact状态一致)
|
|
46
|
|
- private resizeObserver: ResizeObserver | null = null;
|
|
47
|
|
- private currentScrollContainer: HTMLElement | null = null;
|
|
48
|
|
-
|
|
49
|
|
- // 滚动检测和状态缓存
|
|
50
|
|
- private scrollListener: (() => void) | null = null;
|
|
51
|
|
- private isAutoDetecting = false;
|
|
52
|
|
- private lastIsLocked = false; // 状态变化检测
|
|
53
|
|
- private lastIsCompact = false;
|
|
54
|
|
-
|
|
55
|
|
- /** 标题文本 */
|
|
56
|
|
- @Input() title = '';
|
|
57
|
|
-
|
|
58
|
|
- /** 按钮配置数组 */
|
|
59
|
|
- @Input() buttons: StickyHeaderButton[] = [];
|
|
60
|
|
-
|
|
61
|
|
- /** 是否启用自动滚动检测 */
|
|
62
|
|
- @Input() autoDetect = false;
|
|
63
|
|
-
|
|
64
|
|
- /** 滚动容器选择器,默认为'.content-area',设为'window'监听窗口滚动 */
|
|
65
|
|
- @Input() scrollContainer = '.content-area';
|
|
66
|
|
-
|
|
67
|
|
- /** 宽度参考目标选择器(用于锁定状态宽度匹配),如未设置则自动检测 */
|
|
68
|
|
- @Input() widthTarget: string | null = null;
|
|
69
|
|
-
|
|
70
|
|
- /** 按钮点击事件 */
|
|
71
|
|
- @Output() buttonAction = new EventEmitter<string>();
|
|
72
|
|
-
|
|
73
|
|
- // 信号状态
|
|
74
|
|
- isScrolled = signal(false);
|
|
75
|
|
- isLocked = signal(false);
|
|
76
|
|
- isCompact = signal(false);
|
|
77
|
|
- currentHeight = signal<number | null>(null); // 动态高度
|
|
78
|
|
- headerWidth = signal<string | number | null>(null);
|
|
79
|
|
- headerLeft = signal<string | number | null>(null);
|
|
80
|
|
- dynamicSections = signal<DynamicSection[]>([]);
|
|
81
|
|
-
|
|
82
|
|
- // 计算属性:当前压缩比例和高度
|
|
83
|
|
- compressionRatio = computed(() => {
|
|
84
|
|
- if (!this.isLocked()) return 0;
|
|
85
|
|
- const height = this.currentHeight();
|
|
86
|
|
- if (height === null || this.maxHeight <= this.minHeight) return 0;
|
|
87
|
|
-
|
|
88
|
|
- // 计算压缩比例:1 - (当前高度 - 最小高度) / (最大高度 - 最小高度)
|
|
89
|
|
- const ratio = 1 - (height - this.minHeight) / (this.maxHeight - this.minHeight);
|
|
90
|
|
- return Math.max(0, Math.min(1, ratio)); // 限制在0-1之间
|
|
91
|
|
- });
|
|
92
|
|
-
|
|
93
|
|
- // 添加动态区域的方法
|
|
94
|
|
- add(index: number, content: string, style?: string, customClass?: string): void {
|
|
95
|
|
- const sections = this.dynamicSections();
|
|
96
|
|
- const existingIndex = sections.findIndex(s => s.id === index);
|
|
97
|
|
-
|
|
98
|
|
- const newSection: DynamicSection = {
|
|
99
|
|
- id: index,
|
|
100
|
|
- content,
|
|
101
|
|
- style: style as any || 'hint',
|
|
102
|
|
- customClass
|
|
103
|
|
- };
|
|
104
|
|
-
|
|
105
|
|
- if (existingIndex >= 0) {
|
|
106
|
|
- const updated = [...sections];
|
|
107
|
|
- updated[existingIndex] = newSection;
|
|
108
|
|
- this.dynamicSections.set(updated);
|
|
109
|
|
- } else {
|
|
110
|
|
- this.dynamicSections.set([...sections, newSection].sort((a, b) => a.id - b.id));
|
|
111
|
|
- }
|
|
112
|
|
- }
|
|
113
|
|
-
|
|
114
|
|
- // 移除动态区域
|
|
115
|
|
- remove(index: number): void {
|
|
116
|
|
- this.dynamicSections.set(this.dynamicSections().filter(s => s.id !== index));
|
|
117
|
|
- }
|
|
118
|
|
-
|
|
119
|
|
- // 清除所有动态区域
|
|
120
|
|
- clear(): void {
|
|
121
|
|
- this.dynamicSections.set([]);
|
|
122
|
|
- }
|
|
123
|
|
-
|
|
124
|
|
- // 获取渲染内容(支持变量绑定)
|
|
125
|
|
- getRenderedContent(section: DynamicSection): string {
|
|
126
|
|
- const content = section.content;
|
|
127
|
|
- // 简单变量绑定:{propertyName} 替换为组件属性值
|
|
128
|
|
- return content.replace(/\{(\w+)\}/g, (match, key) => {
|
|
129
|
|
- // 从组件实例中获取值
|
|
130
|
|
- const value = (this as any)[key];
|
|
131
|
|
- return value !== undefined ? String(value) : match;
|
|
132
|
|
- });
|
|
133
|
|
- }
|
|
134
|
|
-
|
|
135
|
|
- onButtonClick(name: string) {
|
|
136
|
|
- const button = this.buttons.find(b => b.name === name);
|
|
137
|
|
- if (button && !button.disabled && !button.loading) {
|
|
138
|
|
- this.buttonAction.emit(name);
|
|
139
|
|
- }
|
|
140
|
|
- }
|
|
|
30
|
+ ngOnInit() {}
|
|
141
|
31
|
|
|
142
|
32
|
ngAfterViewInit() {
|
|
143
|
|
- if (this.autoDetect) {
|
|
144
|
|
- this.setupAutoScrollDetection();
|
|
145
|
|
- }
|
|
|
33
|
+ console.log('StickyHeader: 视图初始化完成');
|
|
|
34
|
+ this.setupScrollDetection();
|
|
146
|
35
|
}
|
|
147
|
36
|
|
|
148
|
37
|
ngOnDestroy() {
|
|
149
|
|
- this.cleanupScrollListener();
|
|
150
|
|
- if (this.scrollRafId) {
|
|
151
|
|
- cancelAnimationFrame(this.scrollRafId);
|
|
152
|
|
- this.scrollRafId = null;
|
|
153
|
|
- }
|
|
154
|
|
- if (this.resizeObserver) {
|
|
155
|
|
- this.resizeObserver.disconnect();
|
|
156
|
|
- this.resizeObserver = null;
|
|
157
|
|
- }
|
|
|
38
|
+ this.cleanupScrollDetection();
|
|
158
|
39
|
}
|
|
159
|
40
|
|
|
160
|
|
- private setupAutoScrollDetection() {
|
|
161
|
|
- if (this.isAutoDetecting) return;
|
|
162
|
|
-
|
|
163
|
|
- this.isAutoDetecting = true;
|
|
164
|
|
-
|
|
165
|
|
- const container = this.getScrollContainer();
|
|
166
|
|
- const isWindowScroll = this.scrollContainer === 'window';
|
|
167
|
|
- this.currentScrollContainer = isWindowScroll ? null : container;
|
|
168
|
|
-
|
|
169
|
|
- // 初始化尺寸缓存(根据滚动类型传递正确的容器)
|
|
170
|
|
- this.cacheInitialDimensions(this.currentScrollContainer || undefined);
|
|
171
|
|
- this.setupResizeObserver();
|
|
172
|
|
-
|
|
173
|
|
- if (isWindowScroll) {
|
|
174
|
|
- console.log(`sticky-header: 启用窗口滚动检测`);
|
|
175
|
|
-
|
|
176
|
|
- this.scrollListener = this.renderer.listen(
|
|
177
|
|
- 'window',
|
|
178
|
|
- 'scroll',
|
|
179
|
|
- this.handleWindowScroll.bind(this)
|
|
180
|
|
- );
|
|
181
|
|
-
|
|
182
|
|
- // 初始检查窗口滚动位置
|
|
183
|
|
- this.checkInitialWindowScrollPosition();
|
|
184
|
|
- } else if (container) {
|
|
185
|
|
- console.log(`sticky-header: 启用容器滚动检测,容器: ${this.scrollContainer}`);
|
|
186
|
|
-
|
|
187
|
|
- this.scrollListener = this.renderer.listen(
|
|
188
|
|
- container,
|
|
189
|
|
- 'scroll',
|
|
190
|
|
- this.handleScroll.bind(this)
|
|
191
|
|
- );
|
|
192
|
|
-
|
|
193
|
|
- // 初始检查滚动位置
|
|
194
|
|
- this.checkInitialScrollPosition(container);
|
|
|
41
|
+ private setupScrollDetection() {
|
|
|
42
|
+ console.log('StickyHeader: 开始设置滚动检测');
|
|
|
43
|
+ this.findScrollContainer();
|
|
|
44
|
+ if (!this.scrollContainerElement) {
|
|
|
45
|
+ console.warn('StickyHeader: 未找到滚动容器,回退到window');
|
|
|
46
|
+ this.scrollContainerElement = window;
|
|
195
|
47
|
} else {
|
|
196
|
|
- console.warn('sticky-header: 未找到滚动容器:', this.scrollContainer);
|
|
|
48
|
+ console.log('StickyHeader: 使用滚动容器:', this.scrollContainerElement);
|
|
197
|
49
|
}
|
|
|
50
|
+ this.attachScrollListener();
|
|
198
|
51
|
}
|
|
199
|
52
|
|
|
200
|
|
- private getScrollContainer(): HTMLElement | null {
|
|
201
|
|
- if (this.scrollContainer === 'window') {
|
|
202
|
|
- return null;
|
|
203
|
|
- }
|
|
204
|
|
-
|
|
205
|
|
- // 从当前元素向上查找最近的匹配容器
|
|
206
|
|
- const container = this.elementRef.nativeElement.closest(this.scrollContainer);
|
|
207
|
|
- if (container) {
|
|
208
|
|
- return container;
|
|
209
|
|
- }
|
|
|
53
|
+ private findScrollContainer() {
|
|
|
54
|
+ console.log('StickyHeader: 查找滚动容器,选择器:', this.scrollContainer);
|
|
210
|
55
|
|
|
211
|
|
- // 全局查找
|
|
212
|
|
- return document.querySelector(this.scrollContainer) as HTMLElement;
|
|
213
|
|
- }
|
|
214
|
|
-
|
|
215
|
|
- /**
|
|
216
|
|
- * 自动获取宽度参考目标元素
|
|
217
|
|
- * 优先级:1. widthTarget设置 2. 滚动容器 3. 自动查找合适父级容器
|
|
218
|
|
- */
|
|
219
|
|
- private getAutoWidthTarget(): HTMLElement | null {
|
|
220
|
|
- // 情况1: widthTarget已设置,使用指定元素
|
|
221
|
|
- if (this.widthTarget) {
|
|
222
|
|
- const target = document.querySelector(this.widthTarget) as HTMLElement;
|
|
223
|
|
- if (target) {
|
|
224
|
|
- console.log(`sticky-header: 使用指定宽度目标: ${this.widthTarget}`);
|
|
225
|
|
- return target;
|
|
|
56
|
+ if (this.scrollContainer) {
|
|
|
57
|
+ const el = document.querySelector(this.scrollContainer);
|
|
|
58
|
+ console.log('StickyHeader: 找到元素:', el);
|
|
|
59
|
+ if (el && this.isScrollable(el)) {
|
|
|
60
|
+ this.scrollContainerElement = el as HTMLElement;
|
|
|
61
|
+ console.log('StickyHeader: 使用指定容器');
|
|
|
62
|
+ return;
|
|
|
63
|
+ } else {
|
|
|
64
|
+ console.warn(`StickyHeader: 选择器 "${this.scrollContainer}" 未找到或不可滚动`);
|
|
226
|
65
|
}
|
|
227
|
|
- console.warn(`sticky-header: 未找到宽度目标元素: ${this.widthTarget}`);
|
|
228
|
66
|
}
|
|
229
|
|
-
|
|
230
|
|
- // 情况2: 容器滚动模式,使用滚动容器
|
|
231
|
|
- if (this.scrollContainer !== 'window') {
|
|
232
|
|
- const container = this.getScrollContainer();
|
|
233
|
|
- if (container) {
|
|
234
|
|
- console.log(`sticky-header: 自动使用滚动容器作为宽度目标`);
|
|
235
|
|
- return container;
|
|
|
67
|
+ // Find nearest scrollable parent
|
|
|
68
|
+ console.log('StickyHeader: 向上查找可滚动父元素');
|
|
|
69
|
+ let parent = this.elementRef.nativeElement.parentElement;
|
|
|
70
|
+ while (parent && parent !== document.body) {
|
|
|
71
|
+ if (this.isScrollable(parent)) {
|
|
|
72
|
+ console.log('StickyHeader: 找到可滚动父元素:', parent);
|
|
|
73
|
+ this.scrollContainerElement = parent;
|
|
|
74
|
+ return;
|
|
236
|
75
|
}
|
|
|
76
|
+ parent = parent.parentElement;
|
|
237
|
77
|
}
|
|
238
|
|
-
|
|
239
|
|
- // 情况3: 窗口滚动模式,自动查找合适的父级容器
|
|
240
|
|
- return this.findSuitableParentContainer();
|
|
|
78
|
+ console.log('StickyHeader: 未找到可滚动容器,将回退到window');
|
|
241
|
79
|
}
|
|
242
|
80
|
|
|
243
|
|
- /**
|
|
244
|
|
- * 查找合适的父级容器(用于窗口滚动模式)
|
|
245
|
|
- */
|
|
246
|
|
- private findSuitableParentContainer(): HTMLElement | null {
|
|
247
|
|
- // 算法1: 查找最近的滚动容器祖先
|
|
248
|
|
- const scrollContainer = this.findClosestScrollableAncestor();
|
|
249
|
|
- if (scrollContainer) return scrollContainer;
|
|
250
|
|
-
|
|
251
|
|
- // 算法2: 查找常见的内容区域选择器
|
|
252
|
|
- const commonSelectors = [
|
|
253
|
|
- '.content-area', // ng-configure应用结构
|
|
254
|
|
- '.flex-1.overflow-auto', // 常见flex布局
|
|
255
|
|
- 'div[class*="content"]',
|
|
256
|
|
- 'div[class*="main"]',
|
|
257
|
|
- 'div[class*="container"]'
|
|
258
|
|
- ];
|
|
259
|
|
-
|
|
260
|
|
- for (const selector of commonSelectors) {
|
|
261
|
|
- const element = document.querySelector(selector) as HTMLElement;
|
|
262
|
|
- if (element && this.elementRef.nativeElement.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_CONTAINS) {
|
|
263
|
|
- console.log(`sticky-header: 找到内容容器: ${selector}`);
|
|
264
|
|
- return element;
|
|
265
|
|
- }
|
|
|
81
|
+ private isScrollable(element: Element): boolean {
|
|
|
82
|
+ if (element === document.documentElement || element === document.body) {
|
|
|
83
|
+ console.log('StickyHeader: 元素是documentElement或body,返回true');
|
|
|
84
|
+ return true; // Window scrolling
|
|
266
|
85
|
}
|
|
267
|
86
|
|
|
268
|
|
- // 算法3: 回退到视口
|
|
269
|
|
- console.log('sticky-header: 未找到合适容器,使用视口宽度');
|
|
270
|
|
- return null; // null表示使用视口
|
|
271
|
|
- }
|
|
272
|
|
-
|
|
273
|
|
- /**
|
|
274
|
|
- * 查找最近的可滚动祖先容器
|
|
275
|
|
- */
|
|
276
|
|
- private findClosestScrollableAncestor(): HTMLElement | null {
|
|
277
|
|
- let element = this.elementRef.nativeElement.parentElement;
|
|
278
|
|
-
|
|
279
|
|
- while (element && element !== document.body) {
|
|
280
|
|
- const style = window.getComputedStyle(element);
|
|
281
|
|
- const overflow = style.overflow + style.overflowY + style.overflowX;
|
|
282
|
|
-
|
|
283
|
|
- if (overflow.includes('auto') || overflow.includes('scroll')) {
|
|
284
|
|
- console.log(`sticky-header: 找到可滚动祖先容器`);
|
|
285
|
|
- return element;
|
|
286
|
|
- }
|
|
287
|
|
-
|
|
288
|
|
- element = element.parentElement;
|
|
289
|
|
- }
|
|
|
87
|
+ const style = window.getComputedStyle(element);
|
|
|
88
|
+ const overflow = style.overflow + style.overflowY + style.overflowX;
|
|
|
89
|
+ const isOverflowScrollable = /auto|scroll/.test(overflow);
|
|
|
90
|
+ const hasScrollableContent = element.scrollHeight > element.clientHeight;
|
|
290
|
91
|
|
|
291
|
|
- return null;
|
|
292
|
|
- }
|
|
293
|
|
-
|
|
294
|
|
- private cacheInitialDimensions(container?: HTMLElement): void {
|
|
295
|
|
- const element = this.elementRef.nativeElement;
|
|
296
|
|
- const rect = element.getBoundingClientRect();
|
|
297
|
|
- this.initialHeight = rect.height;
|
|
298
|
|
- this.maxHeight = rect.height; // 设置最大高度
|
|
|
92
|
+ console.log('StickyHeader: 检查元素可滚动性:', {
|
|
|
93
|
+ element,
|
|
|
94
|
+ overflow,
|
|
|
95
|
+ isOverflowScrollable,
|
|
|
96
|
+ scrollHeight: element.scrollHeight,
|
|
|
97
|
+ clientHeight: element.clientHeight,
|
|
|
98
|
+ hasScrollableContent
|
|
|
99
|
+ });
|
|
299
|
100
|
|
|
300
|
|
- if (container) {
|
|
301
|
|
- // 容器滚动:计算相对于容器的偏移量
|
|
302
|
|
- const containerRect = container.getBoundingClientRect();
|
|
303
|
|
- this.initialTop = rect.top - containerRect.top + container.scrollTop;
|
|
304
|
|
- console.log(`sticky-header: 容器滚动尺寸缓存 - 高度: ${this.initialHeight}px, 最大高度: ${this.maxHeight}px, 相对容器顶部: ${this.initialTop}px, 容器scrollTop: ${container.scrollTop}`);
|
|
305
|
|
- } else if (typeof window !== 'undefined') {
|
|
306
|
|
- // 窗口滚动:计算绝对距离
|
|
307
|
|
- this.initialTop = rect.top + (window.scrollY || document.documentElement.scrollTop || 0);
|
|
308
|
|
- this.distanceFromTop = this.initialTop; // 保存距离文档顶部距离
|
|
309
|
|
- console.log(`sticky-header: 窗口滚动尺寸缓存 - 高度: ${this.initialHeight}px, 最大高度: ${this.maxHeight}px, 距离文档顶部: ${this.distanceFromTop}px`);
|
|
310
|
|
- } else {
|
|
311
|
|
- this.initialTop = rect.top;
|
|
312
|
|
- console.log(`sticky-header: 尺寸缓存(无window)- 高度: ${this.initialHeight}px, 最大高度: ${this.maxHeight}px, 距离顶部: ${this.initialTop}px`);
|
|
313
|
|
- }
|
|
|
101
|
+ // 放宽条件:只要设置了overflow: auto/scroll就认为是可滚动的
|
|
|
102
|
+ // 因为内容可能会动态加载(例如表格数据延迟加载)
|
|
|
103
|
+ return isOverflowScrollable;
|
|
314
|
104
|
}
|
|
315
|
105
|
|
|
316
|
|
- private setupResizeObserver(): void {
|
|
317
|
|
- if (typeof ResizeObserver === 'undefined') {
|
|
318
|
|
- console.warn('sticky-header: ResizeObserver 不支持,尺寸变化将不会自动更新');
|
|
|
106
|
+ private attachScrollListener() {
|
|
|
107
|
+ if (!this.scrollContainerElement) {
|
|
|
108
|
+ console.warn('StickyHeader: 无滚动容器,无法绑定滚动监听器');
|
|
319
|
109
|
return;
|
|
320
|
110
|
}
|
|
321
|
111
|
|
|
322
|
|
- this.resizeObserver = new ResizeObserver(() => {
|
|
323
|
|
- console.log('sticky-header: header尺寸变化,重新缓存');
|
|
324
|
|
- this.cacheInitialDimensions(this.currentScrollContainer || undefined);
|
|
|
112
|
+ console.log('StickyHeader: 绑定滚动监听器到容器:', this.scrollContainerElement);
|
|
|
113
|
+ this.ngZone.runOutsideAngular(() => {
|
|
|
114
|
+ const handler = () => {
|
|
|
115
|
+ this.handleScroll();
|
|
|
116
|
+ };
|
|
|
117
|
+ this.scrollListener = handler;
|
|
|
118
|
+ this.scrollContainerElement!.addEventListener('scroll', handler, { passive: true });
|
|
|
119
|
+ console.log('StickyHeader: 滚动监听器已绑定');
|
|
|
120
|
+ // Initial update
|
|
|
121
|
+ handler();
|
|
325
|
122
|
});
|
|
326
|
|
-
|
|
327
|
|
- this.resizeObserver.observe(this.elementRef.nativeElement);
|
|
328
|
|
- console.log('sticky-header: ResizeObserver 已启动');
|
|
329
|
123
|
}
|
|
330
|
124
|
|
|
331
|
|
- private handleWindowScroll() {
|
|
332
|
|
- // 使用requestAnimationFrame防抖
|
|
333
|
|
- if (this.scrollRafId) {
|
|
334
|
|
- cancelAnimationFrame(this.scrollRafId);
|
|
|
125
|
+ private handleScroll() {
|
|
|
126
|
+ if (!this.scrollContainerElement) {
|
|
|
127
|
+ console.log('StickyHeader: handleScroll被调用,但无滚动容器');
|
|
|
128
|
+ return;
|
|
335
|
129
|
}
|
|
336
|
|
-
|
|
337
|
|
- this.scrollRafId = requestAnimationFrame(() => {
|
|
338
|
|
- this.scrollRafId = null;
|
|
339
|
|
-
|
|
340
|
|
- const scrollTop = window.scrollY || document.documentElement.scrollTop || 0;
|
|
341
|
|
-
|
|
342
|
|
- // 检查滚动位置是否显著变化,避免微小滚动触发状态更新
|
|
343
|
|
- const scrollDelta = Math.abs(scrollTop - this.lastScrollTop);
|
|
344
|
|
- if (scrollDelta < 3) {
|
|
345
|
|
- return;
|
|
346
|
|
- }
|
|
347
|
|
-
|
|
348
|
|
- this.lastScrollTop = scrollTop;
|
|
349
|
|
-
|
|
350
|
|
- // 计算压缩状态
|
|
351
|
|
- const { ratio, height, isLocked, isCompact } = this.calculateCompression(scrollTop);
|
|
352
|
|
- const isScrolled = ratio > 0;
|
|
353
|
|
-
|
|
354
|
|
- // 检查状态或高度是否真正变化,避免不必要更新
|
|
355
|
|
- const currentHeightValue = this.currentHeight();
|
|
356
|
|
- const heightChanged = currentHeightValue === null || Math.abs(height - currentHeightValue) > 1;
|
|
357
|
|
- const stateChanged =
|
|
358
|
|
- isLocked !== this.lastIsLocked ||
|
|
359
|
|
- isCompact !== this.lastIsCompact ||
|
|
360
|
|
- isScrolled !== this.isScrolled();
|
|
361
|
|
-
|
|
362
|
|
- if (!stateChanged && !heightChanged) {
|
|
363
|
|
- console.log(`sticky-header: 窗口滚动位置变化但状态和高度未变,scrollTop=${scrollTop}, ratio=${ratio.toFixed(2)}, isLocked=${isLocked}, isCompact=${isCompact}, height=${height.toFixed(0)}px`);
|
|
364
|
|
- return;
|
|
365
|
|
- }
|
|
366
|
|
-
|
|
367
|
|
- this.lastIsLocked = isLocked;
|
|
368
|
|
- this.lastIsCompact = isCompact;
|
|
369
|
|
-
|
|
370
|
|
- // 更新状态
|
|
371
|
|
- this.currentHeight.set(height);
|
|
372
|
|
- this.isScrolled.set(isScrolled);
|
|
373
|
|
- this.isLocked.set(isLocked);
|
|
374
|
|
- this.isCompact.set(isCompact);
|
|
375
|
|
-
|
|
376
|
|
- console.log(`sticky-header: 窗口滚动状态更新,scrollTop=${scrollTop}, ratio=${ratio.toFixed(2)}, height=${height.toFixed(0)}px, isLocked=${isLocked}, isCompact=${isCompact}`);
|
|
377
|
|
-
|
|
378
|
|
- // 设置宽度匹配容器
|
|
379
|
|
- if (isLocked) {
|
|
380
|
|
- const widthTarget = this.getAutoWidthTarget();
|
|
381
|
|
- if (widthTarget) {
|
|
382
|
|
- const rect = widthTarget.getBoundingClientRect();
|
|
383
|
|
- this.headerWidth.set(rect.width);
|
|
384
|
|
- this.headerLeft.set(rect.left);
|
|
385
|
|
- console.log(`sticky-header: 窗口滚动锁定,使用自动宽度目标,宽度: ${rect.width}px, 左侧位置: ${rect.left}px`);
|
|
386
|
|
- } else {
|
|
387
|
|
- // 未找到合适容器,使用视口
|
|
388
|
|
- this.headerWidth.set('100%');
|
|
389
|
|
- this.headerLeft.set(0);
|
|
390
|
|
- console.log(`sticky-header: 窗口滚动锁定,未找到宽度目标,使用视口宽度`);
|
|
391
|
|
- }
|
|
392
|
|
- } else {
|
|
393
|
|
- this.headerWidth.set(null);
|
|
394
|
|
- this.headerLeft.set(null);
|
|
395
|
|
- }
|
|
396
|
|
- });
|
|
397
|
|
- }
|
|
398
|
130
|
|
|
399
|
|
- private checkInitialWindowScrollPosition() {
|
|
400
|
|
- const scrollTop = window.scrollY || document.documentElement.scrollTop || 0;
|
|
401
|
|
- this.lastScrollTop = scrollTop;
|
|
402
|
|
-
|
|
403
|
|
- const { ratio, height, isLocked, isCompact } = this.calculateCompression(scrollTop);
|
|
404
|
|
- const isScrolled = ratio > 0;
|
|
405
|
|
-
|
|
406
|
|
- this.currentHeight.set(height);
|
|
407
|
|
- this.isScrolled.set(isScrolled);
|
|
408
|
|
- this.isLocked.set(isLocked);
|
|
409
|
|
- this.isCompact.set(isCompact);
|
|
410
|
|
-
|
|
411
|
|
- if (isLocked) {
|
|
412
|
|
- const widthTarget = this.getAutoWidthTarget();
|
|
413
|
|
- if (widthTarget) {
|
|
414
|
|
- const rect = widthTarget.getBoundingClientRect();
|
|
415
|
|
- this.headerWidth.set(rect.width);
|
|
416
|
|
- this.headerLeft.set(rect.left);
|
|
417
|
|
- console.log(`sticky-header: 初始窗口滚动锁定,使用自动宽度目标,宽度: ${rect.width}px, 左侧位置: ${rect.left}px`);
|
|
418
|
|
- } else {
|
|
419
|
|
- this.headerWidth.set('100%');
|
|
420
|
|
- this.headerLeft.set(0);
|
|
421
|
|
- console.log(`sticky-header: 初始窗口滚动锁定,未找到宽度目标,使用视口宽度`);
|
|
422
|
|
- }
|
|
|
131
|
+ console.log('StickyHeader: 滚动事件触发');
|
|
|
132
|
+ const container = this.scrollContainerElement;
|
|
|
133
|
+ let scrollTop: number;
|
|
|
134
|
+
|
|
|
135
|
+ if (container === window) {
|
|
|
136
|
+ scrollTop = window.scrollY || document.documentElement.scrollTop;
|
|
|
137
|
+ console.log('StickyHeader: window滚动,scrollTop:', scrollTop);
|
|
|
138
|
+ } else {
|
|
|
139
|
+ const el = container as HTMLElement;
|
|
|
140
|
+ scrollTop = el.scrollTop;
|
|
|
141
|
+ console.log('StickyHeader: 容器滚动,scrollTop:', scrollTop, '容器:', el);
|
|
423
|
142
|
}
|
|
424
|
|
-
|
|
425
|
|
- console.log(`sticky-header: 初始窗口滚动位置检查完成: scrollTop=${scrollTop}, ratio=${ratio.toFixed(2)}, height=${height.toFixed(0)}px, isLocked=${isLocked}, isCompact=${isCompact}`);
|
|
426
|
|
- }
|
|
427
|
143
|
|
|
428
|
|
- private handleScroll(event: Event) {
|
|
429
|
|
- // 使用requestAnimationFrame防抖
|
|
430
|
|
- if (this.scrollRafId) {
|
|
431
|
|
- cancelAnimationFrame(this.scrollRafId);
|
|
|
144
|
+ // Calculate scroll ratio (0-1)
|
|
|
145
|
+ let ratio = 0;
|
|
|
146
|
+ if (this.collapseThreshold > 0) {
|
|
|
147
|
+ ratio = Math.min(1, scrollTop / this.collapseThreshold);
|
|
432
|
148
|
}
|
|
433
|
149
|
|
|
434
|
|
- this.scrollRafId = requestAnimationFrame(() => {
|
|
435
|
|
- this.scrollRafId = null;
|
|
436
|
|
-
|
|
437
|
|
- const container = event.target as HTMLElement;
|
|
438
|
|
- const scrollTop = container.scrollTop || 0;
|
|
439
|
|
-
|
|
440
|
|
- // 检查滚动位置是否显著变化,避免微小滚动触发状态更新
|
|
441
|
|
- const scrollDelta = Math.abs(scrollTop - this.lastScrollTop);
|
|
442
|
|
- if (scrollDelta < 3) {
|
|
443
|
|
- return;
|
|
444
|
|
- }
|
|
445
|
|
-
|
|
446
|
|
- this.lastScrollTop = scrollTop;
|
|
447
|
|
-
|
|
448
|
|
- // 计算压缩状态
|
|
449
|
|
- const { ratio, height, isLocked, isCompact } = this.calculateCompression(scrollTop);
|
|
450
|
|
- const isScrolled = ratio > 0;
|
|
451
|
|
-
|
|
452
|
|
- // 检查状态或高度是否真正变化,避免不必要更新
|
|
453
|
|
- const currentHeightValue = this.currentHeight();
|
|
454
|
|
- const heightChanged = currentHeightValue === null || Math.abs(height - currentHeightValue) > 1;
|
|
455
|
|
- const stateChanged =
|
|
456
|
|
- isLocked !== this.lastIsLocked ||
|
|
457
|
|
- isCompact !== this.lastIsCompact ||
|
|
458
|
|
- isScrolled !== this.isScrolled();
|
|
459
|
|
-
|
|
460
|
|
- if (!stateChanged && !heightChanged) {
|
|
461
|
|
- console.log(`sticky-header: 容器滚动位置变化但状态和高度未变,scrollTop=${scrollTop}, ratio=${ratio.toFixed(2)}, isLocked=${isLocked}, isCompact=${isCompact}, height=${height.toFixed(0)}px`);
|
|
462
|
|
- return;
|
|
463
|
|
- }
|
|
464
|
|
-
|
|
465
|
|
- this.lastIsLocked = isLocked;
|
|
466
|
|
- this.lastIsCompact = isCompact;
|
|
467
|
|
-
|
|
468
|
|
- // 更新状态
|
|
469
|
|
- this.currentHeight.set(height);
|
|
470
|
|
- this.isScrolled.set(isScrolled);
|
|
471
|
|
- this.isLocked.set(isLocked);
|
|
472
|
|
- this.isCompact.set(isCompact);
|
|
473
|
|
-
|
|
474
|
|
- console.log(`sticky-header: 容器滚动状态更新,scrollTop=${scrollTop}, ratio=${ratio.toFixed(2)}, height=${height.toFixed(0)}px, isLocked=${isLocked}, isCompact=${isCompact}`);
|
|
475
|
|
-
|
|
476
|
|
- // 设置宽度匹配容器
|
|
477
|
|
- if (isLocked) {
|
|
478
|
|
- // 容器滚动模式优先使用滚动容器作为宽度目标
|
|
479
|
|
- const widthTarget = this.getAutoWidthTarget();
|
|
480
|
|
- if (widthTarget) {
|
|
481
|
|
- const rect = widthTarget.getBoundingClientRect();
|
|
482
|
|
- this.headerWidth.set(rect.width);
|
|
483
|
|
- this.headerLeft.set(rect.left);
|
|
484
|
|
- console.log(`sticky-header: 容器滚动锁定,使用自动宽度目标,宽度: ${rect.width}px, 左侧位置: ${rect.left}px`);
|
|
485
|
|
- } else {
|
|
486
|
|
- // 未找到合适容器,使用滚动容器本身
|
|
487
|
|
- const rect = container.getBoundingClientRect();
|
|
488
|
|
- this.headerWidth.set(rect.width);
|
|
489
|
|
- this.headerLeft.set(rect.left);
|
|
490
|
|
- console.log(`sticky-header: 容器滚动锁定,使用滚动容器宽度,宽度: ${rect.width}px, 左侧位置: ${rect.left}px`);
|
|
491
|
|
- }
|
|
492
|
|
- } else {
|
|
493
|
|
- this.headerWidth.set(null);
|
|
494
|
|
- this.headerLeft.set(null);
|
|
495
|
|
- }
|
|
496
|
|
- });
|
|
497
|
|
- }
|
|
498
|
|
-
|
|
499
|
|
- private checkInitialScrollPosition(container: HTMLElement) {
|
|
500
|
|
- const scrollTop = container.scrollTop || 0;
|
|
501
|
|
- this.lastScrollTop = scrollTop;
|
|
|
150
|
+ console.log('StickyHeader: 滚动比例:', ratio, '阈值:', this.collapseThreshold);
|
|
502
|
151
|
|
|
503
|
|
- const { ratio, height, isLocked, isCompact } = this.calculateCompression(scrollTop);
|
|
504
|
|
- const isScrolled = ratio > 0;
|
|
|
152
|
+ // Update collapsed state based on threshold
|
|
|
153
|
+ const shouldCollapse = scrollTop > this.collapseThreshold;
|
|
505
|
154
|
|
|
506
|
|
- this.currentHeight.set(height);
|
|
507
|
|
- this.isScrolled.set(isScrolled);
|
|
508
|
|
- this.isLocked.set(isLocked);
|
|
509
|
|
- this.isCompact.set(isCompact);
|
|
|
155
|
+ console.log('StickyHeader: 当前折叠状态:', this.isCollapsed, '应折叠:', shouldCollapse);
|
|
510
|
156
|
|
|
511
|
|
- if (isLocked) {
|
|
512
|
|
- const widthTarget = this.getAutoWidthTarget();
|
|
513
|
|
- if (widthTarget) {
|
|
514
|
|
- const rect = widthTarget.getBoundingClientRect();
|
|
515
|
|
- this.headerWidth.set(rect.width);
|
|
516
|
|
- this.headerLeft.set(rect.left);
|
|
517
|
|
- console.log(`sticky-header: 初始容器滚动锁定,使用自动宽度目标,宽度: ${rect.width}px, 左侧位置: ${rect.left}px`);
|
|
518
|
|
- } else {
|
|
519
|
|
- const rect = container.getBoundingClientRect();
|
|
520
|
|
- this.headerWidth.set(rect.width);
|
|
521
|
|
- this.headerLeft.set(rect.left);
|
|
522
|
|
- console.log(`sticky-header: 初始容器滚动锁定,使用滚动容器宽度,宽度: ${rect.width}px, 左侧位置: ${rect.left}px`);
|
|
523
|
|
- }
|
|
|
157
|
+ if (this.isCollapsed !== shouldCollapse && this.enabled) {
|
|
|
158
|
+ console.log('StickyHeader: 折叠状态改变,从', this.isCollapsed, '到', shouldCollapse);
|
|
|
159
|
+ this.isCollapsed = shouldCollapse;
|
|
|
160
|
+ this.updateCollapsedState();
|
|
524
|
161
|
}
|
|
525
|
162
|
|
|
526
|
|
- console.log(`sticky-header: 初始容器滚动位置检查完成: scrollTop=${scrollTop}, ratio=${ratio.toFixed(2)}, height=${height.toFixed(0)}px, isLocked=${isLocked}, isCompact=${isCompact}`);
|
|
|
163
|
+ // Emit scroll ratio
|
|
|
164
|
+ this.ngZone.run(() => this.scrollRatioChange.emit(ratio));
|
|
527
|
165
|
}
|
|
528
|
166
|
|
|
529
|
|
- /**
|
|
530
|
|
- * 智能压缩计算 - 基于备份文件逻辑,自动计算最大滚动距离
|
|
531
|
|
- * @param scrollTop 当前滚动位置
|
|
532
|
|
- * @returns 压缩比例、高度和状态
|
|
533
|
|
- */
|
|
534
|
|
- private calculateCompression(scrollTop: number): {
|
|
535
|
|
- ratio: number; // 压缩比例 0-1
|
|
536
|
|
- height: number; // 当前动态高度
|
|
537
|
|
- isLocked: boolean; // 是否锁定
|
|
538
|
|
- isCompact: boolean; // 是否缩小
|
|
539
|
|
- } {
|
|
540
|
|
- // 如果尚未缓存尺寸或高度无效,返回默认值
|
|
541
|
|
- if (!this.maxHeight || this.maxHeight <= this.minHeight) {
|
|
542
|
|
- console.log(`sticky-header: 压缩计算 - 未缓存尺寸或高度无效,maxHeight=${this.maxHeight}, minHeight=${this.minHeight}`);
|
|
543
|
|
- return {
|
|
544
|
|
- ratio: 0,
|
|
545
|
|
- height: this.maxHeight || 0,
|
|
546
|
|
- isLocked: false,
|
|
547
|
|
- isCompact: false
|
|
548
|
|
- };
|
|
549
|
|
- }
|
|
550
|
|
-
|
|
551
|
|
- // 计算有效滚动距离
|
|
552
|
|
- const effectiveScroll = Math.max(scrollTop - this.initialTop, 0);
|
|
553
|
|
- const availableScroll = this.maxHeight - this.minHeight;
|
|
554
|
|
-
|
|
555
|
|
- // 自动计算最大可能的滚动距离
|
|
556
|
|
- let maxPossibleScroll = availableScroll; // 默认值
|
|
|
167
|
+ private updateCollapsedState() {
|
|
|
168
|
+ console.log('StickyHeader: 更新折叠状态,isCollapsed:', this.isCollapsed);
|
|
|
169
|
+ const element = this.elementRef.nativeElement as HTMLElement;
|
|
|
170
|
+ const hintArea = element.querySelector('.hint-area');
|
|
557
|
171
|
|
|
558
|
|
- if (this.scrollContainer === 'window') {
|
|
559
|
|
- // 窗口滚动:使用文档高度 - 视口高度
|
|
560
|
|
- const scrollHeight = document.body.scrollHeight || document.documentElement.scrollHeight || 0;
|
|
561
|
|
- const clientHeight = window.innerHeight || document.documentElement.clientHeight || 0;
|
|
562
|
|
- maxPossibleScroll = Math.max(scrollHeight - clientHeight, 0);
|
|
563
|
|
- } else {
|
|
564
|
|
- // 容器滚动:使用容器实际可滚动距离
|
|
565
|
|
- const container = this.getScrollContainer();
|
|
566
|
|
- if (container) {
|
|
567
|
|
- maxPossibleScroll = Math.max(container.scrollHeight - container.clientHeight, 0);
|
|
|
172
|
+ if (hintArea) {
|
|
|
173
|
+ if (this.isCollapsed) {
|
|
|
174
|
+ console.log('StickyHeader: 添加collapsed类');
|
|
|
175
|
+ hintArea.classList.add('collapsed');
|
|
|
176
|
+ } else {
|
|
|
177
|
+ console.log('StickyHeader: 移除collapsed类');
|
|
|
178
|
+ hintArea.classList.remove('collapsed');
|
|
568
|
179
|
}
|
|
|
180
|
+ } else {
|
|
|
181
|
+ console.warn('StickyHeader: 未找到.hint-area元素');
|
|
569
|
182
|
}
|
|
570
|
|
-
|
|
571
|
|
- // 如果容器实际可滚动距离为0(内容未完全加载或高度不足),使用虚拟高度确保压缩计算能进行
|
|
572
|
|
- if (maxPossibleScroll <= 0) {
|
|
573
|
|
- const virtualHeight = typeof window !== 'undefined' ? window.innerHeight + 200 : availableScroll;
|
|
574
|
|
- maxPossibleScroll = Math.max(virtualHeight, availableScroll);
|
|
575
|
|
- console.log(`sticky-header: 使用虚拟高度计算可滚动距离: ${virtualHeight}px, maxPossibleScroll=${maxPossibleScroll}`);
|
|
576
|
|
- }
|
|
577
|
|
-
|
|
578
|
|
- // 确定实际可用的压缩距离:取理论压缩距离和实际最大滚动距离的较小值
|
|
579
|
|
- const compressionDistance = Math.min(availableScroll, maxPossibleScroll);
|
|
580
|
|
-
|
|
581
|
|
- console.log(`sticky-header: 压缩计算参数:
|
|
582
|
|
- scrollTop=${scrollTop},
|
|
583
|
|
- initialTop=${this.initialTop},
|
|
584
|
|
- effectiveScroll=${effectiveScroll},
|
|
585
|
|
- maxHeight=${this.maxHeight},
|
|
586
|
|
- minHeight=${this.minHeight},
|
|
587
|
|
- availableScroll=${availableScroll},
|
|
588
|
|
- maxPossibleScroll=${maxPossibleScroll},
|
|
589
|
|
- compressionDistance=${compressionDistance}`);
|
|
590
|
|
-
|
|
591
|
|
- // 避免除零错误
|
|
592
|
|
- if (compressionDistance <= 0) {
|
|
593
|
|
- console.log(`sticky-header: 压缩计算 - compressionDistance<=0 (${compressionDistance}),跳过计算`);
|
|
594
|
|
- return {
|
|
595
|
|
- ratio: 0,
|
|
596
|
|
- height: this.maxHeight,
|
|
597
|
|
- isLocked: false,
|
|
598
|
|
- isCompact: false
|
|
599
|
|
- };
|
|
600
|
|
- }
|
|
601
|
|
-
|
|
602
|
|
- // 线性比例计算:滚动距离 / 实际压缩距离
|
|
603
|
|
- const ratio = Math.min(effectiveScroll / compressionDistance, 1);
|
|
604
|
|
- const currentHeight = this.maxHeight - (ratio * availableScroll);
|
|
605
|
|
-
|
|
606
|
|
- console.log(`sticky-header: 压缩计算结果:
|
|
607
|
|
- ratio=${ratio.toFixed(3)},
|
|
608
|
|
- currentHeight=${currentHeight.toFixed(0)}px,
|
|
609
|
|
- 需要滚动${compressionDistance * 0.5}px才能进入compact状态`);
|
|
610
|
|
-
|
|
611
|
|
- return {
|
|
612
|
|
- ratio,
|
|
613
|
|
- height: currentHeight,
|
|
614
|
|
- isLocked: ratio > 0, // 开始滚动即锁定
|
|
615
|
|
- isCompact: ratio > 0.5 // 压缩超过50%视为缩小状态
|
|
616
|
|
- };
|
|
617
|
183
|
}
|
|
618
|
184
|
|
|
619
|
|
- private cleanupScrollListener() {
|
|
620
|
|
- if (this.scrollListener) {
|
|
621
|
|
- this.scrollListener();
|
|
622
|
|
- this.scrollListener = null;
|
|
623
|
|
- console.log('sticky-header: 滚动监听器已清理');
|
|
|
185
|
+ private cleanupScrollDetection() {
|
|
|
186
|
+ if (this.scrollListener && this.scrollContainerElement) {
|
|
|
187
|
+ this.scrollContainerElement.removeEventListener('scroll', this.scrollListener);
|
|
624
|
188
|
}
|
|
625
|
|
- this.isAutoDetecting = false;
|
|
626
|
189
|
}
|
|
627
|
190
|
}
|