Created
November 19, 2017 09:01
-
-
Save michael-milette/69eafc92a68d51072fbd0325f3e4b029 to your computer and use it in GitHub Desktop.
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
/** | |
* Enables smooth page scrolling for in-page links | |
* Usage: Just add this to your web page and it will automatically be applied. | |
* | |
* @Copyright 2016-2017 TNG Consulting Inc. GPLv3+ license. | |
*/ | |
jQuery( document ).ready(function() { | |
// Scroll to the in-page id. # alone takes you back to the top of the page. | |
jQuery(document).on('click', 'a[href^="#"]', function(e) { | |
// Get the target element id | |
var id = jQuery(this).attr('href'); | |
if (id === '#') { | |
return true; // Perform default action. | |
} | |
// Prevent standard hash navigation in Chrome and avoids blinking in IE. | |
e.preventDefault(); | |
// Check if the target element exists | |
var id = jQuery(id); | |
if (id.length === 0) { // No. | |
return false; // Do nothing. | |
} | |
// Scroll to id. | |
var pos = jQuery(id).offset().top - 10; | |
// Animated scrolling to id or top of page. | |
jQuery('body,html').animate({scrollTop: pos}, 1000); | |
return false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment