Skip to content

Instantly share code, notes, and snippets.

@nateperry
Last active August 29, 2015 14:24
Show Gist options
  • Save nateperry/b6490f1369eb5e5fd421 to your computer and use it in GitHub Desktop.
Save nateperry/b6490f1369eb5e5fd421 to your computer and use it in GitHub Desktop.
Simple jQuery Scroller
/*
* Simple jQuery Scroller
* Uses jQuery to animate scrolling on click
* Simply use the ID that you want to scroll to
* as the href attribute of an <a> tag
*
* Example HTML: <a href="#main" class="scroll-to">Scroll to #main</a>
*
*/
$(document).ready(function () {
// add event listener
$('.scroll-to').on('click', function (e) {
e.preventDefault();
var id = $(e.currentTarget).attr('href')
var target = $(id);
$('html,body').animate({
scrollTop: (target.offset().top)
}, 1000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment