-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgslider.html
93 lines (89 loc) · 2.53 KB
/
imgslider.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
background-color: wheat;
height: 100vh;
}
.slide-container {
text-align: center;
width: 700px;
height: 500px;
overflow: hidden;
box-shadow: 0 4px 8px rgba(0,0,0,0.4);
border-radius: 8px;
position: relative;
}
#img-container{
display: flex;
transition: transform 0.7s ease-in-out;
}
#img-container img{
border-radius: 8px;
}
.btn{
position: absolute;
top: 50%;
width: 40px;
cursor: pointer;
}
#left{
left: 1%;
}
#rght{
right: 1%;
}
</style>
</head>
<body>
<div class="slide-container">
<div id="img-container">
<img src="https://picsum.photos/id/22/700/500" alt="">
<img src="https://picsum.photos/id/26/700/500" alt="">
<img src="https://picsum.photos/id/38/700/500" alt="">
<img src="https://picsum.photos/id/58/700/500" alt="">
<img src="https://picsum.photos/id/63/700/500" alt="">
<img src="https://picsum.photos/id/78/700/500" alt="">
</div>
<img src="https://cdn2.iconfinder.com/data/icons/symbols-signs-outline/19/40-512.png" alt="" width="25px" id="left" class="btn">
<img src="https://cdn2.iconfinder.com/data/icons/250-perfect-vector-pictograms/48/9.5-512.png" alt="" width="23px" id="rght" class="btn">
</div>
<script>
const left = document.getElementById("left");
const right = document.getElementById("rght");
const imgCon = document.getElementById("img-container");
const imgs = imgCon.querySelectorAll('img');
console.log(imgs);
let currentImg = 1;
const imgWidth = 700;
right.addEventListener('click', () => {
currentImg++;
if(currentImg > imgs.length){
currentImg = 1;
}
imgCon.style.transform = `translateX(-${(currentImg - 1) * imgWidth}px)`;
});
left.addEventListener('click', () => {
currentImg--;
if(currentImg < 1){
currentImg = imgs.length;
}else if(currentImg > imgs.length ){
currentImg = 1;
}
imgCon.style.transform = `translateX(-${(currentImg - 1) * imgWidth}px)`
});
</script>
</body>
</html>