3 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
4 * Dual licensed under MIT and GPL.
7 * @projectDescription Animated scrolling navigation, using anchors.
8 * http://flesler.blogspot.com/2007/10/jquerylocalscroll-10.html
9 * @author Ariel Flesler
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.
16 * @example $('ul.links').localScroll();
18 * @example $('ul.links').localScroll({ filter:'.animated', duration:400, axis:'x' });
20 * @example $.localScroll({ target:'#pane', axis:'xy', queue:true, event:'mouseover' });
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 * - The setting 'speed' is deprecated, use 'duration' instead.
28 * - If onBefore returns false, the event is ignored.
31 var URI
= location
.href
.replace(/#.*/,'');//local url without hash
33 var $localScroll
= $.localScroll = function( settings
){
34 $('body').localScroll( settings
);
37 //Many of these defaults, belong to jQuery.ScrollTo, check it's demo for an example of each option.
38 //@see http://www.freewebs.com/flesler/jQuery.ScrollTo/
39 $localScroll
.defaults
= {//the defaults are public and can be overriden.
40 duration
:1000, //how long to animate.
41 axis
:'y',//which of top and left should be modified.
42 event
:'click',//on which event to react.
43 stop
:true,//avoid queuing animations
44 hash
: true//if true, the hash of the selected link, will appear on the address bar.
46 lock:false,//ignore events if already animating
47 lazy:false,//if true, links can be added later, and will still work.
48 target:null, //what to scroll (selector or element). Keep it null if want to scroll the whole window.
49 filter:null, //filter some anchors out of the matched elements.
53 //if the URL contains a hash, it will scroll to the pointed element
54 $localScroll
.hash = function( settings
){
55 settings
= $.extend( {}, $localScroll
.defaults
, settings
);
56 // settings.hash = false;//can't be true
58 setTimeout(function(){ scroll( 0, location
, settings
); }, 0 );//better wrapped with a setTimeout
61 $.fn
.localScroll = function( settings
){
62 settings
= $.extend( {}, $localScroll
.defaults
, settings
);
64 return ( settings
.persistent
|| settings
.lazy
)
65 ? this.bind( settings
.event
, function( e
){//use event delegation, more links can be added later.
66 var a
= $([e
.target
, e
.target
.parentNode
]).filter(filter
)[0];//if a valid link was clicked.
67 a
&& scroll( e
, a
, settings
);//do scroll.
69 : this.find('a,area')//bind concretely, to each matching link
70 .filter( filter
).bind( settings
.event
, function(e
){
71 scroll( e
, this, settings
);
75 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.
76 return !!this.href
&& !!this.hash
&& this.href
.replace(this.hash
,'') == URI
&& (!settings
.filter
|| $(this).is( settings
.filter
));
80 function scroll( e
, link
, settings
){
81 var id
= link
.hash
.slice(1),
82 elem
= document
.getElementById(id
) || document
.getElementsByName(id
)[0];
84 e
&& e
.preventDefault();
85 var $target
= $( settings
.target
|| $.scrollTo
.window() );//if none specified, then the window.
87 if( settings
.lock
&& $target
.is(':animated') ||
88 settings
.onBefore
&& settings
.onBefore
.call(link
, e
, elem
, $target
) === false ) return;
91 $target
.queue('fx',[]).stop();//remove all its animations
93 .scrollTo( elem
, settings
)//do scroll
94 .trigger('notify.serialScroll',[elem
]);//notify serialScroll about this change
96 $target
.queue(function(){
97 location
= URI
+link
.hash
;
98 // make sure this function is released