47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
document.body.addEventListener('htmx:afterSwap', function (event) {
|
|
var target = event.detail.target;
|
|
if (!target) return;
|
|
if (target.id === 'reference-verse') return scrollToHash();
|
|
if (target.id === 'content') return land();
|
|
});
|
|
|
|
document.body.addEventListener('htmx:historyRestore', function () {
|
|
land();
|
|
});
|
|
|
|
function hashTarget() {
|
|
if (!location.hash) return null;
|
|
return document.getElementById(decodeURIComponent(location.hash.slice(1)));
|
|
}
|
|
|
|
function jumpTo(el) {
|
|
el.scrollIntoView({ block: 'start', behavior: 'instant' });
|
|
}
|
|
|
|
function jumpToTop() {
|
|
window.scrollTo({ top: 0, behavior: 'instant' });
|
|
}
|
|
|
|
function scrollToHash() {
|
|
var el = hashTarget();
|
|
if (el) jumpTo(el);
|
|
}
|
|
|
|
function land() {
|
|
var el = hashTarget();
|
|
if (el) jumpTo(el);
|
|
else jumpToTop();
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
scrollToHash();
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
navigator.serviceWorker.register('sw.js').catch(function () {});
|
|
}
|
|
});
|
|
})();
|