|
138 | 138 | return; |
139 | 139 | } |
140 | 140 |
|
| 141 | + // Track all currently intersecting cards |
| 142 | + const visibleCards = new Set(); |
| 143 | + |
141 | 144 | const observerOptions = { |
142 | 145 | root: null, |
143 | 146 | rootMargin: OBSERVER_ROOT_MARGIN, |
144 | 147 | threshold: 0, |
145 | 148 | }; |
146 | 149 |
|
147 | 150 | state.observer = new IntersectionObserver((entries) => { |
| 151 | + // Update the visibility set with changed entries |
| 152 | + // Only track cards that are actually visible (have dimensions) |
148 | 153 | entries.forEach((entry) => { |
| 154 | + const { cardId } = entry.target.dataset; |
| 155 | + const rect = entry.boundingClientRect; |
| 156 | + const isVisible = rect.height > 0 && rect.width > 0; |
| 157 | + |
| 158 | + // Only process events from visible cards (ignore hidden horizontal duplicates) |
| 159 | + if (!isVisible) { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
149 | 163 | if (entry.isIntersecting) { |
150 | | - const { cardId } = entry.target.dataset; |
151 | | - updateActiveNavLink(cardId); |
| 164 | + visibleCards.add(cardId); |
| 165 | + } else { |
| 166 | + visibleCards.delete(cardId); |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + // Find the card that best overlaps the viewport center |
| 171 | + // Prefer the topmost card whose bounds contain the center point |
| 172 | + const viewportCenter = window.innerHeight / 2; |
| 173 | + let bestCardId = null; |
| 174 | + let bestScore = -Infinity; |
| 175 | + |
| 176 | + visibleCards.forEach((cardId) => { |
| 177 | + // Find the visible card (not the hidden horizontal view duplicate) |
| 178 | + const cards = timeline.querySelectorAll(`[data-card-id="${cardId}"]`); |
| 179 | + const card = Array.from(cards).find( |
| 180 | + (c) => c.getBoundingClientRect().height > 0, |
| 181 | + ); |
| 182 | + if (card) { |
| 183 | + const rect = card.getBoundingClientRect(); |
| 184 | + |
| 185 | + // Score based on how well the card covers the viewport center |
| 186 | + // Higher score = card contains or is closer to center |
| 187 | + let score; |
| 188 | + if (rect.top <= viewportCenter && rect.bottom >= viewportCenter) { |
| 189 | + // Card contains the center point - highest priority |
| 190 | + // Prefer cards where center is further from the edges (more centered) |
| 191 | + const distFromTop = viewportCenter - rect.top; |
| 192 | + const distFromBottom = rect.bottom - viewportCenter; |
| 193 | + score = 1000 + Math.min(distFromTop, distFromBottom); |
| 194 | + } else { |
| 195 | + // Card doesn't contain center - score by distance |
| 196 | + const cardCenter = rect.top + rect.height / 2; |
| 197 | + score = -Math.abs(cardCenter - viewportCenter); |
| 198 | + } |
| 199 | + |
| 200 | + if (score > bestScore) { |
| 201 | + bestScore = score; |
| 202 | + bestCardId = cardId; |
| 203 | + } |
152 | 204 | } |
153 | 205 | }); |
| 206 | + |
| 207 | + if (bestCardId) { |
| 208 | + updateActiveNavLink(bestCardId); |
| 209 | + } |
154 | 210 | }, observerOptions); |
155 | 211 |
|
156 | 212 | elements.cards.forEach((card) => { |
|
0 commit comments