Skip to content

Instantly share code, notes, and snippets.

@AaronLayton
Last active August 29, 2015 14:02
Show Gist options
  • Save AaronLayton/6ba2ce63fff89ac44276 to your computer and use it in GitHub Desktop.
Save AaronLayton/6ba2ce63fff89ac44276 to your computer and use it in GitHub Desktop.
/*=============================================================
e.g. <div class="scrollhere"></div>
<a data-scroll="scrollhere" href="#"></a>
TODO Detect whether class or id without indication
Allow class="js-scroll-{element}" to initiate
==============================================================*/
function scrollToModule() {
$(document).on('click', '[data-scroll]', function (e) {
e.preventDefault();
// Get all the data in one place
var elm = $($(this).data('scroll')),
scrollSpeed = parseInt($(this).data('speed')) || 300,
scrollOffset = 0;
// If we have an element to scroll to then grab the offset
if (elm.length) scrollOffset = elm.offset().top;
// Now lets scroll
$('html:not(:animated),body:not(:animated)').animate({
scrollTop: scrollOffset
}, scrollSpeed);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment