Posted By

davebowker on 01/14/10


Tagged

scroll jquery anchor


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

davebowker
Kerrick
vagrantradio
frysteen


jQuery Scroll to Anchor


 / Published in: jQuery
 

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

  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. });

Report this snippet  

You need to login to post a comment.