tbs-snap-slider 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -2
- package/dist/main.css +188 -0
- package/dist/main.css.map +1 -0
- package/index.html +75 -0
- package/main.js +12 -0
- package/package.json +19 -4
- package/src/js/index.js +122 -0
- package/src/style/main.sass +185 -0
package/README.md
CHANGED
|
@@ -1,3 +1,104 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Snap Slider
|
|
2
|
+
|
|
3
|
+
Snap Slider is a CSS-based horizontal slider with JavaScript navigation. Its items use CSS scroll snapping; optional modifiers add a draggable scrollbar and active-item tracking.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Node.js and npm
|
|
8
|
+
- A modern browser with CSS scroll snapping, container queries, and (for active-item tracking) `IntersectionObserver`
|
|
9
|
+
|
|
10
|
+
## Install and build
|
|
11
|
+
|
|
12
|
+
Install the project's dependencies and compile the Sass stylesheet:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install
|
|
16
|
+
npm run build
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The build writes the compressed stylesheet to `dist/main.css`. To rebuild it automatically while editing `src/style/main.sass`, run:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm run watch
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
To view the included demo, start a local static server from the project root (for example, `npx vite`) and open the URL it prints. The demo loads `dist/main.css` and `main.js` from `index.html`. The demo's skip-link text contains an unrendered template expression; replace it with meaningful text when using the markup.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- Horizontal scrolling with mandatory CSS scroll snapping
|
|
30
|
+
- Previous/next controls that scroll by a configurable number of items
|
|
31
|
+
- Responsive item widths: one item on small screens, two on tablet-sized screens, and three on larger screens by default
|
|
32
|
+
- Optional custom draggable scrollbar and active-item tracking
|
|
33
|
+
- Keyboard-accessible skip link, provided by the markup
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
Each slider needs a `.snap-slider` root, a `.snap-slider__content` list, and previous and next buttons. Add `data-js-module="snap-slider"` so `main.js` initializes it. The optional `data-js-options` attribute must contain valid JSON; `amount` controls how many item widths each button advances.
|
|
38
|
+
|
|
39
|
+
```html
|
|
40
|
+
<div class="snap-slider" data-js-module="snap-slider" data-js-options='{"amount": 1}'>
|
|
41
|
+
<a class="snap-slider__skiplink" href="#featured-slider-controls">
|
|
42
|
+
Skip the featured items
|
|
43
|
+
</a>
|
|
44
|
+
|
|
45
|
+
<ul class="snap-slider__content">
|
|
46
|
+
<li class="snap-slider__item">First item</li>
|
|
47
|
+
<li class="snap-slider__item">Second item</li>
|
|
48
|
+
<li class="snap-slider__item">Third item</li>
|
|
49
|
+
</ul>
|
|
50
|
+
|
|
51
|
+
<div class="snap-slider__controller">
|
|
52
|
+
<button
|
|
53
|
+
id="featured-slider-controls"
|
|
54
|
+
class="snap-slider__controller-button snap-slider__controller-button-prev"
|
|
55
|
+
aria-label="Previous slide"
|
|
56
|
+
></button>
|
|
57
|
+
<button
|
|
58
|
+
class="snap-slider__controller-button snap-slider__controller-button-next"
|
|
59
|
+
aria-label="Next slide"
|
|
60
|
+
></button>
|
|
61
|
+
</div>
|
|
62
|
+
</div>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Use a unique skip-link target for each slider. The link can point to the previous button as above, so keyboard users can bypass the slider. Provide localized, descriptive `aria-label` values for both buttons. The slider script expects the content list and both buttons to be present.
|
|
66
|
+
|
|
67
|
+
## Options and modifiers
|
|
68
|
+
|
|
69
|
+
The initializer reads `data-js-options` as JSON. Its `amount` value is the number of item widths moved per button click and defaults to `1` when omitted or falsy. Scrolling uses the width of the first item, so items in a slider should have consistent widths.
|
|
70
|
+
|
|
71
|
+
```html
|
|
72
|
+
<div class="snap-slider" data-js-module="snap-slider" data-js-options='{"amount": 2}'>
|
|
73
|
+
<!-- slider content and controls -->
|
|
74
|
+
</div>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The Sass sets `--item-count-mobile: 1`, `--item-count-tablet: 2`, and `--item-count-desktop: 3`. The current value switches at `576px` and `992px` viewport widths. Override the custom properties on the slider to change its responsive layout:
|
|
78
|
+
|
|
79
|
+
```css
|
|
80
|
+
.product-slider {
|
|
81
|
+
--item-count-mobile: 1;
|
|
82
|
+
--item-count-tablet: 3;
|
|
83
|
+
--item-count-desktop: 4;
|
|
84
|
+
--content-gutter: 1rem;
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Additional styling properties include `--content-padding-top`, `--content-padding-bottom`, `--content-gutter`, and the `--controller-*` variables for button appearance, size, spacing, and transitions. The stylesheet also accepts `--track-color` and `--thumb-color` for the custom scrollbar.
|
|
89
|
+
|
|
90
|
+
Add these modifier classes to the `.snap-slider` element to enable optional behavior:
|
|
91
|
+
|
|
92
|
+
- `snap-slider--overflow` applies the edge-to-edge overflow styling, hides the native scrollbar, and initializes a custom draggable scrollbar. Include `.scrollbar-track` and `.scrollbar-thumb` elements inside the slider for it to work.
|
|
93
|
+
- `snap-slider--hide-scroll` hides the native scrollbar without enabling the custom scrollbar.
|
|
94
|
+
- `snap-slider--observe-active` uses `IntersectionObserver` to toggle the `active` class on items that are at least 95% visible.
|
|
95
|
+
|
|
96
|
+
## Project files
|
|
97
|
+
|
|
98
|
+
- `index.html` — demo page and example slider markup
|
|
99
|
+
- `main.js` — finds elements marked with `data-js-module="snap-slider"`, parses their JSON options, and initializes them
|
|
100
|
+
- `src/js/index.js` — slider navigation, button state, optional scrollbar, and active-item behavior
|
|
101
|
+
- `src/style/main.sass` — slider layout, snapping, responsive sizing, controls, and scrollbar styles
|
|
102
|
+
- `dist/main.css` — compiled stylesheet loaded by the demo
|
|
103
|
+
- `package.json` — npm scripts and Sass development dependency
|
|
2
104
|
|
|
3
|
-
This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
|
package/dist/main.css
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
.snap-slider {
|
|
2
|
+
--item-count-mobile: 1;
|
|
3
|
+
--item-count-tablet: 2;
|
|
4
|
+
--item-count-desktop: 3;
|
|
5
|
+
--item-count-current: var(--item-count-desktop);
|
|
6
|
+
--content-padding-top: 3.6rem;
|
|
7
|
+
--content-padding-bottom: 3.6rem;
|
|
8
|
+
--content-gutter: 2.4rem;
|
|
9
|
+
--controller-button-icon: url("data:image/svg+xml,%3Csvg width='23' height='20' viewBox='0 0 23 20' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M13.49 0.629998L22.73 9.93L13.49 19.23L11.33 16.98L16.76 11.58H0.05V8.37H16.85L11.33 2.94L13.49 0.629998Z' fill='white'/%3E%3C/svg%3E%0A");
|
|
10
|
+
--controller-button-icon-size: 50%;
|
|
11
|
+
--controller-button-size: 4rem;
|
|
12
|
+
--controller-button-background: black;
|
|
13
|
+
--controller-button-box-shadow: 0;
|
|
14
|
+
--controller-button-border: 1px solid transparent;
|
|
15
|
+
--controller-button-border-radius: 50%;
|
|
16
|
+
--controller-button-color: white;
|
|
17
|
+
--controller-margin-top: 3.6rem;
|
|
18
|
+
--controller-gutter: 2.4rem;
|
|
19
|
+
--controller-button-transition-duration: 300ms;
|
|
20
|
+
display: flex;
|
|
21
|
+
flex-flow: column;
|
|
22
|
+
container-name: snap-slider;
|
|
23
|
+
container-type: inline-size;
|
|
24
|
+
position: relative;
|
|
25
|
+
}
|
|
26
|
+
@media (max-width: 992px) {
|
|
27
|
+
.snap-slider {
|
|
28
|
+
--item-count-current: var(--item-count-tablet);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
@media (max-width: 576px) {
|
|
32
|
+
.snap-slider {
|
|
33
|
+
--item-count-current: var(--item-count-mobile);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
.snap-slider__skiplink {
|
|
37
|
+
color: var(--frame-link-color);
|
|
38
|
+
background-color: var(--frame-background);
|
|
39
|
+
bottom: 2rem;
|
|
40
|
+
opacity: 0;
|
|
41
|
+
pointer-events: none;
|
|
42
|
+
position: absolute;
|
|
43
|
+
}
|
|
44
|
+
.snap-slider__skiplink:focus-visible {
|
|
45
|
+
opacity: 1;
|
|
46
|
+
pointer-events: auto;
|
|
47
|
+
position: static;
|
|
48
|
+
}
|
|
49
|
+
.snap-slider__content {
|
|
50
|
+
--_snap-slider-overflow-width: 0;
|
|
51
|
+
display: flex;
|
|
52
|
+
flex-wrap: nowrap;
|
|
53
|
+
gap: var(--content-gutter);
|
|
54
|
+
list-style: none;
|
|
55
|
+
max-width: var(--content-max-width, none);
|
|
56
|
+
overflow-x: scroll;
|
|
57
|
+
padding: var(--content-padding-top) var(--_snap-slider-overflow-width) var(--content-padding-bottom) !important;
|
|
58
|
+
position: relative;
|
|
59
|
+
scroll-behavior: smooth;
|
|
60
|
+
scroll-snap-type: x mandatory;
|
|
61
|
+
margin: var(--content-margin-top, 0) calc(-1 * var(--_snap-slider-overflow-width)) 0;
|
|
62
|
+
scroll-padding-left: var(--_snap-slider-overflow-width);
|
|
63
|
+
scroll-padding-right: var(--_snap-slider-overflow-width);
|
|
64
|
+
width: calc(100cqw + var(--_snap-slider-overflow-width) * 2);
|
|
65
|
+
}
|
|
66
|
+
@media (max-width: 576px) {
|
|
67
|
+
.snap-slider__content {
|
|
68
|
+
--_snap-slider-overflow-width: calc((100vw - 100cqw) / 2);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
.snap-slider__content > * {
|
|
72
|
+
width: 100%;
|
|
73
|
+
}
|
|
74
|
+
.content-hub-list .content-hub--blog .snap-slider__content .content-hub__teaser {
|
|
75
|
+
max-width: unset;
|
|
76
|
+
}
|
|
77
|
+
.snap-slider.snap-slider--overflow .snap-slider__content {
|
|
78
|
+
--_snap-slider-overflow-width: calc((100vw - 100cqw) / 2);
|
|
79
|
+
scrollbar-width: none;
|
|
80
|
+
}
|
|
81
|
+
.snap-slider.snap-slider--overflow .snap-slider__content::-webkit-scrollbar {
|
|
82
|
+
display: none;
|
|
83
|
+
}
|
|
84
|
+
.snap-slider.snap-slider--overflow .scrollbar-track {
|
|
85
|
+
display: block;
|
|
86
|
+
}
|
|
87
|
+
.snap-slider.snap-slider--hide-scroll .snap-slider__content {
|
|
88
|
+
scrollbar-width: none;
|
|
89
|
+
}
|
|
90
|
+
.snap-slider.snap-slider--hide-scroll .snap-slider__content::-webkit-scrollbar {
|
|
91
|
+
display: none;
|
|
92
|
+
}
|
|
93
|
+
.snap-slider__item {
|
|
94
|
+
display: inline-block;
|
|
95
|
+
flex-shrink: 0;
|
|
96
|
+
margin-left: unset;
|
|
97
|
+
margin-top: 0;
|
|
98
|
+
padding: 0;
|
|
99
|
+
scroll-snap-align: start;
|
|
100
|
+
width: calc((100cqw - (var(--item-count-current) - 1) * var(--content-gutter)) / var(--item-count-current));
|
|
101
|
+
}
|
|
102
|
+
.snap-slider__controller {
|
|
103
|
+
display: flex;
|
|
104
|
+
gap: var(--controller-gutter);
|
|
105
|
+
justify-content: flex-end;
|
|
106
|
+
margin-top: var(--controller-margin-top);
|
|
107
|
+
padding: 0 0 10px;
|
|
108
|
+
pointer-events: none;
|
|
109
|
+
width: 100%;
|
|
110
|
+
z-index: 1090;
|
|
111
|
+
}
|
|
112
|
+
.snap-slider__controller-button {
|
|
113
|
+
align-items: center;
|
|
114
|
+
background: transparent;
|
|
115
|
+
background: var(--controller-button-background);
|
|
116
|
+
border-radius: var(--controller-button-border-radius);
|
|
117
|
+
border: none;
|
|
118
|
+
border: var(--controller-button-border);
|
|
119
|
+
box-shadow: var(--controller-button-box-shadow);
|
|
120
|
+
color: var(--controller-button-color);
|
|
121
|
+
cursor: pointer;
|
|
122
|
+
display: flex;
|
|
123
|
+
height: var(--controller-button-size);
|
|
124
|
+
justify-content: center;
|
|
125
|
+
padding: 0;
|
|
126
|
+
pointer-events: auto;
|
|
127
|
+
position: relative;
|
|
128
|
+
transition: all var(--controller-button-transition-duration) ease-in;
|
|
129
|
+
width: var(--controller-button-size);
|
|
130
|
+
}
|
|
131
|
+
.snap-slider__controller-button:disabled {
|
|
132
|
+
opacity: 0.4;
|
|
133
|
+
cursor: not-allowed;
|
|
134
|
+
}
|
|
135
|
+
.snap-slider__controller-button::before {
|
|
136
|
+
background-color: currentColor;
|
|
137
|
+
content: "";
|
|
138
|
+
display: block;
|
|
139
|
+
height: 100%;
|
|
140
|
+
inset: 0;
|
|
141
|
+
mask-image: var(--controller-button-icon);
|
|
142
|
+
mask-position: center;
|
|
143
|
+
mask-repeat: no-repeat;
|
|
144
|
+
mask-size: var(--controller-button-icon-size);
|
|
145
|
+
position: absolute;
|
|
146
|
+
width: 100%;
|
|
147
|
+
border-radius: var(--controller-button-border-radius);
|
|
148
|
+
transition: background calc(var(--controller-button-transition-duration) / 2) ease-in;
|
|
149
|
+
}
|
|
150
|
+
.snap-slider__controller-button-prev {
|
|
151
|
+
scale: -1;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.scrollbar-track {
|
|
155
|
+
display: none;
|
|
156
|
+
height: 3px;
|
|
157
|
+
margin-top: 8px;
|
|
158
|
+
background: var(--track-color, white);
|
|
159
|
+
border-radius: 3px;
|
|
160
|
+
position: relative;
|
|
161
|
+
width: 100cqw;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.scrollbar-thumb {
|
|
165
|
+
height: 100%;
|
|
166
|
+
width: 30%;
|
|
167
|
+
background: var(--thumb-color, white);
|
|
168
|
+
border-radius: inherit;
|
|
169
|
+
position: absolute;
|
|
170
|
+
left: 0;
|
|
171
|
+
top: 0;
|
|
172
|
+
cursor: grab;
|
|
173
|
+
transition: box-shadow 0.2s ease-in-out;
|
|
174
|
+
}
|
|
175
|
+
.scrollbar-thumb:hover {
|
|
176
|
+
box-shadow: 0 0 0 3px var(--thumb-color, black);
|
|
177
|
+
}
|
|
178
|
+
.scrollbar-thumb::before {
|
|
179
|
+
content: "";
|
|
180
|
+
position: absolute;
|
|
181
|
+
top: 50%;
|
|
182
|
+
left: 0;
|
|
183
|
+
width: 100%;
|
|
184
|
+
height: 3rem;
|
|
185
|
+
transform: translateY(-50%);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/*# sourceMappingURL=main.css.map */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sourceRoot":"","sources":["../src/style/main.sass"],"names":[],"mappings":"AAAA;EACE;EACA;EACA;EACA;EAEA;EACA;EACA;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAGA;EACA;EACA;EACA;EACA;;AAEA;EA7BF;IA8BI;;;AAEF;EAhCF;IAiCI;;;AAEF;EACE;EACA;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;;AAEJ;EACE;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;AAEA;EAlBF;IAmBI;;;AAEF;EACE;;AAEF;EACE;;AAGF;EACE;EAEA;;AAEA;EACE;;AAEJ;EACE;;AAGF;EACE;;AAEA;EACE;;AAEN;EACE;EACA;EACA;EACA;EACA;EACA;EACA;;AAEF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;AAEA;EACE;EACA;;AAEF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;AAEF;EACE;;;AAER;EACE;EACA;EACA;EACA;EACA;EACA;EACA;;;AAEF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;AAEA;EACE;;AAEF;EACE;EACA;EACA;EACA;EACA;EACA;EACA","file":"main.css"}
|
package/index.html
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<title>Snap Slider</title>
|
|
6
|
+
<link rel="stylesheet" href="dist/main.css">
|
|
7
|
+
<script type="module" defer src="main.js"></script>
|
|
8
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
9
|
+
<style>
|
|
10
|
+
.dummy-content {
|
|
11
|
+
width: 100%;
|
|
12
|
+
height: 300px;
|
|
13
|
+
background-color: red;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.container
|
|
17
|
+
{
|
|
18
|
+
max-width: 1200px;
|
|
19
|
+
margin: 0 auto;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.row
|
|
23
|
+
{
|
|
24
|
+
display: flex;
|
|
25
|
+
flex-wrap: wrap;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.col
|
|
29
|
+
{
|
|
30
|
+
flex: 1 1 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
.col-12
|
|
35
|
+
{
|
|
36
|
+
flex: 1 1 100%;
|
|
37
|
+
}
|
|
38
|
+
</style>
|
|
39
|
+
</head>
|
|
40
|
+
<body>
|
|
41
|
+
<div class="container">
|
|
42
|
+
<div class="row">
|
|
43
|
+
<div class="col col-12">
|
|
44
|
+
<div class="snap-slider" data-js-module="snap-slider" data-js-options='{"amount": 1}'>
|
|
45
|
+
<a class="snap-slider__skiplink" href="#snap-slider--jump-target">Skip following slider with {newsItem.allRelatedSorted -> f:count()} slides</a>
|
|
46
|
+
<ul class="snap-slider__content">
|
|
47
|
+
<li class="snap-slider__item">
|
|
48
|
+
<div class="dummy-content"></div>
|
|
49
|
+
</li>
|
|
50
|
+
<li class="snap-slider__item">
|
|
51
|
+
<div class="dummy-content"></div>
|
|
52
|
+
</li>
|
|
53
|
+
<li class="snap-slider__item">
|
|
54
|
+
<div class="dummy-content"></div>
|
|
55
|
+
</li>
|
|
56
|
+
<li class="snap-slider__item">
|
|
57
|
+
<div class="dummy-content"></div>
|
|
58
|
+
</li>
|
|
59
|
+
<li class="snap-slider__item">
|
|
60
|
+
<div class="dummy-content"></div>
|
|
61
|
+
</li>
|
|
62
|
+
</ul>
|
|
63
|
+
<div class="scrollbar-track">
|
|
64
|
+
<div class="scrollbar-thumb"></div>
|
|
65
|
+
</div>
|
|
66
|
+
<div class="snap-slider__controller">
|
|
67
|
+
<button id="snap-slider--jump-target" class="snap-slider__controller-button snap-slider__controller-button-prev" aria-label="Vorheriger Slide"></button>
|
|
68
|
+
<button class="snap-slider__controller-button snap-slider__controller-button-next" aria-label="Nächster Slider"></button>
|
|
69
|
+
</div>
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
</body>
|
|
75
|
+
</html>
|
package/main.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import SnapSlider from "./src/js/index.js";
|
|
2
|
+
|
|
3
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
4
|
+
const snapSliders = document.querySelectorAll(
|
|
5
|
+
'[data-js-module="snap-slider"]'
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
snapSliders.forEach((snapSlider) => {
|
|
9
|
+
const opt = snapSlider.dataset.jsOptions ? JSON.parse(snapSlider.dataset.jsOptions) : {};
|
|
10
|
+
new SnapSlider(snapSlider, opt);
|
|
11
|
+
});
|
|
12
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tbs-snap-slider",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A CSS based Slider with extra JS functionality",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git@gitlab.brettinghams-dev.de:tbs/snap-slider.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"author": "Lucina Johnson",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "src/js/index.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "sass src/style/main.sass dist/main.css --style=compressed",
|
|
15
|
+
"watch": "sass --watch src/style/main.sass:dist/main.css"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"sass": "^1.108.2",
|
|
19
|
+
"vite": "^8.3.8"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/js/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
class SnapSlider {
|
|
2
|
+
constructor(el, options = {}) {
|
|
3
|
+
this.el = el
|
|
4
|
+
this.options = options
|
|
5
|
+
this.content = this.el.querySelector('.snap-slider__content')
|
|
6
|
+
this.prevButton = this.el.querySelector('.snap-slider__controller-button-prev')
|
|
7
|
+
this.nextButton = this.el.querySelector('.snap-slider__controller-button-next')
|
|
8
|
+
this.amount = this.options.amount || 1;
|
|
9
|
+
this.scrollThreshold = 100
|
|
10
|
+
|
|
11
|
+
console.log('initialized snap-slider', this.el, this.options);
|
|
12
|
+
this.el.classList.add('snap-slider--initialized');
|
|
13
|
+
|
|
14
|
+
this.prevButton.addEventListener('click', () => this.scroll(-1, this.amount));
|
|
15
|
+
this.nextButton.addEventListener('click', () => this.scroll(1, this.amount));
|
|
16
|
+
|
|
17
|
+
this.content.addEventListener('scroll', () => this.onScroll());
|
|
18
|
+
|
|
19
|
+
if(this.el.classList.contains('snap-slider--overflow')) {
|
|
20
|
+
this.initCustomScrollbar();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if(this.el.classList.contains('snap-slider--observe-active')) {
|
|
24
|
+
this.observeActiveItem();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
scroll(dir = 1, amount = 1) {
|
|
29
|
+
const item = this.content.children[0].offsetWidth;
|
|
30
|
+
this.content.scrollLeft += dir * (amount * item);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
onScroll() {
|
|
34
|
+
const { scrollLeft, scrollWidth, clientWidth } = this.content;
|
|
35
|
+
const maxScrollLeft = scrollWidth - clientWidth;
|
|
36
|
+
|
|
37
|
+
this.prevButton.disabled = scrollLeft <= this.scrollThreshold;
|
|
38
|
+
this.nextButton.disabled = scrollLeft >= maxScrollLeft - this.scrollThreshold;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
observeActiveItem() {
|
|
42
|
+
const observer = new IntersectionObserver(
|
|
43
|
+
(entries) => {
|
|
44
|
+
entries.forEach((entry) => {
|
|
45
|
+
entry.target.classList.remove("active");
|
|
46
|
+
if (entry.isIntersecting && entry.intersectionRatio >= 0.95) {
|
|
47
|
+
entry.target.classList.add("active");
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
threshold: 1.0
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
for(const child of this.content.children) {
|
|
57
|
+
observer.observe(child);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
initCustomScrollbar() {
|
|
62
|
+
const container = this.content;
|
|
63
|
+
const track = this.el.querySelector(".scrollbar-track");
|
|
64
|
+
const thumb = this.el.querySelector(".scrollbar-thumb");
|
|
65
|
+
|
|
66
|
+
let isDragging = false;
|
|
67
|
+
let startX = 0;
|
|
68
|
+
let startLeft = 0;
|
|
69
|
+
|
|
70
|
+
function updateScrollbar() {
|
|
71
|
+
const maxScroll = container.scrollWidth - container.clientWidth;
|
|
72
|
+
const trackWidth = track.clientWidth;
|
|
73
|
+
|
|
74
|
+
if (maxScroll <= 0) {
|
|
75
|
+
track.style.display = "none";
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
track.style.display = "";
|
|
80
|
+
|
|
81
|
+
const maxThumbTravel = trackWidth - thumb.offsetWidth;
|
|
82
|
+
thumb.style.left = `${(container.scrollLeft / maxScroll) * maxThumbTravel}px`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
container.addEventListener("scroll", updateScrollbar);
|
|
86
|
+
|
|
87
|
+
thumb.style.width = `${(container.clientWidth / container.scrollWidth) * 100}%`;
|
|
88
|
+
thumb.addEventListener("pointerdown", (e) => {
|
|
89
|
+
isDragging = true;
|
|
90
|
+
startX = e.clientX;
|
|
91
|
+
startLeft = thumb.offsetLeft;
|
|
92
|
+
|
|
93
|
+
thumb.setPointerCapture(e.pointerId);
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
thumb.addEventListener("pointermove", (e) => {
|
|
98
|
+
if (!isDragging) return;
|
|
99
|
+
const maxThumbTravel = track.clientWidth - thumb.offsetWidth;
|
|
100
|
+
const maxScroll = container.scrollWidth - container.clientWidth;
|
|
101
|
+
const newLeft = Math.max(
|
|
102
|
+
0,
|
|
103
|
+
Math.min(maxThumbTravel, startLeft + e.clientX - startX)
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
container.scrollLeft =
|
|
107
|
+
(newLeft / maxThumbTravel) * maxScroll;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
thumb.addEventListener("pointerup", () => {
|
|
111
|
+
isDragging = false;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
thumb.addEventListener("pointercancel", () => {
|
|
115
|
+
isDragging = false;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
updateScrollbar();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export default SnapSlider;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
.snap-slider
|
|
2
|
+
--item-count-mobile: 1
|
|
3
|
+
--item-count-tablet: 2
|
|
4
|
+
--item-count-desktop: 3
|
|
5
|
+
--item-count-current: var(--item-count-desktop)
|
|
6
|
+
|
|
7
|
+
--content-padding-top: 3.6rem
|
|
8
|
+
--content-padding-bottom: 3.6rem
|
|
9
|
+
--content-gutter: 2.4rem
|
|
10
|
+
|
|
11
|
+
--controller-button-icon: url("data:image/svg+xml,%3Csvg width='23' height='20' viewBox='0 0 23 20' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M13.49 0.629998L22.73 9.93L13.49 19.23L11.33 16.98L16.76 11.58H0.05V8.37H16.85L11.33 2.94L13.49 0.629998Z' fill='white'/%3E%3C/svg%3E%0A")
|
|
12
|
+
--controller-button-icon-size: 50%
|
|
13
|
+
--controller-button-size: 4rem
|
|
14
|
+
--controller-button-background: black
|
|
15
|
+
--controller-button-box-shadow: 0
|
|
16
|
+
--controller-button-border: 1px solid transparent
|
|
17
|
+
--controller-button-border-radius: 50%
|
|
18
|
+
--controller-button-color: white
|
|
19
|
+
--controller-margin-top: 3.6rem
|
|
20
|
+
--controller-gutter: 2.4rem
|
|
21
|
+
--controller-button-transition-duration: 300ms
|
|
22
|
+
|
|
23
|
+
$this: &
|
|
24
|
+
display: flex
|
|
25
|
+
flex-flow: column
|
|
26
|
+
container-name: snap-slider
|
|
27
|
+
container-type: inline-size
|
|
28
|
+
position: relative
|
|
29
|
+
|
|
30
|
+
@media (max-width: 992px)
|
|
31
|
+
--item-count-current: var(--item-count-tablet)
|
|
32
|
+
|
|
33
|
+
@media (max-width: 576px)
|
|
34
|
+
--item-count-current: var(--item-count-mobile)
|
|
35
|
+
|
|
36
|
+
&__skiplink
|
|
37
|
+
color: var(--frame-link-color)
|
|
38
|
+
background-color: var(--frame-background)
|
|
39
|
+
bottom: 2rem
|
|
40
|
+
opacity: 0
|
|
41
|
+
pointer-events: none
|
|
42
|
+
position: absolute
|
|
43
|
+
|
|
44
|
+
&:focus-visible
|
|
45
|
+
opacity: 1
|
|
46
|
+
pointer-events: auto
|
|
47
|
+
position: static
|
|
48
|
+
|
|
49
|
+
&__content
|
|
50
|
+
--_snap-slider-overflow-width: 0
|
|
51
|
+
|
|
52
|
+
display: flex
|
|
53
|
+
flex-wrap: nowrap
|
|
54
|
+
gap: var(--content-gutter)
|
|
55
|
+
list-style: none
|
|
56
|
+
max-width: var(--content-max-width, none)
|
|
57
|
+
overflow-x: scroll
|
|
58
|
+
padding: var(--content-padding-top) var(--_snap-slider-overflow-width) var(--content-padding-bottom) !important
|
|
59
|
+
position: relative
|
|
60
|
+
scroll-behavior: smooth
|
|
61
|
+
scroll-snap-type: x mandatory
|
|
62
|
+
margin: var(--content-margin-top, 0) calc(-1 * var(--_snap-slider-overflow-width)) 0
|
|
63
|
+
scroll-padding-left: var(--_snap-slider-overflow-width)
|
|
64
|
+
scroll-padding-right: var(--_snap-slider-overflow-width)
|
|
65
|
+
width: calc(100cqw + var(--_snap-slider-overflow-width) * 2)
|
|
66
|
+
|
|
67
|
+
@media (max-width: 576px)
|
|
68
|
+
--_snap-slider-overflow-width: calc((100vw - 100cqw) / 2)
|
|
69
|
+
|
|
70
|
+
> *
|
|
71
|
+
width: 100%
|
|
72
|
+
|
|
73
|
+
.content-hub-list .content-hub--blog & .content-hub__teaser
|
|
74
|
+
max-width: unset
|
|
75
|
+
|
|
76
|
+
&.snap-slider--overflow
|
|
77
|
+
.snap-slider__content
|
|
78
|
+
--_snap-slider-overflow-width: calc((100vw - 100cqw) / 2)
|
|
79
|
+
|
|
80
|
+
scrollbar-width: none
|
|
81
|
+
|
|
82
|
+
&::-webkit-scrollbar
|
|
83
|
+
display: none
|
|
84
|
+
|
|
85
|
+
.scrollbar-track
|
|
86
|
+
display: block
|
|
87
|
+
|
|
88
|
+
&.snap-slider--hide-scroll
|
|
89
|
+
.snap-slider__content
|
|
90
|
+
scrollbar-width: none
|
|
91
|
+
|
|
92
|
+
&::-webkit-scrollbar
|
|
93
|
+
display: none
|
|
94
|
+
|
|
95
|
+
&__item
|
|
96
|
+
display: inline-block
|
|
97
|
+
flex-shrink: 0
|
|
98
|
+
margin-left: unset
|
|
99
|
+
margin-top: 0
|
|
100
|
+
padding: 0
|
|
101
|
+
scroll-snap-align: start
|
|
102
|
+
width: calc((100cqw - (var(--item-count-current) - 1) * var(--content-gutter)) / var(--item-count-current))
|
|
103
|
+
|
|
104
|
+
&__controller
|
|
105
|
+
display: flex
|
|
106
|
+
gap: var(--controller-gutter)
|
|
107
|
+
justify-content: flex-end
|
|
108
|
+
margin-top: var(--controller-margin-top)
|
|
109
|
+
padding: 0 0 10px
|
|
110
|
+
pointer-events: none
|
|
111
|
+
width: 100%
|
|
112
|
+
z-index: 1090
|
|
113
|
+
|
|
114
|
+
&-button
|
|
115
|
+
align-items: center
|
|
116
|
+
background: transparent
|
|
117
|
+
background: var(--controller-button-background)
|
|
118
|
+
border-radius: var(--controller-button-border-radius)
|
|
119
|
+
border: none
|
|
120
|
+
border: var(--controller-button-border)
|
|
121
|
+
box-shadow: var(--controller-button-box-shadow)
|
|
122
|
+
color: var(--controller-button-color)
|
|
123
|
+
cursor: pointer
|
|
124
|
+
display: flex
|
|
125
|
+
height: var(--controller-button-size)
|
|
126
|
+
justify-content: center
|
|
127
|
+
padding: 0
|
|
128
|
+
pointer-events: auto
|
|
129
|
+
position: relative
|
|
130
|
+
transition: all var(--controller-button-transition-duration) ease-in
|
|
131
|
+
width: var(--controller-button-size)
|
|
132
|
+
|
|
133
|
+
&:disabled
|
|
134
|
+
opacity: 0.4
|
|
135
|
+
cursor: not-allowed
|
|
136
|
+
|
|
137
|
+
&::before
|
|
138
|
+
background-color: currentColor
|
|
139
|
+
content: ""
|
|
140
|
+
display: block
|
|
141
|
+
height: 100%
|
|
142
|
+
inset: 0
|
|
143
|
+
mask-image: var(--controller-button-icon)
|
|
144
|
+
mask-position: center
|
|
145
|
+
mask-repeat: no-repeat
|
|
146
|
+
mask-size: var(--controller-button-icon-size)
|
|
147
|
+
position: absolute
|
|
148
|
+
width: 100%
|
|
149
|
+
border-radius: var(--controller-button-border-radius)
|
|
150
|
+
transition: background calc(var(--controller-button-transition-duration) / 2) ease-in
|
|
151
|
+
|
|
152
|
+
&-prev
|
|
153
|
+
scale: -1
|
|
154
|
+
|
|
155
|
+
.scrollbar-track
|
|
156
|
+
display: none
|
|
157
|
+
height: 3px
|
|
158
|
+
margin-top: 8px
|
|
159
|
+
background: var(--track-color, white)
|
|
160
|
+
border-radius: 3px
|
|
161
|
+
position: relative
|
|
162
|
+
width: 100cqw
|
|
163
|
+
|
|
164
|
+
.scrollbar-thumb
|
|
165
|
+
height: 100%
|
|
166
|
+
width: 30%
|
|
167
|
+
background: var(--thumb-color, white)
|
|
168
|
+
border-radius: inherit
|
|
169
|
+
position: absolute
|
|
170
|
+
left: 0
|
|
171
|
+
top: 0
|
|
172
|
+
cursor: grab
|
|
173
|
+
transition: box-shadow 0.2s ease-in-out
|
|
174
|
+
|
|
175
|
+
&:hover
|
|
176
|
+
box-shadow: 0 0 0 3px var(--thumb-color, black)
|
|
177
|
+
|
|
178
|
+
&::before
|
|
179
|
+
content: ''
|
|
180
|
+
position: absolute
|
|
181
|
+
top: 50%
|
|
182
|
+
left: 0
|
|
183
|
+
width: 100%
|
|
184
|
+
height: 3rem
|
|
185
|
+
transform: translateY(-50%)
|