Skip to content

Instantly share code, notes, and snippets.

@juanpujol
Last active April 4, 2023 18:23
Show Gist options
  • Save juanpujol/5547467 to your computer and use it in GitHub Desktop.
Save juanpujol/5547467 to your computer and use it in GitHub Desktop.
Animate scroll for anchors with href="#"
var NavBarHeight = 70 // Change this variable to controll the top of the scroll.
$('a[href*=#]').each(function() {
var target;
target = $(this).attr('href');
return $(this).on('click', function(e) {
e.preventDefault();
setToActive($(this));
// Google Analitycs event tracking.
_gaq.push(['_trackEvent', 'Links', 'Clicked', $(this).text()]);
if ($(target).length > 0) {
return $('html, body').animate({
scrollTop: $(target).offset().top - NavBarHeight
}, 800, 'swing');
}
});
});
setToActive = function(obj) {
$('.main.nav li').not('.active').removeClass('active');
obj.addClass('active');
return $('[data-spy="scroll"]').each(function() {
var $spy;
return $spy = $(this).scrollspy('refresh');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment