File size: 5,093 Bytes
1b9ded1 | 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 | // Intersection Observer for Counter Animation
const counterObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const counters = entry.target.querySelectorAll('.counter');
counters.forEach(counter => {
const target = parseInt(counter.getAttribute('data-target'));
const duration = 2000; // 2 seconds
const step = target / (duration / 16); // 60fps
let current = 0;
const updateCounter = () => {
current += step;
if (current < target) {
if (target > 1000) {
counter.innerText = '$' + Math.floor(current).toLocaleString();
} else if (target > 100) {
counter.innerText = (current / 10).toFixed(1) + '%';
} else {
counter.innerText = Math.floor(current);
}
requestAnimationFrame(updateCounter);
} else {
if (target > 1000) {
counter.innerText = '$' + target.toLocaleString();
} else if (target > 100) {
counter.innerText = (target / 10).toFixed(1) + '%';
} else {
counter.innerText = target;
}
}
};
updateCounter();
});
counterObserver.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
// Observe counter sections
document.addEventListener('DOMContentLoaded', () => {
const counterSections = document.querySelectorAll('.counter');
if (counterSections.length > 0) {
const parent = counterSections[0].closest('section');
if (parent) counterObserver.observe(parent);
}
// Initialize Feather Icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Navigation Scroll Effect
const nav = document.querySelector('custom-navbar');
if (nav) {
window.addEventListener('scroll', () => {
if (window.scrollY > 50) {
nav.classList.add('nav-scrolled');
} else {
nav.classList.remove('nav-scrolled');
}
});
}
// Smooth Scroll for Anchor Links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Parallax Effect for Hero
const hero = document.querySelector('section:first-of-type');
if (hero) {
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const parallax = hero.querySelector('img');
if (parallax) {
parallax.style.transform = `translateY(${scrolled * 0.5}px)`;
}
});
}
// Intersection Observer for Fade-in Elements
const fadeObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in-up');
fadeObserver.unobserve(entry.target);
}
});
}, { threshold: 0.1 });
document.querySelectorAll('section').forEach(section => {
fadeObserver.observe(section);
});
});
// Mobile Menu Toggle
function toggleMobileMenu() {
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenu) {
mobileMenu.classList.toggle('hidden');
}
}
// Form Validation Helper
function validateForm(formElement) {
const inputs = formElement.querySelectorAll('input[required], textarea[required]');
let isValid = true;
inputs.forEach(input => {
if (!input.value.trim()) {
isValid = false;
input.classList.add('border-red-500');
} else {
input.classList.remove('border-red-500');
}
});
return isValid;
}
// Lazy Loading Images
if ('IntersectionObserver' in window) {
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src || img.src;
img.classList.remove('lazy');
imageObserver.unobserve(img);
}
});
});
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('img.lazy').forEach(img => imageObserver.observe(img));
});
} |