|
6 | 6 | <title>Scroll snap</title>
|
7 | 7 | <style>
|
8 | 8 | li {
|
| 9 | + /* |
| 10 | + starts with: |
| 11 | + scroll-snap-align: center center; |
| 12 | + scroll-snap-stop: normal (defaults); |
| 13 | + |
| 14 | + CSS gets changed with JavaScript when you change the controls. |
| 15 | + the following can be set: |
| 16 | + scroll-snap-stop: always | normal; |
| 17 | + scroll-snap-align: start | center | end {2} |
| 18 | + */ |
9 | 19 | }
|
10 | 20 | ul {
|
11 | 21 | overflow: auto;
|
12 | 22 | scroll-snap-type: both mandatory;
|
13 | 23 | overscroll-behavior-x: contain;
|
14 | 24 | }
|
| 25 | + article.snapDisabled fieldset { |
| 26 | + opacity: 20%; |
| 27 | + pointer-events: none; |
| 28 | + } |
| 29 | + article.snapDisabled ul { |
| 30 | + scroll-snap-type: initial; |
| 31 | + overscroll-behavior-x: initial; |
| 32 | + } |
15 | 33 |
|
16 | 34 | @layer pageSetup {
|
17 | 35 | article {
|
18 | 36 | display: flex;
|
19 |
| - gap: 1vw; |
| 37 | + gap: 2vw; |
20 | 38 | }
|
21 | 39 | div {
|
22 | 40 | flex: 1;
|
23 | 41 | }
|
24 |
| - input { |
25 |
| - vertical-align: bottom; |
26 |
| - } |
27 |
| - p { |
28 |
| - font-family: monospace; |
29 |
| - } |
30 |
| - } |
31 |
| - @layer exampleSetup { |
32 | 42 | ul {
|
33 | 43 | display: grid;
|
34 | 44 | gap: 6.25vw;
|
|
66 | 76 | li::after {
|
67 | 77 | content: counter(items);
|
68 | 78 | }
|
| 79 | + input { |
| 80 | + vertical-align: bottom; |
| 81 | + } |
| 82 | + p { |
| 83 | + font-family: monospace; |
| 84 | + } |
69 | 85 | }
|
70 | 86 | </style>
|
71 | 87 | </head>
|
| 88 | + |
72 | 89 | <body>
|
73 | 90 | <article>
|
74 | 91 | <ul>
|
|
121 | 138 | <li></li>
|
122 | 139 | </ul>
|
123 | 140 | <div>
|
| 141 | + <fieldset> |
| 142 | + <legend>Change the options</legend> |
| 143 | + <p> |
| 144 | + <label |
| 145 | + ><input |
| 146 | + type="range" |
| 147 | + min="0" |
| 148 | + max="2" |
| 149 | + value="1" |
| 150 | + list="places" |
| 151 | + id="block" |
| 152 | + /> |
| 153 | + block position</label |
| 154 | + > |
| 155 | + </p> |
| 156 | + <p> |
| 157 | + <label |
| 158 | + ><input |
| 159 | + type="range" |
| 160 | + min="0" |
| 161 | + max="2" |
| 162 | + value="1" |
| 163 | + list="places" |
| 164 | + id="inline" |
| 165 | + /> |
| 166 | + inline position</label |
| 167 | + > |
| 168 | + </p> |
| 169 | + <p> |
| 170 | + <label><input type="checkbox" id="stop" /> Stop on all boxes</label> |
| 171 | + </p> |
| 172 | + </fieldset> |
| 173 | + |
124 | 174 | <p>
|
125 |
| - <label |
126 |
| - ><input |
127 |
| - type="range" |
128 |
| - min="0" |
129 |
| - max="2" |
130 |
| - value="1" |
131 |
| - list="places" |
132 |
| - id="block" |
133 |
| - /> |
134 |
| - block position</label |
135 |
| - > |
136 |
| - </p> |
137 |
| - <p> |
138 |
| - <label |
139 |
| - ><input |
140 |
| - type="range" |
141 |
| - min="0" |
142 |
| - max="2" |
143 |
| - value="1" |
144 |
| - list="places" |
145 |
| - id="inline" |
146 |
| - /> |
147 |
| - inline position</label |
148 |
| - > |
149 |
| - </p> |
150 |
| - <p> |
151 |
| - <label><input type="checkbox" id="stop" /> Stop on all boxes</label> |
152 |
| - <datalist id="places"> |
153 |
| - <option value="0">start</option> |
154 |
| - <option value="1">center</option> |
155 |
| - <option value="2">end</option> |
156 |
| - </datalist> |
| 175 | + <label><input type="checkbox" id="snap" /> disable snapping</label> |
157 | 176 | </p>
|
| 177 | + |
| 178 | + <datalist id="places"> |
| 179 | + <option value="0">start</option> |
| 180 | + <option value="1">center</option> |
| 181 | + <option value="2">end</option> |
| 182 | + </datalist> |
158 | 183 | </div>
|
159 | 184 | </article>
|
| 185 | + |
160 | 186 | <script>
|
161 | 187 | const positions = ["start", "center", "end"];
|
162 | 188 | const inlineDirection = document.getElementById("inline");
|
163 | 189 | const blockDirection = document.getElementById("block");
|
164 | 190 | const stop = document.getElementById("stop");
|
| 191 | + const snap = document.getElementById("snap"); |
| 192 | + const all = document.querySelector("article"); |
165 | 193 | const rules = document.styleSheets[0].cssRules;
|
166 | 194 |
|
167 | 195 | inlineDirection.addEventListener("change", () => {
|
168 | 196 | setSSA();
|
169 | 197 | });
|
170 |
| - |
171 | 198 | blockDirection.addEventListener("change", () => {
|
172 | 199 | setSSA();
|
173 | 200 | });
|
174 |
| - |
175 | 201 | stop.addEventListener("change", () => {
|
176 | 202 | setSST();
|
177 | 203 | });
|
178 |
| - |
179 | 204 | window.addEventListener("load", () => {
|
180 | 205 | setSST();
|
181 | 206 | setSSA();
|
182 | 207 | });
|
| 208 | + snap.addEventListener("change", () => { |
| 209 | + all.classList.toggle("snapDisabled"); |
| 210 | + }); |
183 | 211 |
|
184 | 212 | function setSSA() {
|
185 | 213 | rules[0].style.scrollSnapAlign = `${positions[blockDirection.value]} ${
|
|
0 commit comments