From 16c83df293e294ce142bb55366d02bec82b3035b Mon Sep 17 00:00:00 2001
From: Dan Allen
Date: Sat, 18 Nov 2017 19:39:03 -0700
Subject: [PATCH] add JavaScript for fragment jumper
---
preview-site-src/index.html | 8 ++++----
src/js/02-fragment-jumper.js | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 4 deletions(-)
create mode 100644 src/js/02-fragment-jumper.js
diff --git a/preview-site-src/index.html b/preview-site-src/index.html
index e149f2f..8866334 100644
--- a/preview-site-src/index.html
+++ b/preview-site-src/index.html
@@ -15,7 +15,7 @@ Vis no velit audiam, sonet praesent eum ne.
@@ -65,7 +65,7 @@ gulp.task('js', () =>
Cum dicat putant ne.
-Est in reque homero principes, meis deleniti mediocrem ad has.
+Est in reque homero principes, meis deleniti mediocrem ad has.
Altera atomorum his ex, has cu elitr melius propriae.
Eos suscipit scaevola at.
@@ -240,7 +240,7 @@ Eos suscipit scaevola at.
- I wouldn’t try that if I were you.
+ I wouldn’t try that if I were you.
|
@@ -286,4 +286,4 @@ Altera atomorum his ex, has cu elitr melius propriae.
Eos suscipit scaevola at.
-
\ No newline at end of file
+
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))
+ }
+ })
+})()
--
2.11.0