Skip to content

Commit

Permalink
Change URL's hash fragment when clicking on TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Jan 4, 2019
1 parent 0a1dfc3 commit 7a04a4b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
18 changes: 8 additions & 10 deletions assets/js/_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,24 @@ $(document).ready(function() {
}, 400);
});

// init smooth scroll
// Smooth scrolling

// Bind the hashchange event listener
// Bind the hashchange event listener to support back/forward buttons
$(window).bind("hashchange", function (event) {
$.smoothScroll({
// Replace '#/' with '#' to go to the correct target
scrollTarget: location.hash.replace(/^\#\/?/, "#"),
scrollTarget: location.hash,
offset: -20
});
});
// Override clicking on links to smooth scroll
$('a[href*="#"]').bind("click", function (event) {
// Remove '#' from the hash.
var hash = this.hash.replace(/^#/, "");
if (this.pathname === location.pathname && hash) {
if (this.pathname === location.pathname && this.hash) {
event.preventDefault();
// Change '#' (removed above) to '#/' so it doesn't jump without the smooth scrolling
location.hash = "#/" + hash;
history.pushState(null, null, this.hash);
$(window).trigger("hashchange");
}
});
// Trigger hashchange event on page load if there is a hash in the URL.
// Smooth scroll on page load if there is a hash in the URL.
if (location.hash) {
$(window).trigger("hashchange");
}
Expand Down
Loading

0 comments on commit 7a04a4b

Please sign in to comment.