Last active
August 29, 2015 14:06
-
-
Save fijiwebdesign/e861808b9d57012eb795 to your computer and use it in GitHub Desktop.
Make all in page anchors scroll page with animation
This file contains hidden or 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
// requires jquery selector | |
$("a[href^=#]").click(function(e) { | |
var duration = $(this).data('scroll') || 750; | |
var target = $(this).attr('href'); | |
var $el = $(target); | |
if ($el.length || target == '#') { | |
e.preventDefault(); // prevent jumping to target or adding hash in url | |
// scroll to the element | |
$('html, body').animate({ | |
scrollTop: target == '#' ? 0 : $el.offset().top | |
}, duration, function() { | |
location.hash = target; // add the hash to url | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: http://jsfiddle.net/Ld2zqfn1/1/