wp-wellnet-elevate / script.js
CSNET's picture
สร้างเว็บที่นำเสนอโครงการที่สอดคล้องแนวคิดนี้
1922e6b verified
Raw History Blame Contribute Delete
8.02 kB
// Smooth scroll for navigation 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'
});
}
});
});
// Active navigation highlighting
window.addEventListener('scroll', () => {
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('nav a[href^="#"]');
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (scrollY >= (sectionTop - 200)) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('nav-active');
if (link.getAttribute('href').slice(1) === current) {
link.classList.add('nav-active');
}
});
});
// Mobile menu toggle
const mobileMenuButton = document.createElement('button');
mobileMenuButton.className = 'md:hidden text-gray-700 hover:text-teal-600 transition-colors';
mobileMenuButton.innerHTML = '<i data-feather="menu"></i>';
mobileMenuButton.onclick = toggleMobileMenu;
function toggleMobileMenu() {
const mobileMenu = document.querySelector('.mobile-menu');
if (mobileMenu) {
mobileMenu.classList.toggle('hidden');
}
}
// Add mobile menu to navigation
const nav = document.querySelector('nav');
if (nav && window.innerWidth < 768) {
const navContainer = nav.querySelector('.max-w-7xl');
const rightItems = navContainer.querySelector('.flex.items-center:last-child');
if (rightItems) {
rightItems.insertBefore(mobileMenuButton, rightItems.firstChild);
}
}
// Intersection Observer for animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('fade-in-up');
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Observe elements for animation
document.querySelectorAll('section > div, .card-hover').forEach(el => {
observer.observe(el);
});
// Counter animation for statistics
function animateCounter(element, target, duration = 2000) {
let start = 0;
const increment = target / (duration / 16);
const updateCounter = () => {
start += increment;
if (start < target) {
element.textContent = Math.floor(start);
requestAnimationFrame(updateCounter);
} else {
element.textContent = target;
}
};
updateCounter();
}
// Initialize counters when visible
const counters = document.querySelectorAll('[data-counter]');
counters.forEach(counter => {
const target = parseInt(counter.dataset.counter);
const counterObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
animateCounter(entry.target, target);
counterObserver.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
counterObserver.observe(counter);
});
// Form handling
const forms = document.querySelectorAll('form');
forms.forEach(form => {
form.addEventListener('submit', (e) => {
e.preventDefault();
// Add form submission logic here
showNotification('ข้อความของคุณถูกส่งเรียบร้อยแล้ว', 'success');
form.reset();
});
});
// Notification system
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `fixed top-20 right-4 px-6 py-4 rounded-lg shadow-lg z-50 transform transition-all duration-300 translate-x-full`;
const colors = {
success: 'bg-green-500 text-white',
error: 'bg-red-500 text-white',
info: 'bg-blue-500 text-white',
warning: 'bg-yellow-500 text-white'
};
notification.classList.add(...colors[type].split(' '));
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.classList.remove('translate-x-full');
}, 100);
setTimeout(() => {
notification.classList.add('translate-x-full');
setTimeout(() => {
document.body.removeChild(notification);
}, 300);
}, 3000);
}
// Lazy loading for 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.classList.remove('opacity-0');
imageObserver.unobserve(img);
}
});
});
document.querySelectorAll('img[data-src]').forEach(img => {
imageObserver.observe(img);
});
}
// Add parallax effect to hero section
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const parallax = document.querySelector('#home');
if (parallax) {
parallax.style.transform = `translateY(${scrolled * 0.5}px)`;
}
});
// Add hover effect to cards
document.querySelectorAll('.card-hover').forEach(card => {
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-10px) scale(1.02)';
});
card.addEventListener('mouseleave', function() {
this.style.transform = 'translateY(0) scale(1)';
});
});
// Copy link functionality
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
showNotification('คัดลอกลิงก์เรียบร้อยแล้ว', 'success');
}).catch(() => {
showNotification('ไม่สามารถคัดลอกลิงก์ได้', 'error');
});
}
// Initialize Feather Icons
document.addEventListener('DOMContentLoaded', () => {
feather.replace();
});
// Add loading states for buttons
document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', function() {
if (this.type !== 'submit') {
const originalContent = this.innerHTML;
this.innerHTML = '<i data-feather="loader" class="animate-spin"></i> กำลังดำเนินการ...';
this.disabled = true;
setTimeout(() => {
this.innerHTML = originalContent;
this.disabled = false;
feather.replace();
}, 2000);
}
});
});
// Dynamic year in footer
const yearElements = document.querySelectorAll('[data-year]');
yearElements.forEach(element => {
element.textContent = new Date().getFullYear();
});
// Add keyboard navigation support
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
// Close modals or menus
const openModal = document.querySelector('.modal:not(.hidden)');
if (openModal) {
openModal.classList.add('hidden');
}
}
});
// Performance monitoring
const pageLoadTime = performance.now();
console.log(`Page loaded in ${pageLoadTime}ms`);
// Service Worker for PWA
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then(registration => {
console.log('SW registered: ', registration);
})
.catch(registrationError => {
console.log('SW registration failed: ', registrationError);
});
});
}