Skip to content

Instantly share code, notes, and snippets.

@dkruchok
Created June 3, 2015 10:32
Show Gist options
  • Save dkruchok/459f6db7e0c05cff24aa to your computer and use it in GitHub Desktop.
Save dkruchok/459f6db7e0c05cff24aa to your computer and use it in GitHub Desktop.
Scroll to ID
/*Scroll to ID*/
// handle links with @href started with '#' only
$(document).on('click', '.pages-smaller ul li 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}, 2500);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment