Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
341 lines (295 loc) · 11 KB
/
Copy pathscript.js
File metadata and controls
341 lines (295 loc) · 11 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
// Commander Wars Homepage - Single Page with Smooth Scroll JavaScript
// ===== HAMBURGER MENU =====
const hamburger = document.getElementById('hamburger');
const mainNav = document.getElementById('mainNav');
if (hamburger && mainNav) {
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
mainNav.classList.toggle('active');
});
// Close menu when clicking a link
const navLinks = mainNav.querySelectorAll('a');
navLinks.forEach(link => {
link.addEventListener('click', () => {
hamburger.classList.remove('active');
mainNav.classList.remove('active');
});
});
// Close menu when clicking outside
document.addEventListener('click', (e) => {
if (!mainNav.contains(e.target) && !hamburger.contains(e.target)) {
hamburger.classList.remove('active');
mainNav.classList.remove('active');
}
});
}
// ===== SMOOTH SCROLLING =====
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
const navHeight = 60; // Height of fixed nav
const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - navHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
// ===== ACTIVE NAVIGATION HIGHLIGHTING =====
const sections = document.querySelectorAll('section[id]');
const navLinksAll = document.querySelectorAll('.main-nav a[href^="#"]');
function highlightActiveSection() {
const scrollPosition = window.scrollY + 100;
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
const sectionId = section.getAttribute('id');
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
navLinksAll.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${sectionId}`) {
link.classList.add('active');
}
});
}
});
}
window.addEventListener('scroll', highlightActiveSection);
window.addEventListener('load', highlightActiveSection);
// ===== VIDEO AUTOPLAY HANDLING =====
const video = document.querySelector('.video-bg video');
if (video) {
// Try to autoplay
video.play().catch(err => {
console.log('Video autoplay prevented:', err);
// Add a play button overlay if autoplay fails
const playButton = document.createElement('button');
playButton.textContent = '▶ PLAY VIDEO';
playButton.style.cssText = `
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 100;
padding: 20px 40px;
background: #FF0000;
color: #FFF;
border: 5px solid #000;
font-size: 20px;
font-weight: 900;
cursor: pointer;
`;
playButton.addEventListener('click', () => {
video.play();
playButton.remove();
});
document.querySelector('.video-bg').appendChild(playButton);
});
}
// ===== EXTERNAL LINK SAFETY =====
document.querySelectorAll('a[target="_blank"]').forEach(link => {
link.setAttribute('rel', 'noopener noreferrer');
});
// ===== LAZY LOADING FOR GALLERY =====
if ('IntersectionObserver' in window) {
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.classList.add('loaded');
imageObserver.unobserve(img);
}
});
}, {
rootMargin: '50px'
});
// Observe all lazy images in gallery
document.querySelectorAll('.gallery-grid img[loading="lazy"]').forEach(img => {
imageObserver.observe(img);
});
}
// ===== KONAMI CODE EASTER EGG =====
let konamiCode = [];
const konamiSequence = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a'];
document.addEventListener('keydown', (e) => {
konamiCode.push(e.key);
konamiCode = konamiCode.slice(-10);
if (konamiCode.join(',') === konamiSequence.join(',')) {
activateKonamiCode();
}
});
function activateKonamiCode() {
// Create confetti effect
const colors = ['#FF0000', '#0066FF', '#00CC00', '#FFD700', '#FF00FF'];
for (let i = 0; i < 50; i++) {
setTimeout(() => {
const confetti = document.createElement('div');
confetti.style.cssText = `
position: fixed;
top: -20px;
left: ${Math.random() * 100}vw;
width: 15px;
height: 15px;
background: ${colors[Math.floor(Math.random() * colors.length)]};
border: 2px solid #000;
animation: confettiFall ${2 + Math.random() * 2}s linear forwards;
pointer-events: none;
z-index: 10000;
`;
document.body.appendChild(confetti);
setTimeout(() => confetti.remove(), 4000);
}, i * 50);
}
// Add animation if not already present
if (!document.querySelector('#confetti-style')) {
const style = document.createElement('style');
style.id = 'confetti-style';
style.textContent = `
@keyframes confettiFall {
to {
transform: translateY(100vh) rotate(${Math.random() * 360}deg);
opacity: 0;
}
}
`;
document.head.appendChild(style);
}
// Show message
const message = document.createElement('div');
message.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #FFF;
border: 8px solid #000;
box-shadow: 12px 12px 0 rgba(0, 0, 0, 0.8);
color: #FF0000;
padding: 40px 60px;
font-size: 32px;
font-weight: 900;
text-align: center;
z-index: 10001;
`;
message.innerHTML = `
<div style="font-size: 48px; margin-bottom: 10px;">🎮</div>
COMMANDER WARS<br>FOREVER!
<div style="font-size: 48px; margin-top: 10px;">🎮</div>
`;
document.body.appendChild(message);
setTimeout(() => {
message.style.transition = 'opacity 0.5s';
message.style.opacity = '0';
setTimeout(() => message.remove(), 500);
}, 3000);
}
// ===== CONSOLE ART =====
console.log(`%c
╔═══════════════════════════════════════╗
║ COMMANDER WARS HOMEPAGE v3.0 ║
║ Advance Wars 2 Inspired Design ║
╚═══════════════════════════════════════╝
`, 'color: #FF0000; font-weight: bold; font-size: 14px;');
console.log('%cKeyboard shortcut:', 'color: #0066FF; font-weight: bold;');
console.log('%cTry the Konami Code: ↑↑↓↓←→←→BA', 'color: #00CC00; font-weight: bold;');
// ===== LOAD COMPLETE =====
window.addEventListener('load', () => {
console.log('%c✓ Commander Wars Homepage Loaded Successfully!', 'color: #00CC00; font-weight: bold; font-size: 16px;');
console.log('Features: Smooth scroll, hamburger menu, video background, horizontal gallery');
});
// ===== PERFORMANCE: PAUSE VIDEO WHEN TAB NOT VISIBLE =====
document.addEventListener('visibilitychange', () => {
if (video) {
if (document.hidden) {
video.pause();
} else {
video.play().catch(err => console.log('Video play prevented'));
}
}
});
// ===== ACCESSIBILITY: ANNOUNCE SECTION CHANGES FOR SCREEN READERS =====
const announcer = document.createElement('div');
announcer.setAttribute('aria-live', 'polite');
announcer.setAttribute('aria-atomic', 'true');
announcer.className = 'sr-only';
announcer.style.cssText = `
position: absolute;
left: -10000px;
width: 1px;
height: 1px;
overflow: hidden;
`;
document.body.appendChild(announcer);
let lastSection = '';
window.addEventListener('scroll', () => {
const scrollPosition = window.scrollY + 150;
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
const sectionId = section.getAttribute('id');
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
if (sectionId !== lastSection) {
const sectionName = sectionId.charAt(0).toUpperCase() + sectionId.slice(1);
announcer.textContent = `${sectionName} section`;
lastSection = sectionId;
}
}
});
});
// ===== AUTO-SCROLLING HORIZONTAL GALLERY (Desktop & Mobile) =====
const galleryScroll = document.querySelector('.gallery-scroll');
if (galleryScroll && window.innerWidth > 768) {
let scrollPosition = 0;
let isAutoScrolling = true;
let userInteracted = false;
let lastTime = 0;
const scrollSpeed = 0.3; // Reduced from 1 to 0.3 pixels per frame
// Auto-scroll function with speed control
const autoScroll = (currentTime) => {
if (!isAutoScrolling || userInteracted) return;
// Control scroll speed using time delta
if (!lastTime) lastTime = currentTime;
const deltaTime = currentTime - lastTime;
if (deltaTime >= 16) { // ~60fps
scrollPosition += scrollSpeed;
galleryScroll.scrollLeft = scrollPosition;
// Reset to start when reaching the end
if (scrollPosition >= galleryScroll.scrollWidth - galleryScroll.clientWidth) {
scrollPosition = 0;
}
lastTime = currentTime;
}
requestAnimationFrame(autoScroll);
};
// Start auto-scrolling
requestAnimationFrame(autoScroll);
// Pause on hover
galleryScroll.addEventListener('mouseenter', () => {
isAutoScrolling = false;
});
// Resume on mouse leave
galleryScroll.addEventListener('mouseleave', () => {
if (!userInteracted) {
isAutoScrolling = true;
lastTime = 0;
requestAnimationFrame(autoScroll);
}
});
// Stop auto-scroll permanently if user manually scrolls
galleryScroll.addEventListener('wheel', () => {
userInteracted = true;
isAutoScrolling = false;
});
galleryScroll.addEventListener('touchstart', () => {
userInteracted = true;
isAutoScrolling = false;
});
}
// ===== ERROR HANDLING =====
window.addEventListener('error', (e) => {
console.error('Error detected:', e.message);
});