Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. $(function() {
  2.  
  3. // scroll handler
  4. var scrollToAnchor = function( id ) {
  5.  
  6. // grab the element to scroll to based on the name
  7. var elem = $("a[name='"+ id +"']");
  8.  
  9. // if that didn't work, look for an element with our ID
  10. if ( typeof( elem.offset() ) === "undefined" ) {
  11. elem = $("#"+id);
  12. }
  13.  
  14. // if the destination element exists
  15. if ( typeof( elem.offset() ) !== "undefined" ) {
  16.  
  17. // do the scroll
  18. $('html, body').animate({
  19. scrollTop: elem.offset().top
  20. }, 1000 );
  21.  
  22. }
  23. };
  24.  
  25. // bind to click event
  26. $("a").click(function( event ) {
  27.  
  28. // only do this if it's an anchor link
  29. if ( $(this).attr("href").match("#") ) {
  30.  
  31. // cancel default event propagation
  32. event.preventDefault();
  33.  
  34. // scroll to the location
  35. var href = $(this).attr('href').replace('#', '')
  36. scrollToAnchor( href );
  37.  
  38. }
  39.  
  40. });
  41.  
  42. });
  43.  
  44. $('html, body').animate({
  45. scrollTop: elem.offset().top,
  46. scrollLeft: elem.offset().left
  47. }, 1000 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement