-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
51 lines (44 loc) · 1.94 KB
/
script.js
File metadata and controls
51 lines (44 loc) · 1.94 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
document.addEventListener('DOMContentLoaded', () => {
const toggleBtn = document.getElementById('theme-toggle');
const sunIcon = document.querySelector('.sun-icon');
const moonIcon = document.querySelector('.moon-icon');
// Check for saved user preference, if any, on load of the website
const savedTheme = localStorage.getItem('theme');
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Default to light if no preference is saved (as per requirements),
// but honor saved preference if it exists.
// If we strictly want default light on first visit regardless of system:
// we just check if 'theme' is 'dark'.
// STRICTLY default to light if no preference is saved.
// We intentionally ignore systemPrefersDark to force the "light" aesthetic by default as requested.
if (savedTheme === 'dark') {
document.documentElement.classList.add('dark');
updateIcons(true);
} else {
document.documentElement.classList.remove('dark');
updateIcons(false);
}
toggleBtn.addEventListener('click', () => {
const isDark = document.documentElement.classList.toggle('dark');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
updateIcons(isDark);
});
function updateIcons(isDark) {
if (isDark) {
sunIcon.style.display = 'block';
moonIcon.style.display = 'none';
} else {
sunIcon.style.display = 'none';
moonIcon.style.display = 'block';
}
}
// Interactive Background Effect
// Tracks mouse position and updates CSS variables for the gradient center
document.addEventListener('mousemove', (e) => {
const x = e.clientX;
const y = e.clientY;
// Update CSS variables on the body
document.body.style.setProperty('--mouse-x', `${x}px`);
document.body.style.setProperty('--mouse-y', `${y}px`);
});
});