Skip to content

Instantly share code, notes, and snippets.

@ocReaper
Last active March 1, 2016 22:33
Show Gist options
  • Save ocReaper/093a0fd50920692bf829 to your computer and use it in GitHub Desktop.
Save ocReaper/093a0fd50920692bf829 to your computer and use it in GitHub Desktop.
Anchor smooth scrolling via jQuery
APP.initAnchorScroll = function () {
var anchorScroll = function (hash, event) {
var headerHeight = $('.\\@header').find('.bar').outerHeight()
, $target = $(hash);
if (hash === '') {
return;
}
$target = $target.length ? $target : $('a[name=' + hash.slice(1) + ']');
if ($target.length < 1) {
return;
}
if (event !== undefined) {
event.preventDefault();
}
$('html,body').animate({
scrollTop: $target.offset().top - headerHeight
}, 1000);
};
$('a[href*=#]:not([href=#])').unbind('click.anchorscroll').bind('click.anchorscroll', function (e) {
var href = $(this).attr('href');
anchorScroll(href.substring(href.indexOf('#')), e);
});
anchorScroll(window.location.hash);
};
$(function () {
APP.initAnchorScroll();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment