diff options
| author | Dan Allen <dan@opendevise.com> | 2017-11-18 19:39:03 -0700 |
|---|---|---|
| committer | Dan Allen <dan@opendevise.com> | 2017-11-20 16:55:06 -0700 |
| commit | 16c83df293e294ce142bb55366d02bec82b3035b (patch) | |
| tree | cd51721cd75661d3bce53d1848d347ae377446d1 /src | |
| parent | 43cb449d047ed9053bd84d093a2dec8cc59c336f (diff) | |
| download | alpine-antora-theme-16c83df293e294ce142bb55366d02bec82b3035b.tar.bz2 alpine-antora-theme-16c83df293e294ce142bb55366d02bec82b3035b.tar.xz | |
add JavaScript for fragment jumper
Diffstat (limited to 'src')
| -rw-r--r-- | src/js/02-fragment-jumper.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/js/02-fragment-jumper.js b/src/js/02-fragment-jumper.js new file mode 100644 index 0000000..f680f75 --- /dev/null +++ b/src/js/02-fragment-jumper.js @@ -0,0 +1,37 @@ +;(function () { + 'use strict' + + var article = document.querySelector('article.doc') + var toolbar = document.querySelector('.toolbar') + + function computePosition (el, sum) { + if (article.contains(el)) { + return computePosition(el.offsetParent, el.offsetTop + sum) + } else { + return sum + } + } + + function jumpToAnchor (e) { + if (e) { + window.location.hash = '#' + this.id + e.preventDefault() + } + window.scrollTo(0, computePosition(this, 0) - toolbar.getBoundingClientRect().bottom) + } + + window.addEventListener('load', function jumpOnLoad (e) { + var hash, target + if ((hash = window.location.hash) && (target = document.getElementById(hash.slice(1)))) { + jumpToAnchor.bind(target)() + } + window.removeEventListener('load', jumpOnLoad) + }) + + Array.prototype.slice.call(document.querySelectorAll('.doc a[href^="#"]')).forEach(function (el) { + var hash, target + if ((hash = el.hash.slice(1)) && (target = document.getElementById(hash))) { + el.addEventListener('click', jumpToAnchor.bind(target)) + } + }) +})() |
