Last active
December 15, 2015 04:39
-
-
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 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
/** 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; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
** Updated with generic selector and version for use with scrollTo plugin **