-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
96 lines (87 loc) · 2.3 KB
/
test.html
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
<!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>
.carousel {
overflow: hidden;
position: relative;
width: 600px;
height: 400px;
margin: auto;
}
.carousel-container {
display: flex;
width: calc(100% * 4); /* 4 images for seamless loop */
position: relative;
transition: transform 0.5s ease-in-out;
}
.carousel img {
width: calc(100% / 4); /* 4 images */
height: 100%;
flex-shrink: 0;
}
#prevBtn, #nextBtn {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0, 0, 0, 0.5);
color: white;
padding: 10px;
cursor: pointer;
border: none;
outline: none;
}
#prevBtn {
left: 10px;
}
#nextBtn {
right: 10px;
}
#prevBtn:hover, #nextBtn:hover {
background-color: rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div class="carousel">
<div class="carousel-container">
<img src="吾王.jpg" alt="Image 1">
<img src="素晴日2.jpg" alt="Image 2">
<img src="美咲.jpg" alt="Image 3">
<img src="吾王.jpg" alt="Image 1"> <!-- Duplicate of the first image for seamless loop -->
</div>
<button id="prevBtn">Prev</button>
<button id="nextBtn">Next</button>
</div>
<script>
let currentSlide = 0;
const slides = document.querySelectorAll('.carousel img');
const totalSlides = slides.length; // Include the duplicate image for seamless loop
const carouselContainer = document.querySelector('.carousel-container');
function showSlide(index) {
if (index >= totalSlides) {
index = 0; // Reset to the first slide
} else if (index < 0) {
index = totalSlides - 1; // Go to the last slide
}
carouselContainer.style.transform = `translateX(-${index * 100 / totalSlides}%)`;
currentSlide = index;
}
function nextSlide() {
showSlide(currentSlide + 1);
}
function prevSlide() {
showSlide(currentSlide - 1);
}
document.getElementById('nextBtn').addEventListener('click', nextSlide);
document.getElementById('prevBtn').addEventListener('click', prevSlide);
// Automatically change slide every 6 seconds
setInterval(nextSlide, 6000);
// Initially show the first slide
showSlide(0);
</script>
</body>
</html>