-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
418 lines (357 loc) · 11.9 KB
/
test.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!doctype html>
<html lang=en-US>
<meta charset=UTF-8>
<title>Scrollspy example for mobile</title>
<meta name=viewport content="width=device-width,initial-scale=1">
<link href=node_modules/sanitize.css/sanitize.css rel=stylesheet>
<style>
:root {
--scrollspy-height: 60px;
}
body {
max-width: 600px;
padding-bottom: 30vh;
margin: 0 auto;
scroll-behavior: smooth;
line-height: 2em;
}
h1 {
text-align: center;
}
h2 {
margin: 0;
}
ul#langs {
padding: 0;
list-style: none;
text-align: right;
}
#langs li {
display: inline-block;
}
nav {
position: -webkit-sticky;
position: sticky;
top: 0;
height: var(--scrollspy-height, 2em);
z-index: 12;
overflow-y: hidden;
text-align: center;
}
nav[aria-expanded="true"] {
height: auto;
}
nav ul {
margin: 0;
padding: 0;
transition: transform 100ms;
}
nav[data-scrollspy-target="whatsthis"] ul {
transform: translateY(calc(-0 * var(--scrollspy-height)));
}
nav[data-scrollspy-target="motivation"] ul {
transform: translateY(calc(-1 * var(--scrollspy-height)));
}
nav[data-scrollspy-target="smoothscroll"] ul {
transform: translateY(calc(-2 * var(--scrollspy-height)));
}
nav[data-scrollspy-target="sticky"] ul {
transform: translateY(calc(-3 * var(--scrollspy-height)));
}
nav[data-scrollspy-target="custom-property"] ul {
transform: translateY(calc(-4 * var(--scrollspy-height)));
}
nav[data-scrollspy-target="source-code"] ul {
transform: translateY(calc(-5 * var(--scrollspy-height)));
}
nav[aria-expanded="true"] ul {
transform: translateY(0);
}
nav li {
height: var(--scrollspy-height, 2em);
}
nav li:nth-of-type(1) {
background-color: #ff0000;
}
nav li:nth-of-type(2) {
background-color: #ff7f00;
}
nav li:nth-of-type(3) {
background-color: #ffff00;
}
nav li:nth-of-type(4) {
background-color: #00ff00;
}
nav li:nth-of-type(5) {
background-color: #0000ff;
}
nav li:nth-of-type(6) {
background-color: #6600ff;
}
nav li a {
display: block;
width: 100%;
height: 100%;
text-decoration: none;
color: white;
font-weight: bold;
line-height: var(--scrollspy-height, 2em);
}
nav li:nth-of-type(3) a {
color: gray;
}
nav button {
position: absolute;
top: 0;
right: 0;
display: inline-block;
width: 100%;
height: var(--scrollspy-height, 2em);
padding: 0;
padding-right: 0.5em;
line-height: var(--scrollspy-height, 2em);
text-align: right;
text-decoration: none;
cursor: pointer;
transition: transform 300ms;
}
nav button[aria-expanded="true"] {
width: auto;
padding-right: 0.5em;
transform: rotateX(180deg);
}
section {
min-height: 80vh;
padding: var(--scrollspy-height, 2em) 1em;
border-bottom: 1px solid transparent;
}
section:nth-of-type(1) {
background-color: #ffcccc;
}
section:nth-of-type(2) {
background-color: #ffeacc;
}
section:nth-of-type(3) {
background-color: #fffecc;
}
section:nth-of-type(4) {
background-color: #c7f5c4;
}
section:nth-of-type(5) {
background-color: #c4daf4;
}
section:nth-of-type(6) {
background-color: #e1c4f4;
}
pre {
margin: 1em;
padding: 1em 0.5em;
background-color: #eeeeee;
}
code {
margin-left: 0.1em;
margin-right: 0.1em;
letter-spacing: 0.1em;
}
p code {
text-decoration: underline;
text-decoration-style: dashed;
}
</style>
<h1 id=top>Scrollspy example for mobile</h1>
<ul id="langs">
<li><a href="./">ja</a></li>
<li>en</li>
</ul>
<nav id=nav role=listbox aria-expanded=false>
<ul>
<li><a href=#whatsthis role=option>What's this?</a></li>
<li><a href=#motivation role=option>Motivation</a></li>
<li><a href=#smoothscroll role=option>Smooth scroll by CSS</a></li>
<li><a href=#sticky role=option>Sticky positioning by CSS</a></li>
<li><a href=#custom-property role=option>CSS variables</a></li>
<li><a href=#source-code role=option>Source code</a></li>
</ul>
<button aria-controls=nav aria-expanded=false>▼</button>
</nav>
<section id=whatsthis>
<h2>What's this?</h2>
<p>This is an example of scrollspy for mobile.</p>
<p>Scrollspy is often shown as table of contents in sidebars. But, for mobile, we rarely see scrollspy because sidebars are usually not generated.</p>
<p>This is a proposal of scrollspy for mobile which is fixed on the top, shows only single line and changes it on scrolling.</p>
</section>
<section id=motivation>
<h2>Motivation for this page</h2>
<p>I was impressed by <a href="https://blog.jxck.io/entries/2016-06-25/intersection-observer.html">Intersection Observer を用いた要素出現検出の最適化</a>("Optimization of detect of element appearance by Intersection Observer". You can see similar article in English here: <a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">Intersection Observer API</a>) and wanted to use it, then, chose scrollspy as a material.</p>
</section>
<section id=smoothscroll>
<h2>Smooth scroll by CSS</h2>
<p>CSS has <code>scroll-behavior</code> propery and by setting the value to <code>smooth</code> such as:</p>
<pre><code>body {
scroll-behavior: smooth;
}</code></pre>
<p>browsers run smooth scroll automatically. This page also uses the feature.</p>
<p>Firefox(except for on iOS) supports it as of <time datetime="2016-09">September 2016</time>. Try this page on Firefox.</p>
<p>See also: <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior">scroll-behavior - CSS | MDN</a></p>
</section>
<section id=sticky>
<h2>Sticky poisitioning only by CSS(<code>position: sticky</code>)</h2>
<p>The navigation by scrollspy in this page is under the page title at first, and after scrolling, becomes fixed at the top of the page.</p>
<p>This kind of feature is often implemented by combination of JavaScript and CSS. But it was done by only CSS in this page. By setting <code>position</code> property to <code>sticky</code> such as:</p>
<pre><code>nav {
position: sticky;
/*
:
:
*/
}</code></pre>
<p>the behavior as said above is achieved.</p>
<p>As of <time datetime="2016-09">September 2016</time>, Firefox and Safari support the property.</p>
<p>See also: <a href="https://www.sitepoint.com/css-position-sticky-introduction-polyfills/">CSS "position: sticky" - Introduction and Polyfills</a></p>
</section>
<section id=custom-property>
<h2>CSS custom properties(CSS variables)</h2>
<p>The height of scrollspy is defined by a CSS custom property although you cannot see it directly in the page.</p>
<p>Each section has top padding whose height is the same to scrollspy's. Leaks of setting is prevented by managing the hight by a shared variable.</p>
<p>Now Chrome and Safari support CSS custom properties as well as Firefox.</p>
<p>See also: <a href="https://developers.google.com/web/updates/2016/02/css-variables-why-should-you-care">CSS Variables: Why Should You Care? | Web | Google Developers</a></p>
</section>
<section id=source-code>
<h2>Source code</h2>
<p>Source code of this page is hosted by <a href="https://gitlab.com">GitLab</a>.</p>
<p>Source code:<a href="https://gitlab.com/KitaitiMakoto/scrollspy-example">KitaitiMakoto/scrollspy-example</a></p>
</section>
<p>©<a href="https://gitlab.com/KitaitiMakoto">Kitaiti Makoto</a></p>
<script src=node_modules/intersection-observer/intersection-observer.js></script>
<script>
'use strict';
class Scrollspy {
constructor(element, ids, observerOptions) {
this.element = element;
this.targets = [];
this.targetIndices = {};
this.indicesInViewPort = [];
ids.forEach(function(id, index) {
this.targets.push(document.getElementById(id));
this.targetIndices[id] = index;
}.bind(this));
var observer = new IntersectionObserver(this.onIntersectionChange.bind(this), observerOptions);
this.targets.forEach(observer.observe.bind(observer));
}
onIntersectionChange(changes) {
var oldTargetIndex = this.indicesInViewPort[0] || 0;
for (var i = changes.length - 1; i >= 0; i--) {
this.updateIndicesInViewPort(changes[i], oldTargetIndex);
}
if (this.indicesInViewPort.length === 0) {
return;
}
if (oldTargetIndex === this.indicesInViewPort[0]) {
return;
}
var event = new CustomEvent('targetchange', {
detail: {
newTarget: this.targets[this.indicesInViewPort[0]],
oldTarget: this.targets[oldTargetIndex]
}
});
this.element.dispatchEvent(event);
}
updateIndicesInViewPort(change, oldTargetIndex) {
var index = this.targetIndices[change.target.id];
if (change.intersectionRatio === 0) {
var indexInViewPort = this.indicesInViewPort.indexOf(index);
if (indexInViewPort !== -1) {
this.indicesInViewPort.splice(indexInViewPort, 1);
}
} else {
if (index < oldTargetIndex) {
this.indicesInViewPort.unshift(index);
} else if (index > this.indicesInViewPort[this.indicesInViewPort.length - 1]) {
this.indicesInViewPort.push(index);
} else {
this.indicesInViewPort.push(index);
this.indicesInViewPort.sort();
}
}
}
}
class Navigation {
constructor(element) {
this.element = element;
this.links = {};
var as = this.element.getElementsByTagName('a');
for (var i = 0, l = as.length; i < l; i++) {
var a = as[i];
this.links[a.hash.slice(1)] = a;
a.addEventListener('click', this.collapse.bind(this), {passive: true});
}
this.button = this.element.getElementsByTagName('button')[0];
this.button.addEventListener('click', this.toggle.bind(this)), {passive: true};
}
get expanded() {
return this.element.getAttribute('aria-expanded') === 'true';
}
get height() {
return getComputedStyle(this.element).getPropertyValue('--scrollspy-height').trim();
}
onTargetChange(event) {
var newTargetId = event.detail.newTarget.id;
this.links[event.detail.oldTarget.id].setAttribute('aria-selected', 'false');
this.links[newTargetId].setAttribute('aria-selected', 'true');
this.element.dataset.scrollspyTarget = newTargetId;
history.pushState(null, null, location.href);
console.log(location.href);
}
expand() {
this.element.setAttribute('aria-expanded', 'true');
this.button.setAttribute('aria-expanded', 'true');
}
collapse() {
this.element.setAttribute('aria-expanded', 'false');
this.button.setAttribute('aria-expanded', 'false');
}
toggle() {
if (this.expanded) {
this.collapse();
} else {
this.expand();
}
}
}
class HistoryManager {
onTargetChange(event) {
history.replaceState(null, null, '#' + event.detail.newTarget.id);
}
}
class HashchangeDispatcher {
onTargetChange(event) {
var hashchangeEvent = new Event('hashchange');
var oldURL = new URL(location.href);
if (event.detail.oldTarget) {
oldURL.hash = '#' + event.detail.oldTarget.id;
}
hashchangeEvent.oldURL = oldURL.toString();
if (event.detail.newTarget) {
var newURL = new URL(location.href);
newURL.hash = '#' + event.detail.newTarget.id;
hashchangeEvent.newURL = newURL.toString();
}
dispatchEvent(hashchangeEvent);
}
}
document.addEventListener('DOMContentLoaded', function() {
var nav = document.getElementsByTagName('nav')[0];
var navigation = new Navigation(nav);
var historyManager = new HistoryManager();
var hashchangeDispatcher = new HashchangeDispatcher();
[navigation, historyManager, hashchangeDispatcher].forEach(function(listener) {
nav.addEventListener('targetchange', listener.onTargetChange.bind(listener), {passive: true});
});
addEventListener('hashchange', function(event) {
console.dir(event);
}, {passive: true});
new Scrollspy(nav, Object.keys(navigation.links), {rootMargin: `-${navigation.height}`});
});
</script>
</html>