Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
421 lines (351 loc) · 12.7 KB
/
Copy pathscript.js
File metadata and controls
421 lines (351 loc) · 12.7 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// ===========================
// Smooth Scroll & Navigation
// ===========================
// Mobile Navigation Toggle
const navToggle = document.getElementById('navToggle');
const navMenu = document.getElementById('navMenu');
const navLinks = document.querySelectorAll('.nav-link');
if (navToggle) {
navToggle.addEventListener('click', () => {
navToggle.classList.toggle('active');
navMenu.classList.toggle('active');
document.body.style.overflow = navMenu.classList.contains('active') ? 'hidden' : '';
});
}
// Close mobile menu when clicking on a link
navLinks.forEach(link => {
link.addEventListener('click', () => {
navToggle?.classList.remove('active');
navMenu?.classList.remove('active');
document.body.style.overflow = '';
});
});
// Navbar scroll effect
const navbar = document.getElementById('navbar');
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
lastScroll = currentScroll;
});
// Active navigation link on scroll
window.addEventListener('scroll', () => {
let current = '';
const sections = document.querySelectorAll('section[id]');
// If there are no sections with IDs on the page (for example the Learn page),
// avoid the previous behavior that treated an empty `current` as matching
// every href (because every string includes the empty string). Instead,
// set the active link by matching the current page filename.
if (sections.length === 0) {
const page = location.pathname.split('/').pop(); // e.g. downloads.html
navLinks.forEach(link => {
link.classList.remove('active');
const href = link.getAttribute('href') || '';
if (page && href.includes(page)) {
link.classList.add('active');
}
});
return;
}
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (scrollY >= (sectionTop - 200)) {
current = section.getAttribute('id');
}
});
// Only add the active class when we have a non-empty `current` section id.
navLinks.forEach(link => {
link.classList.remove('active');
if (current) {
const href = link.getAttribute('href') || '';
if (href.includes('#' + current) || href.includes(current)) {
link.classList.add('active');
}
}
});
});
// ===========================
// Scroll Animations
// ===========================
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, observerOptions);
// Observe all fade-in elements
document.addEventListener('DOMContentLoaded', () => {
const fadeElements = document.querySelectorAll('.fade-in-up, .fade-in');
fadeElements.forEach(el => observer.observe(el));
});
// Ensure any old anchors that point to `index.html#about` or `#about`
// are normalized to the dedicated `about.html` page. This fixes cases
// where cached HTML or legacy links still point to the anchor and
// cause navigation back to the homepage instead of the About page.
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('a[href]').forEach(a => {
const href = a.getAttribute('href');
if (!href) return;
// Normalize links like '#about' or 'index.html#about' to 'about.html'
if (href === '#about' || href.endsWith('#about') || href.includes('index.html#about')) {
a.setAttribute('href', 'about.html');
}
});
});
// ===========================
// Back to Top Button
// ===========================
const backToTopButton = document.getElementById('backToTop');
if (backToTopButton) {
window.addEventListener('scroll', () => {
if (window.pageYOffset > 300) {
backToTopButton.classList.add('visible');
} else {
backToTopButton.classList.remove('visible');
}
});
backToTopButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}
// ===========================
// Smooth Scroll for Anchor Links
// ===========================
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
const href = this.getAttribute('href');
if (href === '#') return;
e.preventDefault();
const target = document.querySelector(href);
if (target) {
const offsetTop = target.offsetTop - 80;
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
}
});
});
// ===========================
// Loading Animation
// ===========================
window.addEventListener('load', () => {
document.body.classList.add('loaded');
});
// ===========================
// Floating Cards Animation (Hero Section)
// ===========================
function removeMobileHeroCards() {
// Desktop detection (width > 1024px)
const isDesktop = window.innerWidth > 1024;
const heroImage = document.querySelector('.hero-image');
if (!isDesktop && heroImage) {
heroImage.remove();
console.log('Hero cards removed on mobile device');
} else if (isDesktop && heroImage) {
// Ensure hero image is visible
heroImage.style.display = '';
// Apply animation delays for floating cards
const floatingCards = document.querySelectorAll('.floating-card');
floatingCards.forEach((card, index) => {
card.style.animationDelay = `${index * 0.5}s`;
card.style.visibility = 'visible';
});
}
}
// Run on DOM ready
document.addEventListener('DOMContentLoaded', removeMobileHeroCards);
// Also run immediately in case DOM is already loaded
if (document.readyState === 'loading') {
// Still loading
} else {
removeMobileHeroCards();
}
// ===========================
// Download Tracking (Optional Analytics)
// ===========================
const downloadLinks = document.querySelectorAll('a[download], a[href$=".pdf"]');
downloadLinks.forEach(link => {
link.addEventListener('click', (e) => {
const fileName = link.getAttribute('href').split('/').pop();
console.log(`Download initiated: ${fileName}`);
// You can add Google Analytics or other tracking here
// Example: gtag('event', 'download', { 'file_name': fileName });
});
});
// ===========================
// Parallax Effect (Optional)
// ===========================
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const heroGradient = document.querySelector('.hero-gradient');
if (heroGradient) {
heroGradient.style.transform = `translateY(${scrolled * 0.5}px)`;
}
});
// ===========================
// Card Hover Effects Enhancement
// ===========================
const cards = document.querySelectorAll('.content-card, .workshop-card, .tip-card');
cards.forEach(card => {
card.addEventListener('mouseenter', function() {
this.style.transition = 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)';
});
});
// ===========================
// Performance Optimization
// ===========================
// Lazy load images
if ('loading' in HTMLImageElement.prototype) {
const images = document.querySelectorAll('img[loading="lazy"]');
images.forEach(img => {
img.src = img.dataset.src;
});
} else {
// Fallback for browsers that don't support lazy loading
const script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lazysizes/5.3.2/lazysizes.min.js';
document.body.appendChild(script);
}
// ===========================
// Accessibility Enhancements
// ===========================
// Skip to main content
document.addEventListener('DOMContentLoaded', () => {
const skipLink = document.createElement('a');
skipLink.href = '#main';
skipLink.className = 'skip-link';
skipLink.textContent = 'Skip to main content';
skipLink.style.cssText = `
position: absolute;
top: -40px;
left: 0;
background: var(--primary-color);
color: white;
padding: 8px;
text-decoration: none;
z-index: 100;
`;
skipLink.addEventListener('focus', () => {
skipLink.style.top = '0';
});
skipLink.addEventListener('blur', () => {
skipLink.style.top = '-40px';
});
document.body.insertBefore(skipLink, document.body.firstChild);
});
// Keyboard navigation enhancement
document.addEventListener('keydown', (e) => {
// Escape key closes mobile menu
if (e.key === 'Escape' && navMenu?.classList.contains('active')) {
navToggle?.classList.remove('active');
navMenu?.classList.remove('active');
document.body.style.overflow = '';
}
});
// ===========================
// Print Styles Handler
// ===========================
window.addEventListener('beforeprint', () => {
document.body.classList.add('printing');
});
window.addEventListener('afterprint', () => {
document.body.classList.remove('printing');
});
// ===========================
// Dynamic Year in Footer
// ===========================
const yearElements = document.querySelectorAll('.current-year');
yearElements.forEach(el => {
el.textContent = new Date().getFullYear();
});
// ===========================
// Error Handling for Images
// ===========================
document.querySelectorAll('img').forEach(img => {
img.addEventListener('error', function() {
this.style.display = 'none';
console.warn(`Failed to load image: ${this.src}`);
});
});
// ===========================
// Smooth Page Transitions
// ===========================
document.addEventListener('DOMContentLoaded', () => {
document.body.style.opacity = '0';
setTimeout(() => {
document.body.style.transition = 'opacity 0.5s ease';
document.body.style.opacity = '1';
}, 10);
});
// ===========================
// Interactive Workshop Cards
// ===========================
// Note: workshop card hover styling is handled in CSS (.workshop-card:hover).
// Removed the previous JS that set inline border colors on link hover because
// it could leave inline styles behind and cause persistent purple outlines.
// Clear any leftover inline border-color on workshop cards (fixes persistent outline)
document.addEventListener('DOMContentLoaded', () => {
const workshopCards = document.querySelectorAll('.workshop-card');
workshopCards.forEach(card => {
// remove inline border-color property if present
if (card.style && card.style.borderColor) {
card.style.removeProperty('border-color');
}
});
});
// ===========================
// Progress Bar on Scroll (Optional)
// ===========================
function createProgressBar() {
const progressBar = document.createElement('div');
progressBar.id = 'scroll-progress';
progressBar.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 0%;
height: 3px;
background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
z-index: 9999;
transition: width 0.2s ease;
`;
document.body.appendChild(progressBar);
window.addEventListener('scroll', () => {
const windowHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
const scrolled = (window.scrollY / windowHeight) * 100;
progressBar.style.width = scrolled + '%';
});
}
// Uncomment to enable scroll progress bar
// createProgressBar();
// ===========================
// Console Welcome Message
// ===========================
console.log('%c🔐 CyberSafe', 'font-size: 24px; font-weight: bold; color: #4F46E5;');
console.log('%cPractical Cybersecurity for Everyone', 'font-size: 14px; color: #6B7280;');
console.log('%cStay safe online! 🛡️', 'font-size: 12px; color: #10B981;');
// ===========================
// Service Worker Registration (for PWA)
// ===========================
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
// Uncomment when you create a service worker file
// navigator.serviceWorker.register('/sw.js')
// .then(registration => console.log('SW registered'))
// .catch(err => console.log('SW registration failed'));
});
}