-
Notifications
You must be signed in to change notification settings - Fork 12
/
toc.js
144 lines (133 loc) · 3.98 KB
/
toc.js
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
( function ( document, window ) {
"use strict";
// Teeny-tiny scroll animation lib
var scrollTo = ( function ( window ) {
var requestAnimationFrame = window.requestAnimationFrame;
if ( !requestAnimationFrame ) {
return function ( scrollTop ) {
window.scrollTo( 0, scrollTop );
};
}
var easeInOut = function ( t ) {
return t < 0.5 ?
8 * t * t * t * t :
1 - 8 * (--t) * t * t * t;
};
var endTime = 0;
var startValue = 0;
var endValue = 0;
var deltaValue = 0;
var DURATION = 500;
var drawFrame = function ( frameStartTime ) {
if ( !endTime ) {
endTime = frameStartTime + DURATION;
}
if ( frameStartTime >= endTime ) {
window.scrollTo( 0, endValue );
endTime = 0;
return;
}
requestAnimationFrame( drawFrame );
var animationTime = 1 - ( ( endTime - frameStartTime ) / DURATION );
window.scrollTo( 0,
startValue + easeInOut( animationTime ) * deltaValue );
};
return function ( scrollTop ) {
startValue = window.pageYOffset;
endValue = scrollTop;
deltaValue = endValue - startValue;
if ( !endTime ) {
requestAnimationFrame( drawFrame );
} else {
endTime = 0;
}
};
}( window ) );
// 1. Build TOC
var headings = document.querySelectorAll( 'h2, h3' ),
toc = document.querySelector( '.toc' ),
frag = document.createDocumentFragment(),
index = {},
tocItems = [],
i, l,
heading, text, url,
li, a, span,
item;
for ( i = 0, l = headings.length; i < l; i += 1 ) {
heading = headings[i];
text = heading.textContent;
url = '#' + heading.id;
while ( index[ url ] ) {
url += '*';
}
li = document.createElement( 'li' );
li.className = 'toc-item toc-' + heading.nodeName.toLowerCase();
a = document.createElement( 'a' );
a.className = 'toc-link';
a.href = url;
span = document.createElement( 'span' );
span.textContent = text;
item = {
heading: heading,
li: li,
top: 1 << 30
};
index[ url ] = item;
tocItems.push( item );
a.appendChild( span );
li.appendChild( a );
frag.appendChild( li );
}
toc.appendChild( frag );
// 2. Cache positions, recalculate on potential change
var pageHeaderHeight = document.querySelector( 'header' ).offsetHeight;
var recalculatePositions = function () {
for ( var i = 0, l = tocItems.length, item; i < l; i += 1 ) {
item = tocItems[i];
item.top = item.heading.offsetTop - pageHeaderHeight - 15;
}
};
window.addEventListener( 'load', recalculatePositions, false );
window.addEventListener( 'resize', recalculatePositions, false );
// 3. Make the links work
toc.addEventListener( 'click', function ( event ) {
var target = event.target;
var href, item;
while ( target && target.nodeName !== 'A' ) {
target = target.parentNode;
}
if ( target ) {
href = target.getAttribute( 'href' );
item = index[ href ];
}
if ( item ) {
event.preventDefault();
history.pushState( null, document.title, href );
scrollTo( item.top );
}
});
window.addEventListener( 'hashchange', function () {
var item = index[ location.hash ];
if ( item ) {
scrollTo( item.top );
}
}, false );
// 4. Highlight current section in TOC
var active = null;
var tocSelected = document.querySelector( '.toc-selected' );
window.addEventListener( 'scroll', function () {
var currentTop = window.pageYOffset;
var newActive = tocItems[0];
for ( i = 1; i < l; i += 1 ) {
if ( tocItems[i].top > currentTop ) {
break;
}
newActive = tocItems[i];
}
if ( newActive !== active ) {
active = newActive;
tocSelected.style.top = newActive.li.offsetTop + 'px';
tocSelected.style.height = newActive.li.offsetHeight + 'px';
}
}, false );
}( document, window ) );