-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
250 lines (210 loc) · 7.5 KB
/
Copy pathscript.js
File metadata and controls
250 lines (210 loc) · 7.5 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
/**
* Client-side scripting for David Ruiz Urraca CV website.
* Contains interactive card spotlights, dynamic subtitle typing,
* collapsible experiences, copy-to-clipboard actions, and mobile menu behaviors.
*/
document.addEventListener('DOMContentLoaded', () => {
initStickyHeader();
initMobileMenu();
initCardSpotlights();
initTypingEffect();
initCollapsibleExperiences();
initCopyEmail();
initActiveNavHighlight();
});
/* -----------------------------------------
1. Sticky Header
----------------------------------------- */
function initStickyHeader() {
const header = document.getElementById('main-header');
if (!header) return;
const checkScroll = () => {
if (window.scrollY > 50) {
header.classList.add('scrolled');
} else {
header.classList.remove('scrolled');
}
};
window.addEventListener('scroll', checkScroll);
checkScroll(); // Initial check
}
/* -----------------------------------------
2. Mobile Menu Navigation
----------------------------------------- */
function initMobileMenu() {
const menuToggle = document.getElementById('menu-toggle');
const navMenu = document.getElementById('nav-menu');
const navLinks = document.querySelectorAll('.nav-item');
if (!menuToggle || !navMenu) return;
const toggleMenu = () => {
menuToggle.classList.toggle('active');
navMenu.classList.toggle('mobile-open');
document.body.classList.toggle('no-scroll');
};
const closeMenu = () => {
menuToggle.classList.remove('active');
navMenu.classList.remove('mobile-open');
document.body.classList.remove('no-scroll');
};
menuToggle.addEventListener('click', toggleMenu);
navLinks.forEach(link => {
link.addEventListener('click', closeMenu);
});
}
/* -----------------------------------------
3. Spotlight Mouse-Tracking Effect
----------------------------------------- */
function initCardSpotlights() {
if (!window.matchMedia('(hover: hover)').matches) return;
const cards = document.querySelectorAll('.spotlight-card');
cards.forEach(card => {
card.addEventListener('mousemove', e => {
const rect = card.getBoundingClientRect();
// Calculate mouse position relative to the element
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// Update custom properties in CSS
card.style.setProperty('--mouse-x', `${x}px`);
card.style.setProperty('--mouse-y', `${y}px`);
});
});
}
/* -----------------------------------------
4. Typing Effect for Subtitle
----------------------------------------- */
function initTypingEffect() {
const element = document.getElementById('typing-text');
if (!element) return;
const isSpanish = document.documentElement.lang === 'es';
const roles = isSpanish ? [
"Ingeniero Android",
"Especialista en Flutter",
"Kotlin Multiplatform (KMP) Explorer",
"Desarrollador AI-First"
] : [
"Android Engineer",
"Flutter Specialist",
"Kotlin Multiplatform (KMP) Explorer",
"AI-First Developer"
];
// Disable typing animation if reduced motion is preferred
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) {
element.textContent = isSpanish ? "Desarrollador Android y Flutter" : "Android & Flutter Developer";
return;
}
let roleIdx = 0;
let charIdx = 0;
let isDeleting = false;
let typingSpeed = 100;
function type() {
const currentRole = roles[roleIdx];
if (isDeleting) {
// Deleting character
element.textContent = currentRole.substring(0, charIdx - 1);
charIdx--;
typingSpeed = 50; // Deletes faster than typing
} else {
// Typing character
element.textContent = currentRole.substring(0, charIdx + 1);
charIdx++;
typingSpeed = 100;
}
// Switch states
if (!isDeleting && charIdx === currentRole.length) {
// Fully typed: pause before deleting
isDeleting = true;
typingSpeed = 2000; // Pause at the end of word
} else if (isDeleting && charIdx === 0) {
// Fully deleted: move to next word
isDeleting = false;
roleIdx = (roleIdx + 1) % roles.length;
typingSpeed = 500; // Pause before typing next
}
setTimeout(type, typingSpeed);
}
// Start typing
setTimeout(type, 1000);
}
/* -----------------------------------------
5. Collapsible Experiences
----------------------------------------- */
function initCollapsibleExperiences() {
const toggleBtn = document.getElementById('toggle-older-exp');
const olderContainer = document.getElementById('older-experiences');
if (!toggleBtn || !olderContainer) return;
const isSpanish = document.documentElement.lang === 'es';
toggleBtn.addEventListener('click', () => {
const isExpanded = olderContainer.classList.contains('expanded');
const textSpan = toggleBtn.querySelector('span');
if (isExpanded) {
olderContainer.style.maxHeight = null;
textSpan.textContent = isSpanish ? "Mostrar experiencia anterior" : "Show Older Experience";
} else {
// Programmatically set max-height based on content height for a smooth transition
olderContainer.style.maxHeight = olderContainer.scrollHeight + "px";
textSpan.textContent = isSpanish ? "Ocultar experiencia anterior" : "Hide Older Experience";
}
olderContainer.classList.toggle('expanded');
toggleBtn.classList.toggle('active');
});
}
/* -----------------------------------------
6. Copy Email to Clipboard
----------------------------------------- */
function initCopyEmail() {
const copyBtn = document.getElementById('btn-copy-email');
const tooltip = document.getElementById('copy-tooltip');
const emailText = "david@ruizurraca.com";
if (!copyBtn || !tooltip) return;
const isSpanish = document.documentElement.lang === 'es';
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(emailText)
.then(() => {
// Successful copy
tooltip.textContent = isSpanish ? "¡Copiado!" : "Copied!";
copyBtn.classList.add('copied');
// Reset after 2 seconds
setTimeout(() => {
tooltip.textContent = isSpanish ? "Copiar" : "Copy";
copyBtn.classList.remove('copied');
}, 2000);
})
.catch(err => {
console.error("Failed to copy email: ", err);
tooltip.textContent = isSpanish ? "Error" : "Error";
});
});
}
/* -----------------------------------------
7. Navigation Active Section Highlight
----------------------------------------- */
function initActiveNavHighlight() {
const sections = document.querySelectorAll('section.scroll-offset');
const navItems = document.querySelectorAll('.nav-item');
if (sections.length === 0 || navItems.length === 0) return;
const options = {
root: null,
rootMargin: '-30% 0px -60% 0px', // Trigger when section occupies the middle portion of viewport
threshold: 0
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const id = entry.target.getAttribute('id');
// Update active class on nav links
navItems.forEach(item => {
const href = item.getAttribute('href');
if (href === `#${id}`) {
item.classList.add('active');
} else {
item.classList.remove('active');
}
});
}
});
}, options);
sections.forEach(section => {
observer.observe(section);
});
}