Nature Flow Carousel Slider - MeggiTools
Run
Toggle Theme
Share Link
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Nature Flow Carousel Slider</title> <style> body { margin: 0; font-family: 'Arial', sans-serif; background-color: #e8f5e9; display: flex; justify-content: center; align-items: center; height: 100vh; } .carousel { position: relative; width: 90%; max-width: 1200px; overflow: hidden; border-radius: 25px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3); background-color: #ffffff; } .carousel-track { display: flex; transition: transform 0.8s ease-in-out; } .carousel-slide { min-width: 100%; box-sizing: border-box; } .carousel-slide img { width: 100%; border-radius: 25px; } .carousel-slide-content { position: absolute; bottom: 10%; left: 10%; color: #fff; font-size: 2em; background: rgba(0, 0, 0, 0.7); padding: 20px; border-radius: 15px; } .carousel-slide-content h2 { margin: 0; } .carousel-buttons { position: absolute; top: 50%; width: 100%; display: flex; justify-content: space-between; transform: translateY(-50%); } .carousel-button { background: rgba(0, 0, 0, 0.7); border: none; font-size: 3em; cursor: pointer; color: #fff; border-radius: 50%; width: 80px; height: 80px; display: flex; justify-content: center; align-items: center; } .carousel-indicators { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; justify-content: center; } .carousel-indicator { width: 20px; height: 20px; border-radius: 50%; background: #000; margin: 0 10px; cursor: pointer; transition: background 0.3s; } .carousel-indicator.active { background: #388e3c; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } } .carousel-slide { animation: fadeIn 0.8s ease-in-out; } .carousel-slide-out { animation: fadeOut 0.8s ease-in-out; } </style> </head> <body> <div class="carousel"> <div class="carousel-track"> <div class="carousel-slide"> <img src="https://via.placeholder.com/1200x600/ff7f7f/333333?text=Slide+1" alt="Slide 1"> </div> <div class="carousel-slide"> <img src="https://via.placeholder.com/1200x600/7f7fff/333333?text=Slide+2" alt="Slide 2"> </div> <div class="carousel-slide"> <img src="https://via.placeholder.com/1200x600/7fff7f/333333?text=Slide+3" alt="Slide 3"> </div> </div> <div class="carousel-buttons"> <button class="carousel-button" id="prevBtn">❮</button> <button class="carousel-button" id="nextBtn">❯</button> </div> <div class="carousel-indicators"> <div class="carousel-indicator active" data-slide="0"></div> <div class="carousel-indicator" data-slide="1"></div> <div class="carousel-indicator" data-slide="2"></div> </div> </div> <script> const track = document.querySelector('.carousel-track'); const slides = Array.from(track.children); const nextButton = document.getElementById('nextBtn'); const prevButton = document.getElementById('prevBtn'); const indicators = Array.from(document.querySelectorAll('.carousel-indicator')); let currentSlide = 0; const updateIndicators = (index) => { indicators.forEach(indicator => indicator.classList.remove('active')); indicators[index].classList.add('active'); }; const moveToSlide = (track, currentSlide, targetSlide) => { track.style.transform = 'translateX(-' + targetSlide.style.left + ')'; }; const setSlidePosition = (slide, index) => { slide.style.left = 100 * index + '%'; }; slides.forEach(setSlidePosition); nextButton.addEventListener('click', () => { const currentSlideElement = slides[currentSlide]; currentSlide = (currentSlide + 1) % slides.length; const nextSlideElement = slides[currentSlide]; moveToSlide(track, currentSlideElement, nextSlideElement); updateIndicators(currentSlide); }); prevButton.addEventListener('click', () => { const currentSlideElement = slides[currentSlide]; currentSlide = (currentSlide - 1 + slides.length) % slides.length; const prevSlideElement = slides[currentSlide]; moveToSlide(track, currentSlideElement, prevSlideElement); updateIndicators(currentSlide); }); indicators.forEach(indicator => { indicator.addEventListener('click', () => { const targetIndex = parseInt(indicator.getAttribute('data-slide')); if (targetIndex !== currentSlide) { const currentSlideElement = slides[currentSlide]; const targetSlideElement = slides[targetIndex]; currentSlide = targetIndex; moveToSlide(track, currentSlideElement, targetSlideElement); updateIndicators(currentSlide); } }); }); </script> </body> </html>