-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
323 lines (282 loc) · 8.56 KB
/
script.js
File metadata and controls
323 lines (282 loc) · 8.56 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Theme Switcher JavaScript
document.addEventListener('DOMContentLoaded', function () {
// Get theme elements
const themeLink = document.getElementById('theme-link');
const themeButtons = document.querySelectorAll('.theme-btn');
const designLinks = document.querySelectorAll('.design-link');
// Theme configuration
const themes = {
modern: {
css: 'theme-modern.css',
name: 'Modern Minimalist',
designer: 'by Clean Design Co.',
},
vintage: {
css: 'theme-vintage.css',
name: 'Vintage Classic',
designer: 'by Retro Studios',
},
};
// Get current theme from localStorage or default to modern
let currentTheme = localStorage.getItem('selectedTheme') || 'modern';
// Initialize theme on page load
function initializeTheme() {
setTheme(currentTheme);
updateActiveStates();
}
// Set theme function
function setTheme(themeName) {
if (themes[themeName]) {
// Add transition class to body for smooth theme switching
document.body.classList.add('theme-transitioning');
// Update the CSS link
themeLink.href = themes[themeName].css;
// Update current theme
currentTheme = themeName;
// Save to localStorage
localStorage.setItem('selectedTheme', themeName);
// Update active states
updateActiveStates();
// Add a brief delay for the transition effect
setTimeout(() => {
document.body.classList.remove('theme-transitioning');
}, 300);
// Trigger custom event for theme change
document.dispatchEvent(
new CustomEvent('themeChanged', {
detail: { theme: themeName, themeData: themes[themeName] },
}),
);
}
}
// Update active states for buttons and design links
function updateActiveStates() {
// Update theme buttons
themeButtons.forEach(button => {
const buttonTheme = button.getAttribute('data-theme');
if (buttonTheme === currentTheme) {
button.classList.add('active');
} else {
button.classList.remove('active');
}
});
// Update design links in sidebar
designLinks.forEach((link, index) => {
const linkTheme = index === 0 ? 'modern' : 'vintage';
if (linkTheme === currentTheme) {
link.classList.add('active');
// Update the design name and designer
const designName = link.querySelector('.design-name');
const designerName = link.querySelector('.designer-name');
if (designName && designerName) {
designName.textContent = themes[currentTheme].name;
designerName.textContent = themes[currentTheme].designer;
}
} else {
link.classList.remove('active');
}
});
}
// Add click event listeners to theme buttons
themeButtons.forEach(button => {
button.addEventListener('click', function (e) {
e.preventDefault();
const selectedTheme = this.getAttribute('data-theme');
if (selectedTheme && selectedTheme !== currentTheme) {
setTheme(selectedTheme);
// Add click animation
this.style.transform = 'scale(0.95)';
setTimeout(() => {
this.style.transform = '';
}, 150);
}
});
// Add hover effect for better UX
button.addEventListener('mouseenter', function () {
if (!this.classList.contains('active')) {
this.style.transform = 'translateY(-2px)';
}
});
button.addEventListener('mouseleave', function () {
if (!this.classList.contains('active')) {
this.style.transform = '';
}
});
});
// Add click event listeners to design links in sidebar
designLinks.forEach((link, index) => {
link.addEventListener('click', function (e) {
e.preventDefault();
const selectedTheme = index === 0 ? 'modern' : 'vintage';
if (selectedTheme !== currentTheme) {
setTheme(selectedTheme);
}
});
});
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
}
});
});
// Add intersection observer for fade-in animations
function setupScrollAnimations() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px',
};
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
}, observerOptions);
// Observe sections for animations
document.querySelectorAll('section').forEach(section => {
section.style.opacity = '0';
section.style.transform = 'translateY(30px)';
section.style.transition =
'opacity 0.6s ease-out, transform 0.6s ease-out';
observer.observe(section);
});
// Observe feature cards
document.querySelectorAll('.feature-card').forEach((card, index) => {
card.style.opacity = '0';
card.style.transform = 'translateY(30px)';
card.style.transition = `opacity 0.6s ease-out ${
index * 0.1
}s, transform 0.6s ease-out ${index * 0.1}s`;
observer.observe(card);
});
}
// Add keyboard navigation support
document.addEventListener('keydown', function (e) {
// Allow theme switching with keyboard shortcuts
if (e.altKey) {
switch (e.key) {
case '1':
e.preventDefault();
setTheme('modern');
break;
case '2':
e.preventDefault();
setTheme('vintage');
break;
}
}
});
// Add theme transition CSS dynamically
function addThemeTransitionStyles() {
const style = document.createElement('style');
style.textContent = `
.theme-transitioning * {
transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease !important;
}
/* Prevent FOUC (Flash of Unstyled Content) */
.theme-loading {
opacity: 0;
}
.theme-loaded {
opacity: 1;
transition: opacity 0.3s ease;
}
`;
document.head.appendChild(style);
}
// Performance optimization: Preload theme stylesheets
function preloadThemes() {
Object.keys(themes).forEach(themeName => {
if (themeName !== currentTheme) {
const link = document.createElement('link');
link.rel = 'preload';
link.as = 'style';
link.href = themes[themeName].css;
document.head.appendChild(link);
}
});
}
// Add accessibility announcements for theme changes
function announceThemeChange(themeName) {
const announcement = document.createElement('div');
announcement.setAttribute('aria-live', 'polite');
announcement.setAttribute('aria-atomic', 'true');
announcement.className = 'sr-only';
announcement.textContent = `Theme changed to ${themes[themeName].name}`;
document.body.appendChild(announcement);
// Remove announcement after a brief delay
setTimeout(() => {
document.body.removeChild(announcement);
}, 1000);
}
// Listen for theme change events
document.addEventListener('themeChanged', function (e) {
announceThemeChange(e.detail.theme);
console.log(`Theme switched to: ${e.detail.themeData.name}`);
});
// Add print styles support
function handlePrintMode() {
window.addEventListener('beforeprint', function () {
document.body.classList.add('print-mode');
});
window.addEventListener('afterprint', function () {
document.body.classList.remove('print-mode');
});
}
// Initialize everything
function init() {
addThemeTransitionStyles();
document.body.classList.add('theme-loading');
// Initialize theme
initializeTheme();
// Setup scroll animations
setupScrollAnimations();
// Preload other themes
preloadThemes();
// Setup print handling
handlePrintMode();
// Mark as loaded
setTimeout(() => {
document.body.classList.remove('theme-loading');
document.body.classList.add('theme-loaded');
}, 100);
}
// Start initialization
init();
// Add screen reader only class for accessibility
const srOnlyStyle = document.createElement('style');
srOnlyStyle.textContent = `
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
`;
document.head.appendChild(srOnlyStyle);
});
// Export functions for potential external use
window.ThemeSwitcher = {
setTheme: function (themeName) {
const event = new CustomEvent('setTheme', {
detail: { theme: themeName },
});
document.dispatchEvent(event);
},
getCurrentTheme: function () {
return localStorage.getItem('selectedTheme') || 'modern';
},
};