| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>Image Carousel</title> |
| <style> |
| * { |
| margin: 0; |
| padding: 0; |
| box-sizing: border-box; |
| } |
| |
| body { |
| font-family: Arial, sans-serif; |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| min-height: 100vh; |
| background: #f0f0f0; |
| } |
| |
| .carousel-container { |
| width: 80%; |
| max-width: 800px; |
| position: relative; |
| overflow: hidden; |
| border-radius: 10px; |
| box-shadow: 0 10px 20px rgba(0,0,0,0.2); |
| } |
| |
| .carousel { |
| display: flex; |
| transition: transform 0.5s ease-in-out; |
| } |
| |
| .slide { |
| min-width: 100%; |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| } |
| |
| .slide img { |
| max-width: 100%; |
| max-height: 60vh; |
| object-fit: contain; |
| display: block; |
| } |
| |
| .nav-btn { |
| position: absolute; |
| top: 50%; |
| transform: translateY(-50%); |
| background: rgba(0,0,0,0.5); |
| color: white; |
| border: none; |
| font-size: 24px; |
| width: 50px; |
| height: 50px; |
| border-radius: 50%; |
| cursor: pointer; |
| display: flex; |
| align-items: center; |
| justify-content: center; |
| } |
| |
| .prev { |
| left: 15px; |
| } |
| |
| .next { |
| right: 15px; |
| } |
| |
| .indicators { |
| display: flex; |
| justify-content: center; |
| margin-top: 15px; |
| gap: 10px; |
| } |
| |
| .indicator { |
| width: 12px; |
| height: 12px; |
| border-radius: 50%; |
| background: #bbb; |
| cursor: pointer; |
| transition: background 0.3s; |
| } |
| |
| .indicator.active { |
| background: #333; |
| } |
| </style> |
| </head> |
| <body> |
| <div class="carousel-container"> |
| <div class="carousel" id="carousel"> |
| <div class="slide"><img src="https://placehold.co/800x500/6366f1/white?text=Image+1" alt="Image 1"></div> |
| <div class="slide"><img src="https://placehold.co/800x500/8b5cf6/white?text=Image+2" alt="Image 2"></div> |
| <div class="slide"><img src="https://placehold.co/800x500/10b981/white?text=Image+3" alt="Image 3"></div> |
| <div class="slide"><img src="https://placehold.co/800x500/f59e0b/white?text=Image+4" alt="Image 4"></div> |
| <div class="slide"><img src="https://placehold.co/800x500/ef4444/white?text=Image+5" alt="Image 5"></div> |
| </div> |
| <button class="nav-btn prev" id="prevBtn">‹</button> |
| <button class="nav-btn next" id="nextBtn">›</button> |
| <div class="indicators" id="indicators"></div> |
| </div> |
|
|
| <script> |
| document.addEventListener('DOMContentLoaded', () => { |
| const carousel = document.getElementById('carousel'); |
| const slides = document.querySelectorAll('.slide'); |
| const prevBtn = document.getElementById('prevBtn'); |
| const nextBtn = document.getElementById('nextBtn'); |
| const indicatorsContainer = document.getElementById('indicators'); |
| |
| let currentIndex = 0; |
| const totalSlides = slides.length; |
| |
| |
| slides.forEach((_, index) => { |
| const indicator = document.createElement('div'); |
| indicator.classList.add('indicator'); |
| if (index === 0) indicator.classList.add('active'); |
| indicator.dataset.index = index; |
| indicator.addEventListener('click', () => goToSlide(index)); |
| indicatorsContainer.appendChild(indicator); |
| }); |
| |
| const updateCarousel = () => { |
| carousel.style.transform = `translateX(-${currentIndex * 100}%)`; |
| |
| |
| document.querySelectorAll('.indicator').forEach((dot, index) => { |
| dot.classList.toggle('active', index === currentIndex); |
| }); |
| }; |
| |
| const nextSlide = () => { |
| currentIndex = (currentIndex + 1) % totalSlides; |
| updateCarousel(); |
| }; |
| |
| const prevSlide = () => { |
| currentIndex = (currentIndex - 1 + totalSlides) % totalSlides; |
| updateCarousel(); |
| }; |
| |
| const goToSlide = (index) => { |
| currentIndex = index; |
| updateCarousel(); |
| }; |
| |
| |
| nextBtn.addEventListener('click', nextSlide); |
| prevBtn.addEventListener('click', prevSlide); |
| |
| |
| setInterval(nextSlide, 5000); |
| }); |
| </script> |
| </body> |
| </html> |