Last active
December 13, 2016 11:31
-
-
Save icetee/5e202faf42ae1b88029aac0601b35e1d to your computer and use it in GitHub Desktop.
jQuery local link scroll animation
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
$(document).on('click', 'a[href^="#"], a[data-scroll^="#"]', function(e) { | |
var id = $(this).attr('href'); | |
if (/^#/.test(id)) { | |
var $id = $(id); | |
if ($id.length === 0) { | |
return; | |
} | |
} else { | |
id = "#" + id.split("#")[1]; | |
} | |
e.preventDefault(); | |
var $_id = $(id), | |
pos = $_id.offset().top; | |
if (typeof $_id.attr('data-scroll-offset') !== "undefined") { | |
pos = pos + ($_id.attr('data-scroll-offset') - 0); | |
} | |
$('body, html').animate({ | |
scrollTop: pos | |
}, 800); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment