|
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
| 2 | + // Elements |
| 3 | + const slider = document.querySelector(".slider"); |
| 4 | + const prevBtn = document.getElementById("prev-btn"); |
| 5 | + const nextBtn = document.getElementById("next-btn"); |
| 6 | + const pagination = document.querySelector(".pagination"); |
| 7 | + const cards = document.querySelectorAll(".card"); |
| 8 | + |
| 9 | + // State |
| 10 | + let currentIndex = 0; |
| 11 | + let cardsPerView = getCardsPerView(); |
| 12 | + let totalSlides = Math.ceil(cards.length / cardsPerView); |
| 13 | + |
| 14 | + // Initialize |
| 15 | + createPaginationDots(); |
| 16 | + updateSlider(); |
| 17 | + |
| 18 | + // Event listeners |
| 19 | + prevBtn.addEventListener("click", goToPrevSlide); |
| 20 | + nextBtn.addEventListener("click", goToNextSlide); |
| 21 | + window.addEventListener("resize", handleResize); |
| 22 | + |
| 23 | + // Functions |
| 24 | + function getCardsPerView() { |
| 25 | + const windowWidth = window.innerWidth; |
| 26 | + if (windowWidth >= 1024) return 4; |
| 27 | + if (windowWidth >= 768) return 3; |
| 28 | + if (windowWidth >= 640) return 2; |
| 29 | + return 1; |
| 30 | + } |
| 31 | + |
| 32 | + function createPaginationDots() { |
| 33 | + pagination.innerHTML = ""; |
| 34 | + for (let i = 0; i < totalSlides; i++) { |
| 35 | + const dot = document.createElement("div"); |
| 36 | + dot.classList.add("dot"); |
| 37 | + if (i === currentIndex) dot.classList.add("active"); |
| 38 | + dot.addEventListener("click", () => goToSlide(i)); |
| 39 | + pagination.appendChild(dot); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + function goToSlide(index) { |
| 44 | + currentIndex = index; |
| 45 | + updateSlider(); |
| 46 | + } |
| 47 | + |
| 48 | + function goToPrevSlide() { |
| 49 | + currentIndex = Math.max(currentIndex - 1, 0); |
| 50 | + updateSlider(); |
| 51 | + } |
| 52 | + |
| 53 | + function goToNextSlide() { |
| 54 | + currentIndex = Math.min(currentIndex + 1, totalSlides - 1); |
| 55 | + updateSlider(); |
| 56 | + } |
| 57 | + |
| 58 | + function updateSlider() { |
| 59 | + // Calculate the slide position |
| 60 | + const slideWidth = cards[0].offsetWidth; |
| 61 | + const gapWidth = 16; // 1rem = 16px |
| 62 | + const offset = currentIndex * cardsPerView * (slideWidth + gapWidth); |
| 63 | + |
| 64 | + // Update slider position |
| 65 | + slider.style.transform = `translateX(-${offset}px)`; |
| 66 | + |
| 67 | + // Update pagination |
| 68 | + document.querySelectorAll(".dot").forEach((dot, i) => { |
| 69 | + dot.classList.toggle("active", i === currentIndex); |
| 70 | + }); |
| 71 | + |
| 72 | + // Update button state |
| 73 | + prevBtn.disabled = currentIndex === 0; |
| 74 | + nextBtn.disabled = currentIndex === totalSlides - 1; |
| 75 | + |
| 76 | + // Visual feedback on buttons |
| 77 | + prevBtn.style.opacity = currentIndex === 0 ? "0.5" : "1"; |
| 78 | + nextBtn.style.opacity = currentIndex === totalSlides - 1 ? "0.5" : "1"; |
| 79 | + } |
| 80 | + |
| 81 | + function handleResize() { |
| 82 | + const newCardsPerView = getCardsPerView(); |
| 83 | + if (newCardsPerView !== cardsPerView) { |
| 84 | + cardsPerView = newCardsPerView; |
| 85 | + totalSlides = Math.ceil(cards.length / cardsPerView); |
| 86 | + currentIndex = Math.min(currentIndex, totalSlides - 1); |
| 87 | + createPaginationDots(); |
| 88 | + updateSlider(); |
| 89 | + } else { |
| 90 | + updateSlider(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Add touch swipe support |
| 95 | + let touchStartX = 0; |
| 96 | + let touchEndX = 0; |
| 97 | + |
| 98 | + slider.addEventListener( |
| 99 | + "touchstart", |
| 100 | + (e) => { |
| 101 | + touchStartX = e.changedTouches[0].screenX; |
| 102 | + }, |
| 103 | + false |
| 104 | + ); |
| 105 | + |
| 106 | + slider.addEventListener( |
| 107 | + "touchend", |
| 108 | + (e) => { |
| 109 | + touchEndX = e.changedTouches[0].screenX; |
| 110 | + handleSwipe(); |
| 111 | + }, |
| 112 | + false |
| 113 | + ); |
| 114 | + |
| 115 | + function handleSwipe() { |
| 116 | + const minSwipeDistance = 50; |
| 117 | + if (touchStartX - touchEndX > minSwipeDistance) { |
| 118 | + goToNextSlide(); |
| 119 | + } else if (touchEndX - touchStartX > minSwipeDistance) { |
| 120 | + goToPrevSlide(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Add keyboard support |
| 125 | + document.addEventListener("keydown", (e) => { |
| 126 | + if (e.key === "ArrowLeft") { |
| 127 | + goToPrevSlide(); |
| 128 | + } else if (e.key === "ArrowRight") { |
| 129 | + goToNextSlide(); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + // Auto-play functionality (optional) |
| 134 | + let autoplayInterval; |
| 135 | + const autoplayDelay = 5000; // 5 seconds |
| 136 | + |
| 137 | + function startAutoplay() { |
| 138 | + stopAutoplay(); // Clear any existing interval |
| 139 | + autoplayInterval = setInterval(() => { |
| 140 | + if (currentIndex < totalSlides - 1) { |
| 141 | + goToNextSlide(); |
| 142 | + } else { |
| 143 | + goToSlide(0); |
| 144 | + } |
| 145 | + }, autoplayDelay); |
| 146 | + } |
| 147 | + |
| 148 | + function stopAutoplay() { |
| 149 | + if (autoplayInterval) { |
| 150 | + clearInterval(autoplayInterval); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Uncomment the line below to enable autoplay |
| 155 | + // startAutoplay(); |
| 156 | + |
| 157 | + // Stop autoplay on user interaction |
| 158 | + slider.addEventListener("mouseenter", stopAutoplay); |
| 159 | + slider.addEventListener("touchstart", stopAutoplay); |
| 160 | + prevBtn.addEventListener("click", stopAutoplay); |
| 161 | + nextBtn.addEventListener("click", stopAutoplay); |
| 162 | + pagination.addEventListener("click", stopAutoplay); |
| 163 | +}); |
0 commit comments