Skip to content

Instantly share code, notes, and snippets.

@FrankM1
Forked from billerickson/global.js
Created October 29, 2015 09:09
Show Gist options
  • Save FrankM1/b7b7e0cefebf06532100 to your computer and use it in GitHub Desktop.
Save FrankM1/b7b7e0cefebf06532100 to your computer and use it in GitHub Desktop.
// Smooth scrolling anchor links
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
// This adds an offset in case the header is fixed
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var top_offset = 0;
if ( $('.site-header').css('position') == 'fixed' ) {
top_offset = $('.site-header').height();
}
$('html,body').animate({
scrollTop: target.offset().top - top_offset
}, 1000);
return false;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment