-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleCSS_JS_scroll_snap_no_calculation.html
83 lines (65 loc) · 2.58 KB
/
SimpleCSS_JS_scroll_snap_no_calculation.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
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scroll Snap Slider Simple CSS JS</title>
<style>
.slider {
height: 300px;
width: 500px;
display: block;
white-space: nowrap;
overflow: scroll hidden;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
}
.slider > * {
scroll-snap-align: start;
white-space: normal;
}
.slider > *, .slider::before, .slider::after {
height: 100%;
display: inline-block;
vertical-align: middle;
}
.slider::before, .slider::after {
content: "";
width: 100%;
}
.slider > * + * {
margin-left: 20px; /* Space Between Slides */
}
</style>
<script>
document.addEventListener("DOMContentLoaded", () => {
const setscroll = (e, int) => {
const child = e.querySelector(':scope > :nth-child('+int+')');
if(child !== null) {
child.scrollIntoView({behavior: 'auto', block: 'start', inline: 'start'});
e.dataset.activeslide = int;
}
};
document.querySelectorAll('.slider').forEach(e => {
setscroll(e, 1);
e.nextElementSibling.querySelector(':scope .arrowback').addEventListener('click', () => setscroll(e, parseInt(e.dataset.activeslide) - 1) );
e.nextElementSibling.querySelector(':scope .arrownext').addEventListener('click', () => setscroll(e, parseInt(e.dataset.activeslide) + 1) );
});
});
</script>
</head>
<body>
<div class="slider">
<div style="width: 20%; background-color: red">1</div>
<div style="width: 80%; background-color: green">2</div>
<div style="width: 50%; background-color: yellow">3</div>
<div style="width: 90%; background-color: blue">4</div>
<div style="width: 10%; background-color: purple">5</div>
<div style="width: 30%; background-color: gray">6</div>
</div>
<div class="arrows">
<div class="arrowback">BACK</div>
<div class="arrownext">NEXT</div>
</div>
</body>
</html>