themes/docsy/assets/js/active.js (35 lines of code) (raw):

$(function () { init(); function init() { bindClick(); setActive(); } function bindClick() { $('.container a').not('.link-type').on('click', function () { var hash = $(this).attr('href') if (hash && /^#/.test(hash)) { scrollTop(hash); $('.card-wrapper .card').removeClass('active'); $(hash).parents('.card').addClass('active'); } }); $('.link-type').on('click', function () { var hash = $(this).attr('href') scrollTop(hash, 120); }) } function scrollTop(hash, offset) { if (!$(hash).length) { return; } $('html,body').animate({scrollTop: $(hash).offset().top - (offset || 160)}) } function setActive() { var hash = location.hash; if (!location.hash || !$(hash).length) { return; } $(hash).parents('.card').addClass('active'); scrollTop(hash) } })