Created
July 4, 2013 09:35
-
-
Save dommmel/5926298 to your computer and use it in GitHub Desktop.
jQuery smooth scroll to anchor
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
// use it like this: <a class="scroll" href="#destination1">Destination 1</a> | |
// <section id="destination1">Lore ipsum</section> | |
$(".scroll").click(function(event){ | |
event.preventDefault(); | |
//calculate destination place | |
var dest=0; | |
if($(this.hash).offset().top > $(document).height()-$(window).height()){ | |
dest=$(document).height()-$(window).height(); | |
}else{ | |
dest=$(this.hash).offset().top; | |
} | |
//go to destination | |
$('html,body').animate({scrollTop:dest}, 1000,'swing'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment