Skip to content

Instantly share code, notes, and snippets.

@marcelo-ribeiro
Last active August 10, 2016 19:55
Show Gist options
  • Save marcelo-ribeiro/967c17035fdc2643d3d5 to your computer and use it in GitHub Desktop.
Save marcelo-ribeiro/967c17035fdc2643d3d5 to your computer and use it in GitHub Desktop.
Smooth Scroll to top and to target
// ---------------------------------------------------------
// Back to Top
// ---------------------------------------------------------
$(window).scroll(function () {
if ($(this).scrollTop() > 100) {
$('#back-top').fadeIn();
} else {
$('#back-top').fadeOut();
}
});
$('#back-top a').click(function () {
$('body').animate({
scrollTop: 0
}, 800);
return false;
});
// Listen all anchors click
$('[data-href]').click(function(){
var target = $(this).attr('data-href');
$('body').animate({
scrollTop: $(target).offset().top
}, 800);
return false; // Prevent anchor to add hash on url
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment