Skip to content

Commit

Permalink
[doc toc script] More accurate active link highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Jan 23, 2014
1 parent 765d3a9 commit 62827d6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions doc/activebookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ document.createElement("section");
document.createElement("article");

(function() {
if (!window.addEventListener) return;
var pending = false, prevVal = null;

function updateSoon() {
Expand Down Expand Up @@ -41,8 +42,16 @@ document.createElement("article");
}
}

if (window.addEventListener) {
window.addEventListener("scroll", updateSoon);
window.addEventListener("load", updateSoon);
}
window.addEventListener("scroll", updateSoon);
window.addEventListener("load", updateSoon);
window.addEventListener("hashchange", function() {
setTimeout(function() {
var hash = document.location.hash, found = null, m;
var marks = document.getElementById("nav").getElementsByTagName("a");
for (var i = 0; i < marks.length; i++)
if ((m = marks[i].href.match(/(#.*)/)) && m[1] == hash) { found = i; break; }
if (found != null) for (var i = 0; i < marks.length; i++)
marks[i].className = i == found ? "active" : "";
}, 300);
});
})();

0 comments on commit 62827d6

Please sign in to comment.