-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
91 lines (77 loc) · 3.08 KB
/
script.js
File metadata and controls
91 lines (77 loc) · 3.08 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
document.addEventListener('DOMContentLoaded', () => {
// --- Theme Toggling Logic ---
const themeToggleBtn = document.getElementById('themeToggle');
const rootElement = document.documentElement;
const icon = themeToggleBtn.querySelector('i');
// Check for saved user preference, if any, on load of the website
const savedTheme = localStorage.getItem('theme');
// Check system preference if no saved theme
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Apply the initial theme
if (savedTheme) {
rootElement.setAttribute('data-theme', savedTheme);
updateIcon(savedTheme);
} else if (systemPrefersDark) {
rootElement.setAttribute('data-theme', 'dark');
updateIcon('dark');
}
// Toggle theme on button click
themeToggleBtn.addEventListener('click', () => {
const currentTheme = rootElement.getAttribute('data-theme');
const newTheme = currentTheme === 'light' ? 'dark' : 'light';
rootElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
// Add a small rotation animation to the icon during toggle
icon.style.transform = 'rotate(360deg)';
setTimeout(() => {
icon.style.transform = 'none';
}, 300);
updateIcon(newTheme);
});
function updateIcon(theme) {
if (theme === 'dark') {
icon.classList.remove('fa-moon');
icon.classList.add('fa-sun');
} else {
icon.classList.remove('fa-sun');
icon.classList.add('fa-moon');
}
}
// --- Typing Animation Logic ---
const greetingElement = document.getElementById('greeting');
const textToType = 'Hello, World! Welcome. ';
const typingSpeed = 100; // milliseconds per character
let i = 0;
function typeWriter() {
if (i < textToType.length) {
greetingElement.innerHTML += textToType.charAt(i);
i++;
setTimeout(typeWriter, typingSpeed);
} else {
// After typing finishes, maybe add a slight pause then change text or format
// Here we just leave it and let the CSS cursor blink
}
}
// Start typing animation with a small delay
setTimeout(typeWriter, 500);
// --- Intersection Observer for Scroll Animations ---
// (Optional enhancement to trigger animations only when element is in view)
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.1
};
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.animationPlayState = 'running';
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Pause all slide-up animations initially and let observer trigger them
document.querySelectorAll('.slide-up').forEach(el => {
el.style.animationPlayState = 'paused';
observer.observe(el);
});
});