|
| 1 | +// Ultra-performant scroll handler - No Alpine, No RAF, No timers, No forEach |
| 2 | +// Direct DOM manipulation with passive listeners |
| 3 | + |
| 4 | +let lastScrollY = 0; |
| 5 | +let ticking = false; |
| 6 | + |
| 7 | +// Cache DOM elements |
| 8 | +let header = null; |
| 9 | +let scrollButton = null; |
| 10 | +let scrollUpArrow = null; |
| 11 | +let scrollDownArrow = null; |
| 12 | +let progressBar = null; |
| 13 | +let progressBarContainer = null; |
| 14 | + |
| 15 | +// Initialize scroll handler |
| 16 | +function initScrollHandler() { |
| 17 | + // Cache all DOM elements once |
| 18 | + header = document.querySelector('header[data-scroll-header]'); |
| 19 | + scrollButton = document.getElementById('scroll-button'); |
| 20 | + scrollUpArrow = document.getElementById('scroll-up-arrow'); |
| 21 | + scrollDownArrow = document.getElementById('scroll-down-arrow'); |
| 22 | + progressBar = document.getElementById('scroll-progress-bar'); |
| 23 | + progressBarContainer = document.getElementById('scroll-progress-container'); |
| 24 | + |
| 25 | + // Set initial states |
| 26 | + updateScrollState(); |
| 27 | + |
| 28 | + // Single passive scroll listener |
| 29 | + window.addEventListener('scroll', onScroll, { passive: true }); |
| 30 | +} |
| 31 | + |
| 32 | +function onScroll() { |
| 33 | + if (ticking) return; |
| 34 | + ticking = true; |
| 35 | + |
| 36 | + // Use native browser scheduling (not RAF - just next tick) |
| 37 | + Promise.resolve().then(() => { |
| 38 | + updateScrollState(); |
| 39 | + ticking = false; |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +function updateScrollState() { |
| 44 | + const scrollY = window.pageYOffset; |
| 45 | + const docHeight = document.documentElement.scrollHeight; |
| 46 | + const winHeight = window.innerHeight; |
| 47 | + const maxScroll = docHeight - winHeight; |
| 48 | + const scrollPercent = maxScroll > 0 ? (scrollY / maxScroll) * 100 : 0; |
| 49 | + |
| 50 | + // 1. Update header (scrolled state) |
| 51 | + if (header) { |
| 52 | + if (scrollY > 20) { |
| 53 | + if (!header.classList.contains('scrolled')) { |
| 54 | + header.classList.add('scrolled'); |
| 55 | + } |
| 56 | + } else { |
| 57 | + if (header.classList.contains('scrolled')) { |
| 58 | + header.classList.remove('scrolled'); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // 2. Update scroll button visibility & direction |
| 64 | + if (scrollButton && scrollUpArrow && scrollDownArrow) { |
| 65 | + const isAtTop = scrollY < 100; |
| 66 | + const isAtBottom = scrollPercent > 95; |
| 67 | + |
| 68 | + // Show/hide button |
| 69 | + if (isAtTop || isAtBottom || (scrollY > 100 && scrollPercent < 95)) { |
| 70 | + if (scrollButton.style.display === 'none') { |
| 71 | + scrollButton.style.display = 'block'; |
| 72 | + } |
| 73 | + } else { |
| 74 | + if (scrollButton.style.display !== 'none') { |
| 75 | + scrollButton.style.display = 'none'; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Direction |
| 80 | + if (isAtTop || (!isAtBottom && scrollY > lastScrollY)) { |
| 81 | + // Scrolling down or at top = show down arrow |
| 82 | + if (scrollDownArrow.style.display !== 'block') { |
| 83 | + scrollDownArrow.style.display = 'block'; |
| 84 | + scrollUpArrow.style.display = 'none'; |
| 85 | + } |
| 86 | + } else { |
| 87 | + // Scrolling up or at bottom = show up arrow |
| 88 | + if (scrollUpArrow.style.display !== 'block') { |
| 89 | + scrollUpArrow.style.display = 'block'; |
| 90 | + scrollDownArrow.style.display = 'none'; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // 3. Update progress bar |
| 96 | + if (progressBar && progressBarContainer) { |
| 97 | + if (scrollPercent > 5) { |
| 98 | + if (progressBarContainer.style.display === 'none') { |
| 99 | + progressBarContainer.style.display = 'block'; |
| 100 | + } |
| 101 | + progressBar.style.width = scrollPercent + '%'; |
| 102 | + } else { |
| 103 | + if (progressBarContainer.style.display !== 'none') { |
| 104 | + progressBarContainer.style.display = 'none'; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + lastScrollY = scrollY; |
| 110 | +} |
| 111 | + |
| 112 | +// Initialize on DOM ready |
| 113 | +if (document.readyState === 'loading') { |
| 114 | + document.addEventListener('DOMContentLoaded', initScrollHandler); |
| 115 | +} else { |
| 116 | + initScrollHandler(); |
| 117 | +} |
| 118 | + |
| 119 | +// Export for button click handlers |
| 120 | +window.scrollToTop = () => { |
| 121 | + window.scrollTo({ top: 0, behavior: 'smooth' }); |
| 122 | +}; |
| 123 | + |
| 124 | +window.scrollToBottom = () => { |
| 125 | + window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' }); |
| 126 | +}; |
0 commit comments