Created
December 27, 2017 21:23
-
-
Save BruceMcKinnon/57838d7b9dbbb6f110173f65801d28c1 to your computer and use it in GitHub Desktop.
Smooth Anchor Nav Scrolling
This file contains hidden or 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
// Smooth anchor nav scrolling | |
// Handle links with @href started with '#' only | |
$(document).on('click', 'a[href^="#"]', function(e) { | |
// target element id | |
var id = $(this).attr('href'); | |
// target element | |
var $id = $(id); | |
if ($id.length === 0) { | |
return; | |
} | |
// prevent standard hash navigation (avoid blinking in IE) | |
e.preventDefault(); | |
// top position relative to the document | |
var pos = $(id).offset().top; | |
// animated top scrolling | |
$('body, html').animate({ | |
scrollTop: pos | |
}, 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment