[SPIP] ~maj v2.1.25-->2.1.26
[velocampus/web/www.git] / www / plugins / auto / couteau_suisse / couteau_suisse / outils / jquery.localscroll.js
1 /**
2 * jQuery.LocalScroll
3 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
4 * Dual licensed under MIT and GPL.
5 * Date: 6/3/2008
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.6
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 * - The setting 'speed' is deprecated, use 'duration' instead.
28 * - If onBefore returns false, the event is ignored.
29 **/
30 ;(function( $ ){
31 var URI = location.href.replace(/#.*/,'');//local url without hash
32
33 var $localScroll = $.localScroll = function( settings ){
34 $('body').localScroll( settings );
35 };
36
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.
45 /*
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.
50 */
51 };
52
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
57 if( location.hash )
58 setTimeout(function(){ scroll( 0, location, settings ); }, 0 );//better wrapped with a setTimeout
59 };
60
61 $.fn.localScroll = function( settings ){
62 settings = $.extend( {}, $localScroll.defaults, settings );
63
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.
68 })
69 : this.find('a,area')//bind concretely, to each matching link
70 .filter( filter ).bind( settings.event, function(e){
71 scroll( e, this, settings );
72 }).end()
73 .end();
74
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 ));
77 };
78 };
79
80 function scroll( e, link, settings ){
81 var id = link.hash.slice(1),
82 elem = document.getElementById(id) || document.getElementsByName(id)[0];
83 if ( elem ){
84 e && e.preventDefault();
85 var $target = $( settings.target || $.scrollTo.window() );//if none specified, then the window.
86
87 if( settings.lock && $target.is(':animated') ||
88 settings.onBefore && settings.onBefore.call(link, e, elem, $target) === false ) return;
89
90 if( settings.stop )
91 $target.queue('fx',[]).stop();//remove all its animations
92 $target
93 .scrollTo( elem, settings )//do scroll
94 .trigger('notify.serialScroll',[elem]);//notify serialScroll about this change
95 if( settings.hash )
96 $target.queue(function(){
97 location = URI+link.hash;
98 // make sure this function is released
99 $(this).dequeue();
100 });
101 }
102 };
103
104 })( jQuery );