Skip to content

Instantly share code, notes, and snippets.

@apajak
Last active August 29, 2015 13:57
Show Gist options
  • Save apajak/9649257 to your computer and use it in GitHub Desktop.
Save apajak/9649257 to your computer and use it in GitHub Desktop.
Creates a smooth scrolling. Based off http://tinyurl.com/smoothscrollingjs. Added features: Use the smooth scroll for only specific anchor tags, and keeps the #anchor name for future linking purposes.
var jump=function(e){
//prevent the "normal" behaviour which would be a "hard" jump
e.preventDefault();
//Get the target
var target = $(this).attr("href");
//perform animated scrolling
if(target =='#home' || target=='#about'|| target=='#portfolio' || target=='#contact'){
$('html,body').animate(
{
//get top-position of target-element and set it as scroll target
scrollTop: $(target).offset().top
//scrolldelay: 2 seconds
},1000,function()
{
//attach the hash (#jumptarget) to the pageurl
location.hash = target;
});
}else{
}
}
$(document).ready(function()
{
$('a[href*=#]').bind("click", jump);
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment