Skip to content

Commit 5b32a23

Browse files
committed
Debounce TOC refreshes and height recaches
1 parent 366d3f1 commit 5b32a23

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

source/javascripts/app/_toc.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
var headerHeights = {};
55

6+
// hides the TOC on mobile devices
67
var closeToc = function() {
78
$(".toc-wrapper").removeClass('open');
89
$("#nav-button").removeClass('open');
@@ -30,10 +31,20 @@
3031
}
3132
}
3233

33-
var target = $(".toc a[href='#" + best + "']");
34-
console.log("new target " + best);
34+
$(".toc a[href='#" + best + "']").addClass("active").parentsUntil('.toc', 'li').addClass("open");
35+
};
3536

36-
target.addClass("active").parentsUntil('.toc', 'li').addClass("open");
37+
var debounce = function(func, waitTime) {
38+
var timeout = false;
39+
return function() {
40+
if (timeout === false) {
41+
setTimeout(function() {
42+
func();
43+
timeout = false;
44+
}, waitTime);
45+
timeout = true;
46+
}
47+
};
3748
};
3849

3950
var makeToc = function() {
@@ -47,11 +58,18 @@
4758

4859
$(".page-wrapper").click(closeToc);
4960
$(".toc-item").click(closeToc);
61+
62+
// reload immediately after scrolling on toc click
63+
$('.toc a').click(function() {
64+
setTimeout(refreshToc, 1);
65+
});
66+
67+
$(window).scroll(debounce(refreshToc, 200));
68+
$(window).resize(debounce(recacheHeights, 200));
5069
};
5170

5271
$(makeToc);
5372

54-
global.onscroll = refreshToc;
5573
global.recacheHeights = recacheHeights;
5674

5775
})(window);

0 commit comments

Comments
 (0)