Fix scrollspy.js & use query-string@5 to support IE (#1621)

* Fix scrollspy.js syntax error on IE

* Use query-string@5 to support IE
This commit is contained in:
ylemkimon
2018-08-18 21:25:37 +09:00
committed by GitHub
parent 52f3c05ccc
commit 93b1b7947c
3 changed files with 31 additions and 24 deletions

View File

@@ -1,26 +1,31 @@
/* eslint-disable no-var */
// Inspired by ScrollSpy as in e.g. Bootstrap
(function() {
const OFFSET = 10;
let timer;
let headingsCache;
const findHeadings = () => headingsCache ? headingsCache :
document.querySelectorAll('.toc-headings > li > a');
const onScroll = () => {
var OFFSET = 10;
var timer;
var headingsCache;
function findHeadings() {
return headingsCache ? headingsCache :
document.querySelectorAll('.toc-headings > li > a');
}
function onScroll() {
if (timer) { // throttle
return;
}
timer = setTimeout(() => {
timer = setTimeout(function() {
timer = null;
let found = false;
const headings = findHeadings();
for (let i = 0; i < headings.length; i++) {
var found = false;
var headings = findHeadings();
for (var i = 0; i < headings.length; i++) {
// if !found and i is the last element, highlight the last
let current = !found;
var current = !found;
if (!found && i < headings.length - 1) {
const next = headings[i + 1].href.split('#')[1];
const nextHeader = document.getElementById(next);
const top = nextHeader.getBoundingClientRect().top;
var next = headings[i + 1].href.split('#')[1];
var nextHeader = document.getElementById(next);
var top = nextHeader.getBoundingClientRect().top;
// The following tests whether top + scrollTop
// (the top of the header) is greater than scrollTop
// (where scrollTop = window.pageYOffset, the top of
@@ -35,10 +40,11 @@
}
}
}, 100);
};
}
document.addEventListener('scroll', onScroll);
document.addEventListener('resize', onScroll);
document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('DOMContentLoaded', function() {
// Cache the headings once the page has fully loaded.
headingsCache = findHeadings();
onScroll();