Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Smooth scrolling anchor links
function ea_scroll( hash ) {
var target = $( 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();
}
if( $('body').hasClass('admin-bar') ) {
top_offset = top_offset + $('#wpadminbar').height();
}
$('html,body').animate({
scrollTop: target.offset().top - top_offset
}, 1000);
return false;
}
}
// -- Smooth scroll on pageload
if( window.location.hash ) {
ea_scroll( window.location.hash );
}
// -- Smooth scroll on click
$('a[href*="#"]:not([href="#"]):not(.no-scroll)').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) {
ea_scroll( this.hash );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.