Last active
December 27, 2015 02:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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