-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
388 lines (319 loc) · 12.3 KB
/
Copy pathscript.js
File metadata and controls
388 lines (319 loc) · 12.3 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
/**
* Javier Cruz Villarreal - Portfolio JavaScript
* Handles animations, smooth scroll, and interactive elements
*/
(function() {
'use strict';
// =====================================================
// CONFIGURATION
// =====================================================
const CONFIG = {
terminalText: 'whoami && cat profile.txt',
typingSpeed: 80,
scrollOffset: 80,
animationThreshold: 0.15
};
// =====================================================
// DOM ELEMENTS
// =====================================================
const elements = {
nav: document.getElementById('nav'),
navToggle: document.getElementById('nav-toggle'),
navLinks: document.getElementById('nav-links'),
terminalText: document.getElementById('terminal-text'),
sections: document.querySelectorAll('.section'),
animatedElements: document.querySelectorAll('[data-animate]'),
aboutText: document.querySelector('.about-text'),
aboutTerminal: document.querySelector('.about-terminal'),
contactContent: document.querySelector('.contact-content')
};
// =====================================================
// UTILITY FUNCTIONS
// =====================================================
/**
* Debounce function to limit execution rate
*/
function debounce(func, wait = 10) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
/**
* Check if element is in viewport
*/
function isInViewport(element, threshold = 0) {
const rect = element.getBoundingClientRect();
const windowHeight = window.innerHeight || document.documentElement.clientHeight;
return (
rect.top <= windowHeight * (1 - threshold) &&
rect.bottom >= 0
);
}
// =====================================================
// TERMINAL TYPING EFFECT
// =====================================================
function typeTerminalText() {
if (!elements.terminalText) return;
const text = CONFIG.terminalText;
let index = 0;
function type() {
if (index < text.length) {
elements.terminalText.textContent += text.charAt(index);
index++;
setTimeout(type, CONFIG.typingSpeed);
}
}
// Start typing after a short delay
setTimeout(type, 800);
}
// =====================================================
// NAVIGATION
// =====================================================
/**
* Handle navigation scroll state
*/
function handleNavScroll() {
if (!elements.nav) return;
if (window.scrollY > 50) {
elements.nav.classList.add('scrolled');
} else {
elements.nav.classList.remove('scrolled');
}
}
/**
* Toggle mobile navigation
*/
function toggleMobileNav() {
if (!elements.navToggle || !elements.navLinks) return;
elements.navToggle.classList.toggle('active');
elements.navLinks.classList.toggle('active');
// Update ARIA
const isExpanded = elements.navToggle.classList.contains('active');
elements.navToggle.setAttribute('aria-expanded', isExpanded);
// Prevent body scroll when menu is open
document.body.style.overflow = isExpanded ? 'hidden' : '';
}
/**
* Close mobile nav when clicking a link
*/
function closeMobileNav() {
if (!elements.navToggle || !elements.navLinks) return;
elements.navToggle.classList.remove('active');
elements.navLinks.classList.remove('active');
elements.navToggle.setAttribute('aria-expanded', 'false');
document.body.style.overflow = '';
}
/**
* Smooth scroll to section
*/
function smoothScrollTo(target) {
const element = document.querySelector(target);
if (!element) return;
const offsetTop = element.offsetTop - CONFIG.scrollOffset;
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
// Update URL without jumping
history.pushState(null, '', target);
}
// =====================================================
// SCROLL ANIMATIONS
// =====================================================
/**
* Handle scroll animations for elements
*/
function handleScrollAnimations() {
// Animate data-animate elements
elements.animatedElements.forEach(element => {
if (isInViewport(element, CONFIG.animationThreshold)) {
element.classList.add('visible');
}
});
// Animate about section
if (elements.aboutText && isInViewport(elements.aboutText, CONFIG.animationThreshold)) {
elements.aboutText.classList.add('visible');
}
if (elements.aboutTerminal && isInViewport(elements.aboutTerminal, CONFIG.animationThreshold)) {
elements.aboutTerminal.classList.add('visible');
}
// Animate contact section
if (elements.contactContent && isInViewport(elements.contactContent, CONFIG.animationThreshold)) {
elements.contactContent.classList.add('visible');
}
}
/**
* Highlight active navigation link
*/
function highlightActiveNav() {
const scrollPosition = window.scrollY + CONFIG.scrollOffset + 100;
elements.sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
const sectionId = section.getAttribute('id');
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
// Remove active from all links
document.querySelectorAll('.nav-links a').forEach(link => {
link.classList.remove('active');
});
// Add active to current section link
const activeLink = document.querySelector(`.nav-links a[href="#${sectionId}"]`);
if (activeLink) {
activeLink.classList.add('active');
}
}
});
}
// =====================================================
// INTERSECTION OBSERVER (Better Performance)
// =====================================================
function initIntersectionObserver() {
const observerOptions = {
root: null,
rootMargin: '0px 0px -100px 0px',
threshold: CONFIG.animationThreshold
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
// Optional: unobserve after animation
// observer.unobserve(entry.target);
}
});
}, observerOptions);
// Observe all animated elements
elements.animatedElements.forEach(element => {
observer.observe(element);
});
// Observe special elements
if (elements.aboutText) observer.observe(elements.aboutText);
if (elements.aboutTerminal) observer.observe(elements.aboutTerminal);
if (elements.contactContent) observer.observe(elements.contactContent);
}
// =====================================================
// KEYBOARD NAVIGATION
// =====================================================
function handleKeyboardNav(e) {
// Close mobile nav on Escape
if (e.key === 'Escape') {
closeMobileNav();
}
}
// =====================================================
// PARALLAX EFFECT (Subtle)
// =====================================================
function handleParallax() {
const hero = document.querySelector('.hero-content');
if (!hero) return;
const scrolled = window.scrollY;
const rate = scrolled * 0.3;
if (scrolled < window.innerHeight) {
hero.style.transform = `translateY(${rate}px)`;
hero.style.opacity = 1 - (scrolled / window.innerHeight) * 0.5;
}
}
// =====================================================
// GLOW CURSOR EFFECT (Optional)
// =====================================================
function initGlowCursor() {
const glowElements = document.querySelectorAll('.project-card, .skill-category, .timeline-content');
glowElements.forEach(element => {
element.addEventListener('mousemove', (e) => {
const rect = element.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
element.style.setProperty('--mouse-x', `${x}px`);
element.style.setProperty('--mouse-y', `${y}px`);
});
});
}
// =====================================================
// EVENT LISTENERS
// =====================================================
function initEventListeners() {
// Scroll events (debounced for performance)
const debouncedScroll = debounce(() => {
handleNavScroll();
highlightActiveNav();
handleParallax();
}, 5);
window.addEventListener('scroll', debouncedScroll, { passive: true });
// Initial scroll check
handleNavScroll();
// Mobile nav toggle
if (elements.navToggle) {
elements.navToggle.addEventListener('click', toggleMobileNav);
}
// Navigation links
document.querySelectorAll('a[href^="#"]').forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const target = link.getAttribute('href');
smoothScrollTo(target);
closeMobileNav();
});
});
// Keyboard navigation
document.addEventListener('keydown', handleKeyboardNav);
// Close mobile nav when clicking outside
document.addEventListener('click', (e) => {
if (elements.navLinks && elements.navLinks.classList.contains('active')) {
if (!elements.navLinks.contains(e.target) && !elements.navToggle.contains(e.target)) {
closeMobileNav();
}
}
});
// Resize handler
window.addEventListener('resize', debounce(() => {
// Close mobile nav on resize to desktop
if (window.innerWidth > 768) {
closeMobileNav();
}
}, 100));
}
// =====================================================
// PRELOADER (Optional)
// =====================================================
function hidePreloader() {
document.body.classList.add('loaded');
}
// =====================================================
// INITIALIZE
// =====================================================
function init() {
// Start typing effect
typeTerminalText();
// Initialize intersection observer for animations
if ('IntersectionObserver' in window) {
initIntersectionObserver();
} else {
// Fallback for older browsers
window.addEventListener('scroll', debounce(handleScrollAnimations, 10), { passive: true });
handleScrollAnimations();
}
// Initialize event listeners
initEventListeners();
// Initialize glow cursor effect
initGlowCursor();
// Hide preloader after content loads
window.addEventListener('load', hidePreloader);
// Initial animation trigger
setTimeout(() => {
handleScrollAnimations();
}, 100);
console.log('%c Portfolio Loaded ', 'background: #0a0e17; color: #00ff88; padding: 8px 16px; font-family: monospace; font-size: 14px;');
}
// Run initialization when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();