Clicking TOC links changes hash fragment (#2019)

* Fix smooth scroll breaking back button #1767
* Change URL's hash fragment when clicking on TOC
* Switch from hashchange to popstate event handler

This seems to have better behavior with the Forward button, on page load,
and always scrolls in the right direction.

Close #1767
This commit is contained in:
Erik Demaine 2019-01-04 12:31:38 -05:00 committed by Michael Rose
parent 2784b3ad43
commit 33551c8cdb
2 changed files with 25 additions and 6 deletions

View file

@ -61,8 +61,27 @@ $(document).ready(function() {
}, 400);
});
// init smooth scroll
$("a").smoothScroll({ offset: -20 });
// Smooth scrolling
// Bind popstate event listener to support back/forward buttons.
$(window).bind("popstate", function (event) {
$.smoothScroll({
scrollTarget: location.hash,
offset: -20
});
});
// Override clicking on links to smooth scroll
$('a[href*="#"]').bind("click", function (event) {
if (this.pathname === location.pathname && this.hash) {
event.preventDefault();
history.pushState(null, null, this.hash);
$(window).trigger("popstate");
}
});
// Smooth scroll on page load if there is a hash in the URL.
if (location.hash) {
$(window).trigger("popstate");
}
// add lightbox class to all image links
$(

File diff suppressed because one or more lines are too long