Skip to content

Instantly share code, notes, and snippets.

@kisabelle
Last active December 27, 2015 02:49
Show Gist options
  • Save kisabelle/7254696 to your computer and use it in GitHub Desktop.
Save kisabelle/7254696 to your computer and use it in GitHub Desktop.
jQuery Scroll to Element with Easing / Smooth Scrolling, Reveal "Back to Top" link on scroll down
// method 1
$(function() {
$('nav a').bind('click',function(event){
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1500,'easeInOutExpo');
event.preventDefault();
});
});
// method 2
$("article a[href^='#']").on('click', function(e) {
console.log('triggered');
// prevent default anchor click behavior
e.preventDefault();
// store hash
var hash = this.hash;
// animate
$('html, body').animate({
scrollTop: $(this.hash).offset().top
}, 600, function(){
// when done, add hash to url
// (default click behaviour)
window.location.hash = hash;
});
});
$(window).scroll(function() {
var offset = 100;
if ($(this).scrollTop() > offset) {
$('#toTop:hidden').stop(true, true).fadeIn();
} else {
$('#toTop').stop(true, true).fadeOut();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment