-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-slider.html
226 lines (189 loc) · 5.56 KB
/
test-slider.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<html>
<head>
<title>Test Slider</title>
<style>
body {
color: #333;
text-align: center;
background: #222;
margin: 0;
}
ul {
padding: 0;
}
li {
list-style: none;
}
main {
max-width: 1200px;
margin: 0 auto;
padding: 50px 0;
}
main h1 {
margin-bottom: 50px;
}
.wrapper {
position: relative;
}
.wrapper:before,
.wrapper:after {
position: absolute;
top: 0;
z-index: 1;
content: "";
display: block;
width: 20px;
height: 100%;
}
.wrapper:before {
left: 0;
background: linear-gradient(90deg, #222, transparent);
}
.wrapper:after {
right: 0;
background: linear-gradient(-90deg, #222, transparent);
}
.items {
position: relative;
width: 100%;
overflow: hidden;
white-space: nowrap;
font-size: 0;
cursor: pointer;
}
.items.active {
cursor: grab;
}
.item {
display: inline-block;
margin-left: 20px;
user-select: none;
background: tomato;
width: 50%;
height: 130px;
color: #222;
font-size: 33px;
font-weight: bold;
line-height: 130px;
}
.item:last-child {
margin-right: 20px;
}
@media screen and (min-width: 500px) {
.item {
width: 33%;
}
}
@media screen and (min-width: 800px) {
.item {
width: 25%;
}
}
@media screen and (min-width: 1200px) {
.wrapper {
margin-left: -20px;
}
.item {
width: 20%;
}
}
</style>
</head>
<body>
<main>
<h1>Slide</h1>
<div class="wrapper">
<ul class="items">
<li class="item">0</li>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
<li class="item">5</li>
<li class="item">6</li>
<li class="item">7</li>
<li class="item">8</li>
<li class="item">9</li>
</ul>
</div>
<div>
<button class="btn-pre">Pre</button>
<button class="btn-next">Next</button>
</div>
</main>
<script>
let isDown = false;
let startX;
let scrollLeft;
const slider = document.querySelector('.items');
const end = () => {
isDown = false;
slider.classList.remove('active');
// Update the active item based on the current scroll position
active = Math.round(slider.scrollLeft / itemWidth);
}
const start = (e) => {
isDown = true;
slider.classList.add('active');
startX = e.pageX || e.touches[0].pageX - slider.offsetLeft;
scrollLeft = slider.scrollLeft;
}
const move = (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX || e.touches[0].pageX - slider.offsetLeft;
const dist = (x - startX);
slider.scrollLeft = scrollLeft - dist;
}
(() => {
slider.addEventListener('mousedown', start);
slider.addEventListener('touchstart', start);
slider.addEventListener('mousemove', move);
slider.addEventListener('touchmove', move);
slider.addEventListener('mouseleave', end);
slider.addEventListener('mouseup', end);
slider.addEventListener('touchend', end);
})();
// Get the buttons
const nextButton = document.querySelector('.btn-next');
const preButton = document.querySelector('.btn-pre');
// Get the items
const items = document.querySelector('.items');
console.log(items.firstElementChild.offsetWidth);
// Calculate the width of a single item (including margin)
const itemWidth = items.firstElementChild.offsetWidth + parseInt(window.getComputedStyle(items.firstElementChild).marginRight) * 2;
// Variable to keep track of the active item
let active = 0;
// Function to update the active item
function updateActive(newActive) {
// Check if the new active index is out of bounds
if (newActive < 0) {
active = items.children.length - 1; // Set to the last item
} else if (newActive >= items.children.length) {
active = 0; // Set to the first item
} else {
active = newActive;
}
// Scroll the active item into view
items.children[active].scrollIntoView({
behavior: 'smooth',
inline: 'start'
});
}
// Add event listener for the next button
nextButton.addEventListener('click', () => {
// Update the active item
updateActive((active + 1) % items.children.length);
});
// Add event listener for the pre button
preButton.addEventListener('click', () => {
// Update the active item
updateActive((active - 1 + items.children.length) % items.children.length);
});
// Scroll to the active item when the page loads
window.addEventListener('load', () => {
updateActive(active);
});
</script>
</body>
</html>