Skip to content

Instantly share code, notes, and snippets.

@tsvensen
Created March 12, 2012 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsvensen/2023290 to your computer and use it in GitHub Desktop.
Save tsvensen/2023290 to your computer and use it in GitHub Desktop.
jQuery ScrollTo
// ScrollTo top of an element
$('html, body').animate({scrollTop: $('#my-element').offset().top}, 600, 'linear');
// ScrollTo and center the element
$('html, body').animate({
scrollTop: parseInt($('#my-element').offset().top) - ($(window).height() / 2) + ($('#my-element').height() / 2),
scrollLeft: parseInt($('#my-element').offset().left) - ($(window).width() / 2) + ($('#my-element').width() / 2)
}, 600, 'linear');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment