jquery.localscroll-1.2.7.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * jQuery.LocalScroll
  3. * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
  4. * Dual licensed under MIT and GPL.
  5. * Date: 3/11/2009
  6. *
  7. * @projectDescription Animated scrolling navigation, using anchors.
  8. * http://flesler.blogspot.com/2007/10/jquerylocalscroll-10.html
  9. * @author Ariel Flesler
  10. * @version 1.2.7
  11. *
  12. * @id jQuery.fn.localScroll
  13. * @param {Object} settings Hash of settings, it is passed in to jQuery.ScrollTo, none is required.
  14. * @return {jQuery} Returns the same jQuery object, for chaining.
  15. *
  16. * @example $('ul.links').localScroll();
  17. *
  18. * @example $('ul.links').localScroll({ filter:'.animated', duration:400, axis:'x' });
  19. *
  20. * @example $.localScroll({ target:'#pane', axis:'xy', queue:true, event:'mouseover' });
  21. *
  22. * Notes:
  23. * - The plugin requires jQuery.ScrollTo.
  24. * - The hash of settings, is passed to jQuery.ScrollTo, so the settings are valid for that plugin as well.
  25. * - jQuery.localScroll can be used if the desired links, are all over the document, it accepts the same settings.
  26. * - If the setting 'lazy' is set to true, then the binding will still work for later added anchors.
  27. * - If onBefore returns false, the event is ignored.
  28. **/
  29. ;(function( $ ){
  30. var URI = location.href.replace(/#.*/,''); // local url without hash
  31. var $localScroll = $.localScroll = function( settings ){
  32. $('body').localScroll( settings );
  33. };
  34. // Many of these defaults, belong to jQuery.ScrollTo, check it's demo for an example of each option.
  35. // @see http://flesler.demos.com/jquery/scrollTo/
  36. // The defaults are public and can be overriden.
  37. $localScroll.defaults = {
  38. duration:1000, // How long to animate.
  39. axis:'y', // Which of top and left should be modified.
  40. event:'click', // On which event to react.
  41. stop:true, // Avoid queuing animations
  42. target: window, // What to scroll (selector or element). The whole window by default.
  43. reset: true // Used by $.localScroll.hash. If true, elements' scroll is resetted before actual scrolling
  44. /*
  45. lock:false, // ignore events if already animating
  46. lazy:false, // if true, links can be added later, and will still work.
  47. filter:null, // filter some anchors out of the matched elements.
  48. hash: false // if true, the hash of the selected link, will appear on the address bar.
  49. */
  50. };
  51. // If the URL contains a hash, it will scroll to the pointed element
  52. $localScroll.hash = function( settings ){
  53. if( location.hash ){
  54. settings = $.extend( {}, $localScroll.defaults, settings );
  55. settings.hash = false; // can't be true
  56. if( settings.reset ){
  57. var d = settings.duration;
  58. delete settings.duration;
  59. $(settings.target).scrollTo( 0, settings );
  60. settings.duration = d;
  61. }
  62. scroll( 0, location, settings );
  63. }
  64. };
  65. $.fn.localScroll = function( settings ){
  66. settings = $.extend( {}, $localScroll.defaults, settings );
  67. return settings.lazy ?
  68. // use event delegation, more links can be added later.
  69. this.bind( settings.event, function( e ){
  70. // Could use closest(), but that would leave out jQuery -1.3.x
  71. var a = $([e.target, e.target.parentNode]).filter(filter)[0];
  72. // if a valid link was clicked
  73. if( a )
  74. scroll( e, a, settings ); // do scroll.
  75. }) :
  76. // bind concretely, to each matching link
  77. this.find('a,area')
  78. .filter( filter ).bind( settings.event, function(e){
  79. scroll( e, this, settings );
  80. }).end()
  81. .end();
  82. function filter(){// is this a link that points to an anchor and passes a possible filter ? href is checked to avoid a bug in FF.
  83. return !!this.href && !!this.hash && this.href.replace(this.hash,'') == URI && (!settings.filter || $(this).is( settings.filter ));
  84. };
  85. };
  86. function scroll( e, link, settings ){
  87. var id = link.hash.slice(1),
  88. elem = document.getElementById(id) || document.getElementsByName(id)[0];
  89. if ( !elem )
  90. return;
  91. if( e )
  92. e.preventDefault();
  93. var $target = $( settings.target );
  94. if( settings.lock && $target.is(':animated') ||
  95. settings.onBefore && settings.onBefore.call(settings, e, elem, $target) === false )
  96. return;
  97. if( settings.stop )
  98. $target.stop(true); // remove all its animations
  99. if( settings.hash ){
  100. var attr = elem.id == id ? 'id' : 'name',
  101. $a = $('<a> </a>').attr(attr, id).css({
  102. position:'absolute',
  103. top: $(window).scrollTop(),
  104. left: $(window).scrollLeft()
  105. });
  106. elem[attr] = '';
  107. $('body').prepend($a);
  108. location = link.hash;
  109. $a.remove();
  110. elem[attr] = id;
  111. }
  112. $target
  113. .scrollTo( elem, settings ) // do scroll
  114. .trigger('notify.serialScroll',[elem]); // notify serialScroll about this change
  115. };
  116. })( jQuery );