Skip to content

Commit

Permalink
Add js for shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodmillman committed Oct 26, 2021
1 parent 69b7a2f commit 9a41108
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Content Page Shortcuts
const shortcutsTarget = $('#shortcuts');
if (shortcutsTarget.length > 0) {
$('.content-container h2, .content-container h3').map(function(idx, el) {
const title = el.textContent;
// transforms title into snake-case
const elTitle = title.replace(/\s/g, '-').toLowerCase();
// Gets the element type (e.g. h2, h3)
const elType = $(el).get(0).tagName;
// Adds snake-case title as an id attribute to target element
$(el).attr('id', elTitle);
shortcutsTarget.append(`<div id="${elTitle}-shortcut" class="shortcuts-${elType}">${title}</div>`);

$(`#${elTitle}-shortcut`).click(function() {
$([document.documentElement, document.body]).animate({
scrollTop: $(`#${elTitle}`).offset().top-60
}, 1000);
})
});
}

// Removes the shortcuts container if no shortcuts exist.
// Also removes the 'Get Help' link.
if ($('#shortcuts div').length < 1) {
$('.shortcuts-container').css('display', 'none');
}

0 comments on commit 9a41108

Please sign in to comment.