Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active December 1, 2020 15:21
Show Gist options
  • 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();
});
});
@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