Skip to content

Instantly share code, notes, and snippets.

@dommmel
Created July 4, 2013 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dommmel/5926298 to your computer and use it in GitHub Desktop.
Save dommmel/5926298 to your computer and use it in GitHub Desktop.
jQuery smooth scroll to anchor
// 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