Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment