/** | |
* 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