-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-home.js
More file actions
96 lines (83 loc) · 2.89 KB
/
Copy pathscript-home.js
File metadata and controls
96 lines (83 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Performance-optimized JavaScript for zer0state.com (2026 standards)
// Implements scheduler.yield() for INP optimization and other performance best practices
const year = document.querySelector('[data-year]');
if (year) year.textContent = String(new Date().getFullYear());
const masthead = document.querySelector('[data-masthead]');
// INP Optimization: Utility function to yield to browser before expensive operations
async function yieldThen(callback) {
await scheduler.yield();
return callback();
}
// Optimized scroll handler with requestAnimationFrame and yielding
let ticking = false;
let lastScrollY = 0;
const updateMast = () => {
// Yield to browser first for INP optimization
return yieldThen(() => {
const scrollY = window.scrollY;
// Only update if scroll position has changed significantly
if (Math.abs(scrollY - lastScrollY) > 5) {
masthead?.classList.toggle('is-scrolled', scrollY > 12);
lastScrollY = scrollY;
}
});
};
// Scroll event listener with throttling and yielding
window.addEventListener('scroll', (e) => {
if (!ticking) {
window.requestAnimationFrame(() => {
updateMast();
ticking = false;
});
ticking = true;
}
}, { passive: true }); // Passive for better scrolling performance
// Optional: Add intersection observer for lazy loading if we implement more lazy elements
// This would be for future enhancements beyond current lazy loading
// Function to handle clicks with INP optimization
function handleClickWithYield(element, callback) {
element.addEventListener('click', async (e) => {
// Prevent default if it's a link and we want to handle navigation specially
if (element.tagName === 'A' && element.hasAttribute('href')) {
e.preventDefault();
await yieldThen(() => {
// Handle navigation - for now just follow the link
window.location.href = element.getAttribute('href');
});
} else {
// For buttons and other elements
await yieldThen(() => {
callback(e);
});
}
});
}
// Example usage for specific elements (uncomment and adapt as needed)
// handleClickWithYield(document.querySelector('.some-button'), (e) => {
// // Your click handling logic here
// console.log('Button clicked');
// });
// Resize handler optimization (if needed)
let resizing = false;
window.addEventListener('resize', () => {
if (!resizing) {
resizing = true;
window.requestAnimationFrame(() => {
// Add resize handling logic here if needed
resizing = false;
});
}
});
// Visibility change handler to pause/resume animations if needed
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
// Page is hidden - pause any expensive operations
} else {
// Page is visible - resume operations
}
});
// Export utility functions for use in other scripts if needed
window.zer0state = {
yieldThen,
handleClickWithYield
};