Last active
December 1, 2020 15:21
-
-
Save martynchamberlin/9456925b17c078d2846cced73f3ee96b to your computer and use it in GitHub Desktop.
JavaScript Footnotes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(() => { | |
/** | |
* @param {element} e The element that was clicked | |
* @return {element} The element that is being pointed to | |
*/ | |
const goTo = (e) => { | |
let goTo = $(e.currentTarget).attr('href'); | |
// remove the pound sign | |
goTo = goTo.substr(1); | |
// Use document.getElementById() to accomodate colon in element id | |
// (https://stackoverflow.com/a/11862160/1591507) | |
return $(document.getElementById(goTo)); | |
}; | |
/** | |
* The amount of top padding when scrolling to/from a footnote | |
*/ | |
const padding = 10; | |
$('.footnote').click((e) => { | |
$(window).scrollTop(goTo(e).offset().top - padding); | |
e.preventDefault(); | |
}); | |
$('.reversefootnote').click((e) => { | |
$(window).scrollTop(goTo(e).parents().offset().top - padding); | |
e.preventDefault(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Martyn, thanks for sharing this and the blog post https://www.drinkingcaffeine.com/2017/08/12/a-better-way-to-handle-website-footnotes/
It was super easy to implement and really helped with my footnotes getting caught in my nav bar.