File size: 5,169 Bytes
4c1ad4e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | <!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;
// Create indicators
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}%)`;
// Update active indicator
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();
};
// Event listeners
nextBtn.addEventListener('click', nextSlide);
prevBtn.addEventListener('click', prevSlide);
// Auto-rotate every 5 seconds
setInterval(nextSlide, 5000);
});
</script>
</body>
</html> |