-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathImplementing a Basic Carousel
More file actions
63 lines (63 loc) · 1.58 KB
/
Implementing a Basic Carousel
File metadata and controls
63 lines (63 loc) · 1.58 KB
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
<!-- Objective: Create a simple image carousel. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Basic Carousel</title>
<style>
#carousel {
width: 600px;
overflow: hidden;
margin: auto;
}
.carousel-images {
display: flex;
transition: transform 0.5s ease;
}
.carousel-image {
max-width: 600px;
display: block;
}
.control-btn {
cursor: pointer;
padding: 10px;
background-color: #ddd;
border: none;
}
</style>
</head>
<body>
<div id="carousel">
<div class="carousel-images">
<img class="carousel-image" src="image1.jpg" alt="Image 1">
<img class="carousel-image" src="image2.jpg" alt="Image 2">
<img class="carousel-image" src="image3.jpg" alt="Image 3">
<!-- More images as needed -->
</div>
</div>
<button class="control-btn" id="prev">Previous</button>
<button class="control-btn" id="next">Next</button>
<script>
const imagesContainer = document.querySelector('.carousel-images');
const images = document.querySelectorAll('.carousel-image');
let index = 0;
function updateCarousel() {
const offset = -index * 600; // Assuming each image is 600px wide
imagesContainer.style.transform = `translateX(${offset}px)`;
}
document.getElementById('next').addEventListener('click', () => {
index = (index + 1) % images.length;
updateCarousel();
});
document.getElementById('prev').addEventListener('click', () => {
index = (index - 1 + images.length) % images.length;
updateCarousel();
});
// Optional: Automatically cycle through images
setInterval(() => {
index = (index + 1) % images.length;
updateCarousel();
}, 3000);
</script>
</body>
</html>