parent
de4aed91e4
commit
55488b65ad
@ -0,0 +1,169 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Carousel Example</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
background-color: #f0f0f0;
|
||||
}
|
||||
|
||||
.carousel-container {
|
||||
position: relative;
|
||||
width: 80%;
|
||||
max-width: 600px;
|
||||
overflow: hidden;
|
||||
border: 2px solid #ddd;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.carousel-images {
|
||||
display: flex;
|
||||
transition: transform 0.5s ease-in-out;
|
||||
}
|
||||
|
||||
.carousel-images img {
|
||||
width: 100%;
|
||||
display: block;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
button {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px;
|
||||
cursor: pointer;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
#prev-btn {
|
||||
left: 10px;
|
||||
}
|
||||
|
||||
#next-btn {
|
||||
right: 10px;
|
||||
}
|
||||
|
||||
.dots-container {
|
||||
position: absolute;
|
||||
bottom: 10px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.dot {
|
||||
height: 10px;
|
||||
width: 10px;
|
||||
margin: 0 5px;
|
||||
background-color: rgba(255, 255, 255, 0.5);
|
||||
border-radius: 50%;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.dot.active {
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="carousel-container">
|
||||
<div class="carousel-images">
|
||||
<!-- JavaScript will populate these images -->
|
||||
</div>
|
||||
<button id="prev-btn"> < </button>
|
||||
<button id="next-btn"> > </button>
|
||||
<div class="dots-container">
|
||||
<!-- JavaScript will populate these dots -->
|
||||
</div>
|
||||
</div>
|
||||
<script>document.addEventListener("DOMContentLoaded", function () {
|
||||
const imgs = [
|
||||
{ src: "pic/1.png" },
|
||||
{ src: "pic/2.jpg" },
|
||||
{ src: "pic/3.jpg" },
|
||||
{ src: "pic/4.jpg" },
|
||||
{ src: "pic/5.png" },
|
||||
];
|
||||
|
||||
const carouselImages = document.querySelector('.carousel-images');
|
||||
const dotsContainer = document.querySelector('.dots-container');
|
||||
let currentIndex = 0;
|
||||
|
||||
// Populate images
|
||||
imgs.forEach(img => {
|
||||
const imgElement = document.createElement('img');
|
||||
imgElement.src = img.src;
|
||||
carouselImages.appendChild(imgElement);
|
||||
});
|
||||
|
||||
// Populate dots
|
||||
imgs.forEach((_, index) => {
|
||||
const dot = document.createElement('div');
|
||||
dot.classList.add('dot');
|
||||
dot.dataset.index = index;
|
||||
dotsContainer.appendChild(dot);
|
||||
});
|
||||
|
||||
const dots = document.querySelectorAll('.dot');
|
||||
|
||||
function autoPlayCarousel() {
|
||||
carouselInterval = setInterval(() => {
|
||||
currentIndex = (currentIndex + 1) % imgs.length;
|
||||
updateCarousel();
|
||||
}, 5000); // 2 seconds
|
||||
}
|
||||
|
||||
autoPlayCarousel();
|
||||
|
||||
// Update carousel on next button click
|
||||
document.getElementById('next-btn').addEventListener('click', () => {
|
||||
currentIndex = (currentIndex + 1) % imgs.length;
|
||||
updateCarousel();
|
||||
autoPlayCarousel();
|
||||
});
|
||||
|
||||
// Update carousel on prev button click
|
||||
document.getElementById('prev-btn').addEventListener('click', () => {
|
||||
currentIndex = (currentIndex - 1 + imgs.length) % imgs.length;
|
||||
updateCarousel();
|
||||
autoPlayCarousel();
|
||||
});
|
||||
|
||||
// Update carousel on dot click
|
||||
dots.forEach(dot => {
|
||||
dot.addEventListener('click', () => {
|
||||
currentIndex = parseInt(dot.dataset.index);
|
||||
updateCarousel();
|
||||
autoPlayCarousel();
|
||||
});
|
||||
});
|
||||
|
||||
function updateCarousel() {
|
||||
const offset = -currentIndex * 100 + '%';
|
||||
carouselImages.style.transform = `translateX(${offset})`;
|
||||
|
||||
dots.forEach(dot => dot.classList.remove('active'));
|
||||
dots[currentIndex].classList.add('active');
|
||||
}
|
||||
|
||||
// Initial highlight
|
||||
dots[currentIndex].classList.add('active');
|
||||
});</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in new issue