|
8 | 8 | function recalcActiveHeader(): void { |
9 | 9 | const headers = Array.from( |
10 | 10 | document.querySelectorAll<HTMLElement>( |
11 | | - ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].map(it => `.article__layout > ${it}`).join(', ') |
| 11 | + [/* 'h1', */'h2', 'h3', 'h4', 'h5', 'h6'].map(it => `.article__layout > ${it}`).join(', ') |
12 | 12 | ) |
13 | 13 | ); |
14 | 14 | const navHeaders = Array.from( |
15 | 15 | document.querySelectorAll<HTMLElement>('.toc .toc-link') |
16 | 16 | ); |
17 | 17 | const scrollTop = window.scrollY; |
18 | | - const offsets = headers.map(it => |
19 | | - it.getBoundingClientRect().top - |
| 18 | + const scrollHeight = window.innerHeight; |
| 19 | + const offsets = headers.map(it => { |
| 20 | + const bbox = it.getBoundingClientRect(); |
| 21 | + return bbox.top - |
20 | 22 | (parseFloat(getComputedStyle(it).marginTop) || 0) + |
21 | | - window.scrollY |
22 | | - ); |
23 | | - let activeIndex = 0; |
24 | | - for (let i = 1; i < offsets.length; ++i) { |
25 | | - if (scrollTop >= offsets[i] - 40 - 2) { |
26 | | - activeIndex = i; |
| 23 | + window.scrollY; |
| 24 | + }); |
| 25 | + const heights = offsets.map((it, i) => { |
| 26 | + if (i === offsets.length - 1) { |
| 27 | + return document.body.scrollHeight - it; |
| 28 | + } |
| 29 | + return offsets[i + 1] - it; |
| 30 | + }); |
| 31 | + let firstActiveIndex = -1; |
| 32 | + let lastActiveIndex = -1; |
| 33 | + for (let i = 0; i < offsets.length; ++i) { |
| 34 | + const intersection = Math.min(scrollTop + scrollHeight, offsets[i] + heights[i]) - |
| 35 | + Math.max(scrollTop, offsets[i]); |
| 36 | + console.log(i, intersection, offsets[i], scrollTop, scrollHeight) |
| 37 | + if (intersection > 0) { |
| 38 | + if (firstActiveIndex < 0) { |
| 39 | + firstActiveIndex = i; |
| 40 | + } |
| 41 | + lastActiveIndex = i; |
27 | 42 | } |
28 | 43 | } |
29 | | - const oldItem = document.querySelector<HTMLElement>('.toc-link_active'); |
30 | | - const newItem = navHeaders[activeIndex]; |
31 | | - if (newItem && newItem !== oldItem) { |
32 | | - oldItem?.classList.remove('toc-link_active'); |
33 | | - newItem.classList.add('toc-link_active'); |
| 44 | + console.log({firstActiveIndex, lastActiveIndex}) |
| 45 | + firstActiveIndex = Math.max(firstActiveIndex, 0); |
| 46 | + lastActiveIndex = Math.max(lastActiveIndex, 0); |
| 47 | + const oldItems = new Set(document.querySelectorAll<HTMLElement>('.toc-link_active')); |
| 48 | + const newItems = new Set(navHeaders.slice(firstActiveIndex, lastActiveIndex + 1)); |
| 49 | + if (!(oldItems.isSubsetOf(newItems) && newItems.isSubsetOf(oldItems)) ) { |
| 50 | + for (const it of oldItems) { |
| 51 | + it.classList.remove('toc-link_active'); |
| 52 | + } |
| 53 | + for (const it of newItems) { |
| 54 | + it.classList.add('toc-link_active'); |
| 55 | + } |
34 | 56 | } |
35 | 57 | } |
36 | 58 |
|
|
0 commit comments