From da208441c0cd5a867b0f1116ac36d6d7b4c5608c Mon Sep 17 00:00:00 2001 From: matcornic Date: Tue, 1 Aug 2017 21:20:15 +0200 Subject: [PATCH] feat: add sticky bar with smooth scroll --- exampleSite/README.md | 4 +- layouts/partials/favicon.html | 1 + layouts/partials/header.html | 2 +- static/js/hugo-learn.js | 8 ++-- static/js/learn.js | 83 +++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 7 deletions(-) diff --git a/exampleSite/README.md b/exampleSite/README.md index 07d4cad079..b68338b9c2 100644 --- a/exampleSite/README.md +++ b/exampleSite/README.md @@ -3,9 +3,9 @@ - créer une section Showcase - refaire la possibilité d'overrider le style/script/etc -> TODO - créer des jolis thèmes de base (avec des noms) -> TODO -- ajouter Travis pour tester le thème quotidiennement avec les nouvelles versions de Hugo -> TODO - +- ajouter les attachments -> TODO +- ajouter Travis pour tester le thème quotidiennement avec les nouvelles versions de Hugo -> OK - #54 -> OK - corriger slider menu qui ne fonctionne plus -> OK - Update font awesome -> OK diff --git a/layouts/partials/favicon.html b/layouts/partials/favicon.html index 08d54111be..41bbe8500f 100644 --- a/layouts/partials/favicon.html +++ b/layouts/partials/favicon.html @@ -1 +1,2 @@ + diff --git a/layouts/partials/header.html b/layouts/partials/header.html index 1d56375073..35af4673ad 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -34,7 +34,7 @@ {{ partial "menu.html" . }}
-
+
{{if not .IsHome}}
{{ if and (or .IsPage .IsSection) .Site.Params.editURL }} diff --git a/static/js/hugo-learn.js b/static/js/hugo-learn.js index 27c2679e52..a90f35a772 100644 --- a/static/js/hugo-learn.js +++ b/static/js/hugo-learn.js @@ -56,10 +56,10 @@ images.each(function(index){ }); // Stick the top to the top of the screen when scrolling -//$("#top-bar").stick_in_parent( { -// parent: ".sticky-parent", -// spacer: ".sticky-spacer", -//}); +$("#top-bar").stick_in_parent( { + parent: ".sticky-parent", + spacer: ".sticky-spacer", +}); jQuery(document).ready(function() { diff --git a/static/js/learn.js b/static/js/learn.js index 29dddb3463..ab67ac37e9 100644 --- a/static/js/learn.js +++ b/static/js/learn.js @@ -251,6 +251,89 @@ jQuery(document).ready(function() { }); } + /** + * Fix anchor scrolling that hides behind top nav bar + * Courtesy of https://stackoverflow.com/a/13067009/28106 + * + * We could use pure css for this if only heading anchors were + * involved, but this works for any anchor, including footnotes + **/ + (function (document, history, location) { + var HISTORY_SUPPORT = !!(history && history.pushState); + + var anchorScrolls = { + ANCHOR_REGEX: /^#[^ ]+$/, + OFFSET_HEIGHT_PX: 50, + + /** + * Establish events, and fix initial scroll position if a hash is provided. + */ + init: function () { + this.scrollToCurrent(); + $(window).on('hashchange', $.proxy(this, 'scrollToCurrent')); + $('body').on('click', 'a', $.proxy(this, 'delegateAnchors')); + }, + + /** + * Return the offset amount to deduct from the normal scroll position. + * Modify as appropriate to allow for dynamic calculations + */ + getFixedOffset: function () { + return this.OFFSET_HEIGHT_PX; + }, + + /** + * If the provided href is an anchor which resolves to an element on the + * page, scroll to it. + * @param {String} href + * @return {Boolean} - Was the href an anchor. + */ + scrollIfAnchor: function (href, pushToHistory) { + var match, anchorOffset; + + if (!this.ANCHOR_REGEX.test(href)) { + return false; + } + + match = document.getElementById(href.slice(1)); + + if (match) { + anchorOffset = $(match).offset().top - this.getFixedOffset(); + $('html, body').animate({ scrollTop: anchorOffset }); + + // Add the state to history as-per normal anchor links + if (HISTORY_SUPPORT && pushToHistory) { + history.pushState({}, document.title, location.pathname + href); + } + } + + return !!match; + }, + + /** + * Attempt to scroll to the current location's hash. + */ + scrollToCurrent: function (e) { + if (this.scrollIfAnchor(window.location.hash) && e) { + e.preventDefault(); + } + }, + + /** + * If the click event's target was an anchor, fix the scroll position. + */ + delegateAnchors: function (e) { + var elem = e.target; + + if (this.scrollIfAnchor(elem.getAttribute('href'), true)) { + e.preventDefault(); + } + } + }; + + $(document).ready($.proxy(anchorScrolls, 'init')); + })(window.document, window.history, window.location); + }); jQuery(window).on('load', function() {