jQuery Scroll to Anchor


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $('a[href*="#"]').click(function(event){
  2. //prevent the default action for the click event
  3. event.preventDefault();
  4.  
  5. //get the full url - like mysitecom/index.htm#home
  6. var full_url = this.href;
  7.  
  8. //split the url by # and get the anchor target name - home in mysitecom/index.htm#home
  9. var parts = full_url.split("#");
  10. var trgt = parts[1];
  11.  
  12. //get the top offset of the target anchor
  13. var target_offset = $("#"+trgt).offset();
  14. var target_top = target_offset.top;
  15.  
  16. //goto that anchor by setting the body scroll top to anchor top
  17. $('html, body').animate({scrollTop:target_top}, 500);
  18. });

URL: http://beski.wordpress.com/2009/04/21/scroll-effect-with-local-anchors-jquery/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.