Skip to content

Instantly share code, notes, and snippets.

@simonpioli
Last active December 15, 2015 04:39
Show Gist options
  • Save simonpioli/5203485 to your computer and use it in GitHub Desktop.
Save simonpioli/5203485 to your computer and use it in GitHub Desktop.
Animate the scroll for links to areas of the same page. Requires jQuery.
/** This version only requires jQuery **/
$("a[href*='#']").click(function(event) {
event.preventDefault();
var full_url = this.href;
if(full_url !== '#') {
var parts = full_url.split("#");
var trgt = parts[1];
var target_offset = $("#" + trgt).offset();
var target_top = target_offset.top;
$('html, body').animate({
scrollTop: target_top
}, 400);
} else {
return false;
}
});
/** This version requires scrollTo jQuery Plugin **/
$("a[href*='#']").click(function() {
target = this.href;
if(target !== '#') {
$.scrollTo($(target), 600);
}
else {
return false;
}
});
@simonpioli
Copy link
Author

I want to change the selector it binds to so you don't have to add a class to the link each time.

@simonpioli
Copy link
Author

** Updated with generic selector and version for use with scrollTo plugin **

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment