Pages

Monday 3 March 2014

jQuery – Scroll page to position of an element/div

When you’re working with a complex page that has a load of areas on-screen, making use of this small function below to scroll to the location will enhance you’re user experience by saving them needing to scroll… take them straight to it!

 
 
1
2
3
4
5
6
// This is a functions that scrolls to #id
function scrollTo(id)
{
  // Scroll
  $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}


Example usage:
1
2
3
$('#thing').click(function(e){
  scrollTo("divid");
});

No comments:

Post a Comment