Skip to content

Commit 33551c8

Browse files
edemainemmistakes
authored andcommittedJan 4, 2019
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
1 parent 2784b3a commit 33551c8

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed
 

‎assets/js/_main.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,27 @@ $(document).ready(function() {
6161
}, 400);
6262
});
6363

64-
// init smooth scroll
65-
$("a").smoothScroll({ offset: -20 });
64+
// Smooth scrolling
65+
66+
// Bind popstate event listener to support back/forward buttons.
67+
$(window).bind("popstate", function (event) {
68+
$.smoothScroll({
69+
scrollTarget: location.hash,
70+
offset: -20
71+
});
72+
});
73+
// Override clicking on links to smooth scroll
74+
$('a[href*="#"]').bind("click", function (event) {
75+
if (this.pathname === location.pathname && this.hash) {
76+
event.preventDefault();
77+
history.pushState(null, null, this.hash);
78+
$(window).trigger("popstate");
79+
}
80+
});
81+
// Smooth scroll on page load if there is a hash in the URL.
82+
if (location.hash) {
83+
$(window).trigger("popstate");
84+
}
6685

6786
// add lightbox class to all image links
6887
$(

‎assets/js/main.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.