Skip to content

Instantly share code, notes, and snippets.

@geeac
Created June 28, 2018 15:35
Show Gist options
  • Save geeac/c3c00d7564b44669aff8124909eb877e to your computer and use it in GitHub Desktop.
Save geeac/c3c00d7564b44669aff8124909eb877e to your computer and use it in GitHub Desktop.
smooth scroll
jQuery(window).load(function($){
function scrollToAnchor(hash) {
var target = $(hash), headerHeight;
if ($("#wpadminbar").length) {
headerHeight = $(".site-header").height() + $("#wpadminbar").height() + 5; // Get fixed header height
}
else headerHeight = $(".site-header").height() + 5;
target = target.length ? target : $('[name=' + hash.slice(1) +']');
if (target.length)
{
$('html,body').animate({
scrollTop: target.offset().top - headerHeight
}, 100);
return false;
}
}
if(window.location.hash) {
scrollToAnchor(window.location.hash);
}
$("a[href*=\\#]:not([href=\\#])").click(function()
{
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname)
{
scrollToAnchor(this.hash);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment