Created
February 16, 2017 21:41
-
-
Save TwisterMc/92ff7eafe49901025ebef2ca4b46af5f to your computer and use it in GitHub Desktop.
Make jump links between pages work better when there is a fixed header.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from http://stackoverflow.com/questions/30293784/smooth-scroll-to-anchor-after-loading-new-page | |
NERD.ScrollToHashFix = { | |
init: function () { | |
// to top right away | |
if ( window.location.hash ) scroll(0,0); // void some browsers issue | |
setTimeout( function() { scroll(0,0); }, 1); | |
$(function() { | |
// *only* if we have anchor on the url and if it exists on the page. | |
if($(window.location.hash).length) { | |
var hashPos = $(window.location.hash).offset().top; | |
var headerOffset = 100; | |
var scrollPos = hashPos - headerOffset; | |
// smooth scroll to the anchor id | |
$('html, body').animate({ | |
scrollTop: scrollPos + 'px' | |
}, 1000, 'swing'); | |
} | |
}); | |
} | |
}, | |
$(window).load(function(){ // wait for the site to finish loading first. | |
NERD.ScrollToHashFix.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment