Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active December 1, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martynchamberlin/9456925b17c078d2846cced73f3ee96b to your computer and use it in GitHub Desktop.
Save martynchamberlin/9456925b17c078d2846cced73f3ee96b to your computer and use it in GitHub Desktop.
JavaScript Footnotes
$(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();
});
});
@admin-mccallumpartners
Copy link

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.

@martynchamberlin
Copy link
Author

@admin-mccallumpartners You bet. Glad it was helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment