Last active
December 14, 2015 16:58
-
-
Save willbroderick/8973459 to your computer and use it in GitHub Desktop.
Javascript to scroll to in-page # hrefs, and permit external links
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
/// Scroll to in-page links | |
$(document).on('click', 'a[href^="#"]:not([href="#"]), a[href^="'+location.pathname+'#"]', function(e){ | |
var $target = $('#'+$(this).attr('href').split('#')[1]).first(); | |
if($target.length == 1) { | |
$('html:not(:animated),body:not(:animated)').animate({ | |
scrollTop: $target.offset().top | |
}, 500 ); | |
e.preventDefault(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment