Untitled
unknown
plain_text
8 days ago
2.0 kB
5
Indexable
document.addEventListener('DOMContentLoaded', () => { const track = document.querySelector('.carousel-track'); const items = document.querySelectorAll('.cultural-item'); const dotsContainer = document.querySelector('.carousel-dots'); const prevBtn = document.querySelector('.prev-btn'); const nextBtn = document.querySelector('.next-btn'); let currentIndex = 0; // Create dots items.forEach((_, index) => { const dot = document.createElement('button'); dot.classList.add('carousel-dot'); dot.addEventListener('click', () => goToSlide(index)); dotsContainer.appendChild(dot); }); // Initialize first slide updateCarousel(); function updateCarousel() { // Calculate slide position const slideWidth = items[0].clientWidth; track.style.transform = `translateX(-${currentIndex * slideWidth}px)`; // Update opacity and dots items.forEach((item, index) => { item.style.opacity = index === currentIndex ? '1' : '0'; }); document.querySelectorAll('.carousel-dot').forEach((dot, index) => { dot.classList.toggle('active', index === currentIndex); }); } function goToSlide(index) { currentIndex = index; updateCarousel(); } function nextSlide() { currentIndex = (currentIndex + 1) % items.length; updateCarousel(); } function prevSlide() { currentIndex = (currentIndex - 1 + items.length) % items.length; updateCarousel(); } // Event listeners prevBtn.addEventListener('click', prevSlide); nextBtn.addEventListener('click', nextSlide); // Auto-play (optional) let autoPlay = setInterval(nextSlide, 5000); // Pause on hover track.addEventListener('mouseenter', () => clearInterval(autoPlay)); track.addEventListener('mouseleave', () => autoPlay = setInterval(nextSlide, 5000)); });
Editor is loading...
Leave a Comment