Skip to content

Commit 8d023d4

Browse files
committed
image slider
1 parent c5f2b1b commit 8d023d4

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

image-slider/index.html

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Image Slider</title>
6+
<style>
7+
body {
8+
display: flex;
9+
flex-direction: column;
10+
align-items: center;
11+
justify-content: center;
12+
height: 100vh;
13+
background-color: #f3f4f6;
14+
font-family: system-ui, Arial;
15+
}
16+
.slider {
17+
position: relative;
18+
width: 400px;
19+
height: 250px;
20+
overflow: hidden;
21+
border-radius: 10px;
22+
box-shadow: 0 5px 20px rgba(0,0,0,0.2);
23+
}
24+
.slider img {
25+
width: 100%;
26+
height: 100%;
27+
object-fit: cover;
28+
border-radius: 10px;
29+
display: none;
30+
}
31+
.slider img.active {
32+
display: block;
33+
animation: fade 0.5s ease-in;
34+
}
35+
@keyframes fade {
36+
from { opacity: 0; }
37+
to { opacity: 1; }
38+
}
39+
.controls {
40+
margin-top: 20px;
41+
}
42+
button {
43+
background: #2563eb;
44+
color: white;
45+
border: none;
46+
padding: 8px 12px;
47+
margin: 0 10px;
48+
border-radius: 6px;
49+
cursor: pointer;
50+
font-size: 15px;
51+
}
52+
button:hover {
53+
background: #1d4ed8;
54+
}
55+
</style>
56+
</head>
57+
<body>
58+
<h2>🖼️ Image Slider</h2>
59+
<div class="slider">
60+
<img src="https://picsum.photos/400/250?random=1" class="active" alt="Image 1">
61+
<img src="https://picsum.photos/400/250?random=2" alt="Image 2">
62+
<img src="https://picsum.photos/400/250?random=3" alt="Image 3">
63+
<img src="https://picsum.photos/400/250?random=4" alt="Image 4">
64+
</div>
65+
66+
<div class="controls">
67+
<button id="prevBtn">⬅️ Prev</button>
68+
<button id="nextBtn">Next ➡️</button>
69+
</div>
70+
71+
<script>
72+
const images = document.querySelectorAll(".slider img");
73+
const nextBtn = document.getElementById("nextBtn");
74+
const prevBtn = document.getElementById("prevBtn");
75+
let current = 0;
76+
77+
function showImage(index) {
78+
images.forEach((img, i) => {
79+
img.classList.toggle("active", i === index);
80+
});
81+
}
82+
83+
nextBtn.addEventListener("click", () => {
84+
current = (current + 1) % images.length;
85+
showImage(current);
86+
});
87+
88+
prevBtn.addEventListener("click", () => {
89+
current = (current - 1 + images.length) % images.length;
90+
showImage(current);
91+
});
92+
93+
setInterval(() => {
94+
current = (current + 1) % images.length;
95+
showImage(current);
96+
}, 3000);
97+
98+
window.addEventListener("keydown", (e) => {
99+
if (e.key === "ArrowRight") nextBtn.click();
100+
if (e.key === "ArrowLeft") prevBtn.click();
101+
});
102+
103+
104+
</script>
105+
</body>
106+
</html>

0 commit comments

Comments
 (0)