|
96 | 96 | } |
97 | 97 | } |
98 | 98 |
|
| 99 | + function getScrollTop() { |
| 100 | + const scrollEl = document.scrollingElement || document.documentElement || document.body; |
| 101 | + return Math.max( |
| 102 | + window.scrollY || 0, |
| 103 | + window.pageYOffset || 0, |
| 104 | + scrollEl?.scrollTop || 0 |
| 105 | + ); |
| 106 | + } |
| 107 | + |
99 | 108 | function createSettledScheduler(callback) { |
100 | 109 | const timers = []; |
101 | 110 |
|
|
334 | 343 | }); |
335 | 344 | } |
336 | 345 |
|
| 346 | + function initOrientationRecovery(){ |
| 347 | + let lastPortrait = isPortraitMobile(); |
| 348 | + let lastStableScrollY = getScrollTop(); |
| 349 | + let pendingRecovery = null; |
| 350 | + |
| 351 | + const updateStableState = () => { |
| 352 | + lastPortrait = isPortraitMobile(); |
| 353 | + lastStableScrollY = getScrollTop(); |
| 354 | + }; |
| 355 | + |
| 356 | + const settledRecovery = createSettledScheduler(() => { |
| 357 | + if (pendingRecovery && isPortraitMobile() && !pendingRecovery.previousPortrait){ |
| 358 | + const currentY = getScrollTop(); |
| 359 | + if (Math.abs(currentY - pendingRecovery.scrollY) > 1){ |
| 360 | + window.scrollTo({ top: pendingRecovery.scrollY, behavior: 'auto' }); |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + pendingRecovery = null; |
| 365 | + updateStableState(); |
| 366 | + }); |
| 367 | + |
| 368 | + window.addEventListener('scroll', () => { |
| 369 | + lastStableScrollY = getScrollTop(); |
| 370 | + }, { passive:true }); |
| 371 | + |
| 372 | + window.addEventListener('orientationchange', () => { |
| 373 | + pendingRecovery = { |
| 374 | + previousPortrait: lastPortrait, |
| 375 | + scrollY: lastStableScrollY |
| 376 | + }; |
| 377 | + settledRecovery.schedule(160); |
| 378 | + }); |
| 379 | + |
| 380 | + window.addEventListener('resize', () => { |
| 381 | + if (pendingRecovery){ |
| 382 | + settledRecovery.schedule(120); |
| 383 | + return; |
| 384 | + } |
| 385 | + updateStableState(); |
| 386 | + }); |
| 387 | + |
| 388 | + window.addEventListener('pageshow', updateStableState); |
| 389 | + |
| 390 | + if (window.visualViewport){ |
| 391 | + const syncViewportRecovery = () => { |
| 392 | + if (!pendingRecovery) return; |
| 393 | + settledRecovery.schedule(120); |
| 394 | + }; |
| 395 | + |
| 396 | + window.visualViewport.addEventListener('resize', syncViewportRecovery); |
| 397 | + window.visualViewport.addEventListener('scroll', syncViewportRecovery); |
| 398 | + } |
| 399 | + } |
| 400 | + |
337 | 401 | function initNavBackdrop(){ |
338 | 402 | if (document.body.dataset.backdropInit === '1') return; |
339 | 403 | document.body.dataset.backdropInit = '1'; |
|
343 | 407 | let ticking = false; |
344 | 408 |
|
345 | 409 | const getScrolled = () => { |
346 | | - const scrollEl = document.scrollingElement || document.documentElement || document.body; |
347 | | - const y = Math.max( |
348 | | - window.scrollY || 0, |
349 | | - window.pageYOffset || 0, |
350 | | - scrollEl?.scrollTop || 0 |
351 | | - ); |
352 | | - return y > 4; |
| 410 | + return getScrollTop() > 4; |
353 | 411 | }; |
354 | 412 |
|
355 | 413 | const resetBackdropState = () => { |
|
1134 | 1192 | renderProjects(); |
1135 | 1193 | initYear(); |
1136 | 1194 | initNav(); |
| 1195 | + initOrientationRecovery(); |
1137 | 1196 | syncMobileNavState(); |
1138 | 1197 | initAnchorScroll(); |
1139 | 1198 | initSectionSpy(); |
|
0 commit comments