<div class="auto-carousel" id="carouselContainer">
<div class="carousel-track" id="track">
<div class="c-slide">
<img src="https://placehold.co/800x450/3b82f6/ffffff?text=Modern+Design" alt="Slide 1" loading="lazy">
<div class="c-caption">
<h3>Modern Design</h3>
<p>Clean and responsive layouts.</p>
</div>
</div>
<div class="c-slide">
<img src="https://placehold.co/800x450/ec4899/ffffff?text=Smart+Automation" alt="Slide 2" loading="lazy">
<div class="c-caption">
<h3>Smart Automation</h3>
<p>Slides advance automatically.</p>
</div>
</div>
<div class="c-slide">
<img src="https://placehold.co/800x450/10b981/ffffff?text=Fully+Responsive" alt="Slide 3" loading="lazy">
<div class="c-caption">
<h3>Fully Responsive</h3>
<p>Looks great on any device.</p>
</div>
</div>
</div>
<div class="c-nav">
<button class="nav-dot active" data-index="0" aria-label="Slide 1"></button>
<button class="nav-dot" data-index="1" aria-label="Slide 2"></button>
<button class="nav-dot" data-index="2" aria-label="Slide 3"></button>
</div>
</div>
.auto-carousel {
font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
position: relative;
width: 100%;
max-width: 800px;
margin: 0 auto;
border-radius: 20px;
overflow: hidden;
box-shadow: 0 10px 30px rgba(0,0,0,0.15);
background: #000;
}
.carousel-track {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch;
scrollbar-width: none;
}
.carousel-track::-webkit-scrollbar {
display: none;
}
.c-slide {
flex: 0 0 100%;
scroll-snap-align: start;
position: relative;
}
.c-slide img {
width: 100%;
display: block;
object-fit: cover;
height: 400px;
opacity: 0.9;
transition: opacity 0.3s;
}
.c-slide:hover img {
opacity: 1;
}
.c-caption {
position: absolute;
bottom: 40px;
left: 40px;
color: white;
z-index: 10;
text-shadow: 0 2px 10px rgba(0,0,0,0.5);
}
.c-caption h3 {
margin: 0 0 5px 0;
font-size: 1.8rem;
font-weight: 800;
}
.c-caption p {
margin: 0;
font-size: 1rem;
opacity: 0.9;
}
/* Gradient Overlay */
.c-slide::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 50%;
background: linear-gradient(to top, rgba(0,0,0,0.8), transparent);
pointer-events: none;
}
.c-nav {
position: absolute;
bottom: 20px;
right: 20px;
display: flex;
gap: 8px;
z-index: 20;
}
.nav-dot {
width: 12px;
height: 12px;
border-radius: 50%;
border: 2px solid rgba(255,255,255,0.5);
background: transparent;
cursor: pointer;
padding: 0;
transition: all 0.3s;
}
.nav-dot:hover {
background: rgba(255,255,255,0.5);
}
.nav-dot.active {
background: #fff;
border-color: #fff;
transform: scale(1.2);
}
const track = document.getElementById("track");
const dots = document.querySelectorAll(".nav-dot");
let autoPlayInterval;
// Handle scroll detection to update dots
track.addEventListener("scroll", () => {
const slideWidth = track.offsetWidth;
const index = Math.round(track.scrollLeft / slideWidth);
dots.forEach(dot => dot.classList.remove("active"));
if (dots[index]) dots[index].classList.add("active");
});
// Manual Navigation
dots.forEach(dot => {
dot.addEventListener("click", () => {
const index = parseInt(dot.getAttribute("data-index"));
scrollToSlide(index);
resetTimer();
});
});
function scrollToSlide(index) {
const slideWidth = track.offsetWidth;
track.scrollTo({
left: index * slideWidth,
behavior: "smooth"
});
}
function nextSlide() {
const slideWidth = track.offsetWidth;
const maxScroll = track.scrollWidth - slideWidth;
if (track.scrollLeft >= maxScroll - 10) {
// Loop back to start
track.scrollTo({ left: 0, behavior: "smooth" });
} else {
// Go to next
track.scrollBy({ left: slideWidth, behavior: "smooth" });
}
}
function resetTimer() {
clearInterval(autoPlayInterval);
autoPlayInterval = setInterval(nextSlide, 4000);
}
// Init Auto Play
resetTimer();
// Pause on hover
const container = document.getElementById("carouselContainer");
container.addEventListener("mouseenter", () => clearInterval(autoPlayInterval));
container.addEventListener("mouseleave", resetTimer);