Skip to content

Instantly share code, notes, and snippets.

@icetee
Last active December 13, 2016 11:31
Show Gist options
  • Save icetee/5e202faf42ae1b88029aac0601b35e1d to your computer and use it in GitHub Desktop.
Save icetee/5e202faf42ae1b88029aac0601b35e1d to your computer and use it in GitHub Desktop.
jQuery local link scroll animation
$(document).on('click', 'a[href^="#"], a[data-scroll^="#"]', function(e) {
var id = $(this).attr('href');
if (/^#/.test(id)) {
var $id = $(id);
if ($id.length === 0) {
return;
}
} else {
id = "#" + id.split("#")[1];
}
e.preventDefault();
var $_id = $(id),
pos = $_id.offset().top;
if (typeof $_id.attr('data-scroll-offset') !== "undefined") {
pos = pos + ($_id.attr('data-scroll-offset') - 0);
}
$('body, html').animate({
scrollTop: pos
}, 800);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment