File size: 12,763 Bytes
9f30756 | 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 | // Main JavaScript file for portfolio website
document.addEventListener('DOMContentLoaded', function() {
// Initialize all functionality
initMobileMenu();
initSmoothScrolling();
initScrollAnimations();
initFormHandling();
initScrollToTop();
initTypingEffect();
});
// Mobile menu functionality
function initMobileMenu() {
const mobileMenuButton = document.getElementById('mobile-menu-button');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuButton && mobileMenu) {
mobileMenuButton.addEventListener('click', function() {
mobileMenu.classList.toggle('hidden');
// Toggle menu icon
const icon = mobileMenuButton.querySelector('i');
if (mobileMenu.classList.contains('hidden')) {
icon.setAttribute('data-feather', 'menu');
} else {
icon.setAttribute('data-feather', 'x');
}
feather.replace();
});
// Close mobile menu when clicking on a link
const mobileLinks = mobileMenu.querySelectorAll('a');
mobileLinks.forEach(link => {
link.addEventListener('click', function() {
mobileMenu.classList.add('hidden');
const icon = mobileMenuButton.querySelector('i');
icon.setAttribute('data-feather', 'menu');
feather.replace();
});
});
}
}
// Smooth scrolling for navigation links
function initSmoothScrolling() {
const navLinks = document.querySelectorAll('a[href^="#"]');
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetSection = document.querySelector(targetId);
if (targetSection) {
const navHeight = document.querySelector('nav').offsetHeight;
const targetPosition = targetSection.offsetTop - navHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
}
// Scroll-triggered animations
function initScrollAnimations() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in-up');
entry.target.style.opacity = '1';
}
});
}, observerOptions);
// Observe elements for animation
const animateElements = document.querySelectorAll('.card-hover, .bg-white, section > div');
animateElements.forEach(el => {
el.style.opacity = '0';
observer.observe(el);
});
// Scroll-based navigation highlight
window.addEventListener('scroll', highlightActiveNav);
}
function highlightActiveNav() {
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('nav a[href^="#"]');
const navHeight = document.querySelector('nav').offsetHeight;
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop - navHeight - 100;
const sectionHeight = section.offsetHeight;
if (window.pageYOffset >= sectionTop && window.pageYOffset < sectionTop + sectionHeight) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('text-primary-600', 'font-semibold');
link.classList.add('text-secondary-600');
if (link.getAttribute('href') === '#' + current) {
link.classList.remove('text-secondary-600');
link.classList.add('text-primary-600', 'font-semibold');
}
});
}
// Form handling
function initFormHandling() {
const contactForm = document.querySelector('form');
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
// Get form data
const formData = new FormData(this);
const formObject = {};
formData.forEach((value, key) => {
formObject[key] = value;
});
// Show loading state
const submitButton = this.querySelector('button[type="submit"]');
const originalText = submitButton.textContent;
submitButton.textContent = 'Sending...';
submitButton.disabled = true;
// Simulate form submission (replace with actual API call)
setTimeout(() => {
// Show success message
showNotification('Message sent successfully!', 'success');
// Reset form
this.reset();
// Reset button
submitButton.textContent = originalText;
submitButton.disabled = false;
}, 2000);
});
}
}
// Notification system
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `fixed top-20 right-4 z-50 px-6 py-3 rounded-lg shadow-lg transform translate-x-full transition-transform duration-300 ${
type === 'success' ? 'bg-green-500 text-white' :
type === 'error' ? 'bg-red-500 text-white' :
'bg-blue-500 text-white'
}`;
notification.textContent = message;
document.body.appendChild(notification);
// Slide in
setTimeout(() => {
notification.classList.remove('translate-x-full');
}, 100);
// Slide out and remove
setTimeout(() => {
notification.classList.add('translate-x-full');
setTimeout(() => {
document.body.removeChild(notification);
}, 300);
}, 4000);
}
// Scroll to top button
function initScrollToTop() {
// Create scroll to top button
const scrollButton = document.createElement('button');
scrollButton.innerHTML = '<i data-feather="arrow-up"></i>';
scrollButton.className = 'fixed bottom-6 right-6 w-12 h-12 bg-primary-600 text-white rounded-full shadow-lg hover:bg-primary-700 transition-colors duration-300 opacity-0 transform translate-y-full z-50';
scrollButton.style.display = 'none';
document.body.appendChild(scrollButton);
feather.replace();
// Show/hide based on scroll position
window.addEventListener('scroll', function() {
if (window.pageYOffset > 300) {
scrollButton.style.display = 'block';
setTimeout(() => {
scrollButton.classList.remove('opacity-0', 'translate-y-full');
}, 10);
} else {
scrollButton.classList.add('opacity-0', 'translate-y-full');
setTimeout(() => {
scrollButton.style.display = 'none';
}, 300);
}
});
// Scroll to top functionality
scrollButton.addEventListener('click', function() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}
// Typing effect for hero section
function initTypingEffect() {
const heroTitle = document.querySelector('#home h1');
if (heroTitle) {
const text = heroTitle.textContent;
heroTitle.textContent = '';
heroTitle.style.borderRight = '2px solid white';
let i = 0;
const typeWriter = () => {
if (i < text.length) {
heroTitle.textContent += text.charAt(i);
i++;
setTimeout(typeWriter, 100);
} else {
// Remove cursor after typing is complete
setTimeout(() => {
heroTitle.style.borderRight = 'none';
}, 1000);
}
};
// Start typing effect after a short delay
setTimeout(typeWriter, 1000);
}
}
// Parallax effect for hero section
window.addEventListener('scroll', function() {
const scrolled = window.pageYOffset;
const heroSection = document.querySelector('#home');
if (heroSection) {
const rate = scrolled * -0.5;
heroSection.style.transform = `translateY(${rate}px)`;
}
});
// Counter animation
function animateCounters() {
const counters = document.querySelectorAll('[data-count]');
counters.forEach(counter => {
const target = parseInt(counter.getAttribute('data-count'));
const increment = target / 100;
let current = 0;
const timer = setInterval(() => {
current += increment;
counter.textContent = Math.floor(current);
if (current >= target) {
counter.textContent = target;
clearInterval(timer);
}
}, 20);
});
}
// Initialize counter animation when about section is visible
const aboutSection = document.querySelector('#about');
if (aboutSection) {
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
animateCounters();
observer.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
observer.observe(aboutSection);
}
// Portfolio filter functionality (if needed)
function initPortfolioFilter() {
const filterButtons = document.querySelectorAll('[data-filter]');
const portfolioItems = document.querySelectorAll('[data-category]');
filterButtons.forEach(button => {
button.addEventListener('click', function() {
const filter = this.getAttribute('data-filter');
// Update active button
filterButtons.forEach(btn => btn.classList.remove('active'));
this.classList.add('active');
// Filter portfolio items
portfolioItems.forEach(item => {
if (filter === 'all' || item.getAttribute('data-category') === filter) {
item.style.display = 'block';
} else {
item.style.display = 'none';
}
});
});
});
}
// Theme switcher (for future dark mode implementation)
function initThemeSwitcher() {
const themeToggle = document.getElementById('theme-toggle');
if (themeToggle) {
themeToggle.addEventListener('click', function() {
document.body.classList.toggle('dark-theme');
// Save theme preference
const isDark = document.body.classList.contains('dark-theme');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
// Update toggle icon
const icon = this.querySelector('i');
if (isDark) {
icon.setAttribute('data-feather', 'sun');
} else {
icon.setAttribute('data-feather', 'moon');
}
feather.replace();
});
// Load saved theme preference
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.body.classList.add('dark-theme');
}
}
}
// Performance optimization
function optimizePerformance() {
// Lazy load images
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.remove('lazy');
imageObserver.unobserve(img);
}
});
});
images.forEach(img => imageObserver.observe(img));
// Debounce scroll events
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Apply debouncing to scroll events
const debouncedScroll = debounce(() => {
// Scroll-dependent operations
}, 10);
window.addEventListener('scroll', debouncedScroll);
}
// Initialize performance optimizations
optimizePerformance(); |