-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.html
132 lines (111 loc) · 3.39 KB
/
carousel.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<html>
<head>
<style>
.carousel-arrow {
position: absolute;
display: flex;
justify-content: center;
top: 0;
bottom: 64px;
margin-block: auto;
height: fit-content;
width: 48px;
background-color: white;
border: none;
font-size: 3rem;
padding: 0;
cursor: pointer;
opacity: 0.5;
transition: opacity 100ms;
}
.carousel-arrow:hover,
.carousel-arrow:focus {
opacity: 1;
}
.carousel-arrow--prev {
left: 0;
}
.carousel-arrow--next {
right: 0;
}
.carousel-container {
width: 100%;
padding-block: 16px 32px;
margin: 16px 48px;
overflow-x: hidden;
display: flex;
width: 100%;
gap: 8px;
align-items: center;
scroll-snap-type: x mandatory;
flex-flow: row nowrap;
scroll-behavior: smooth;
}
.carousel-container::-webkit-scrollbar {
height: 14px;
width: calc(100% - 48px);
}
.carousel-container::-webkit-scrollbar-track {
background: #b1b3b399;
}
.carousel-container::-webkit-scrollbar-thumb {
background: #29AB87;
}
.carousel-container::-webkit-scrollbar-track-piece:start {
background: #29AB87;
}
.carousel-slide {
flex: 1 0 30%;
aspect-ratio: 1;
flex-flow: column nowrap;
display: flex;
justify-content: center;
align-items: center;
background-color: #bae;
scroll-snap-align: center;
}
div.carousel-parent {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding-bottom: 64px;
position: relative;
}
@media (max-width: 600px) {
.carousel-slide {
flex: 1 0 90%;
}
}
</style>
</head>
<body>
<div class="carousel-parent">
<button class="carousel-arrow carousel-arrow--prev" onclick="handleCarouselMove(false)">
‹
</button>
<button class="carousel-arrow carousel-arrow--next" onclick="handleCarouselMove(true)">
›
</button>
<div class="carousel-container" dir="ltr">
<div class="carousel-slide">1</div>
<div class="carousel-slide">2</div>
<div class="carousel-slide">3</div>
<div class="carousel-slide">4</div>
<div class="carousel-slide">5</div>
<div class="carousel-slide">6</div>
<div class="carousel-slide">7</div>
<div class="carousel-slide">8</div>
</div>
</div>
<script>
const carousel = document.querySelector(".carousel-container");
const slide = document.querySelector(".carousel-slide");
function handleCarouselMove(positive = true) {
const slideWidth = slide.clientWidth;
console.log(slideWidth);
carousel.scrollLeft = positive ? carousel.scrollLeft + slideWidth : carousel.scrollLeft - slideWidth;
}
</script>
</body>
</html>