-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrollto.js
More file actions
29 lines (28 loc) · 1.09 KB
/
scrollto.js
File metadata and controls
29 lines (28 loc) · 1.09 KB
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
// Scroll to an anchor on this page without clogging "back" history
function scrollToAnchor(selectedAnchor) {
// Scroll to the anchor
document.querySelector(selectedAnchor).scrollIntoView();
// Replace previous history with this page
history.replaceState(null, "", selectedAnchor);
}
// Go through and replace all links to anchors on this page with onclick events
function replaceAnchorLinks() {
// Get this page's URL without any anchors
var leading = window.location.href.replace(/\#.*$/, "")
// For each anchor element
document.querySelectorAll("a").forEach(oldAnchor => {
// If it links to another anchor on this page
if (oldAnchor.href.startsWith(leading + "#")) {
// Find out where it links to
var name = oldAnchor.href.match(/\#.*$/)
// Make a deep clone of it
var newAnchor = oldAnchor.cloneNode(true);
// Add an onlick event to scroll to the anchor
newAnchor.onclick = () => scrollToAnchor(name);
// Remove the old link
newAnchor.removeAttribute("href");
// Replace the original with the clone
oldAnchor.parentNode.replaceChild(newAnchor, oldAnchor);
}
});
}