Statistiques
| Révision :

root / tei / js / jquery.scrollTo.js @ 2

Historique | Voir | Annoter | Télécharger (7,44 ko)

1 2 mingarao
/**
2 2 mingarao
 * jQuery.ScrollTo
3 2 mingarao
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
4 2 mingarao
 * Dual licensed under MIT and GPL.
5 2 mingarao
 * Date: 3/9/2009
6 2 mingarao
 *
7 2 mingarao
 * @projectDescription Easy element scrolling using jQuery.
8 2 mingarao
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
9 2 mingarao
 * Works with jQuery +1.2.6. Tested on FF 2/3, IE 6/7/8, Opera 9.5/6, Safari 3, Chrome 1 on WinXP.
10 2 mingarao
 *
11 2 mingarao
 * @author Ariel Flesler
12 2 mingarao
 * @version 1.4.1
13 2 mingarao
 *
14 2 mingarao
 * @id jQuery.scrollTo
15 2 mingarao
 * @id jQuery.fn.scrollTo
16 2 mingarao
 * @param {String, Number, DOMElement, jQuery, Object} target Where to scroll the matched elements.
17 2 mingarao
 *          The different options for target are:
18 2 mingarao
 *                - A number position (will be applied to all axes).
19 2 mingarao
 *                - A string position ('44', '100px', '+=90', etc ) will be applied to all axes
20 2 mingarao
 *                - A jQuery/DOM element ( logically, child of the element to scroll )
21 2 mingarao
 *                - A string selector, that will be relative to the element to scroll ( 'li:eq(2)', etc )
22 2 mingarao
 *                - A hash { top:x, left:y }, x and y can be any kind of number/string like above.
23 2 mingarao
 *                - The string 'max' for go-to-end.
24 2 mingarao
 * @param {Number} duration The OVERALL length of the animation, this argument can be the settings object instead.
25 2 mingarao
 * @param {Object,Function} settings Optional set of settings or the onAfter callback.
26 2 mingarao
 *         @option {String} axis Which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'.
27 2 mingarao
 *         @option {Number} duration The OVERALL length of the animation.
28 2 mingarao
 *         @option {String} easing The easing method for the animation.
29 2 mingarao
 *         @option {Boolean} margin If true, the margin of the target element will be deducted from the final position.
30 2 mingarao
 *         @option {Object, Number} offset Add/deduct from the end position. One number for both axes or { top:x, left:y }.
31 2 mingarao
 *         @option {Object, Number} over Add/deduct the height/width multiplied by 'over', can be { top:x, left:y } when using both axes.
32 2 mingarao
 *         @option {Boolean} queue If true, and both axis are given, the 2nd axis will only be animated after the first one ends.
33 2 mingarao
 *         @option {Function} onAfter Function to be called after the scrolling ends.
34 2 mingarao
 *         @option {Function} onAfterFirst If queuing is activated, this function will be called after the first scrolling ends.
35 2 mingarao
 * @return {jQuery} Returns the same jQuery object, for chaining.
36 2 mingarao
 *
37 2 mingarao
 * @desc Scroll to a fixed position
38 2 mingarao
 * @example $('div').scrollTo( 340 );
39 2 mingarao
 *
40 2 mingarao
 * @desc Scroll relatively to the actual position
41 2 mingarao
 * @example $('div').scrollTo( '+=340px', { axis:'y' } );
42 2 mingarao
 *
43 2 mingarao
 * @dec Scroll using a selector (relative to the scrolled element)
44 2 mingarao
 * @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
45 2 mingarao
 *
46 2 mingarao
 * @ Scroll to a DOM element (same for jQuery object)
47 2 mingarao
 * @example var second_child = document.getElementById('container').firstChild.nextSibling;
48 2 mingarao
 *                        $('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){
49 2 mingarao
 *                                alert('scrolled!!');
50 2 mingarao
 *                        }});
51 2 mingarao
 *
52 2 mingarao
 * @desc Scroll on both axes, to different values
53 2 mingarao
 * @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } );
54 2 mingarao
 */
55 2 mingarao
;(function( $ ){
56 2 mingarao
57 2 mingarao
        var $scrollTo = $.scrollTo = function( target, duration, settings ){
58 2 mingarao
                $(window).scrollTo( target, duration, settings );
59 2 mingarao
        };
60 2 mingarao
61 2 mingarao
        $scrollTo.defaults = {
62 2 mingarao
                axis:'xy',
63 2 mingarao
                duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1
64 2 mingarao
        };
65 2 mingarao
66 2 mingarao
        // Returns the element that needs to be animated to scroll the window.
67 2 mingarao
        // Kept for backwards compatibility (specially for localScroll & serialScroll)
68 2 mingarao
        $scrollTo.window = function( scope ){
69 2 mingarao
                return $(window).scrollable();
70 2 mingarao
        };
71 2 mingarao
72 2 mingarao
        // Hack, hack, hack... stay away!
73 2 mingarao
        // Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
74 2 mingarao
        $.fn.scrollable = function(){
75 2 mingarao
                return this.map(function(){
76 2 mingarao
                        var elem = this,
77 2 mingarao
                                isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;
78 2 mingarao
79 2 mingarao
                                if( !isWin )
80 2 mingarao
                                        return elem;
81 2 mingarao
82 2 mingarao
                        var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
83 2 mingarao
84 2 mingarao
                        return $.browser.safari || doc.compatMode == 'BackCompat' ?
85 2 mingarao
                                doc.body :
86 2 mingarao
                                doc.documentElement;
87 2 mingarao
                });
88 2 mingarao
        };
89 2 mingarao
90 2 mingarao
        $.fn.scrollTo = function( target, duration, settings ){
91 2 mingarao
                if( typeof duration == 'object' ){
92 2 mingarao
                        settings = duration;
93 2 mingarao
                        duration = 0;
94 2 mingarao
                }
95 2 mingarao
                if( typeof settings == 'function' )
96 2 mingarao
                        settings = { onAfter:settings };
97 2 mingarao
98 2 mingarao
                if( target == 'max' )
99 2 mingarao
                        target = 9e9;
100 2 mingarao
101 2 mingarao
                settings = $.extend( {}, $scrollTo.defaults, settings );
102 2 mingarao
                // Speed is still recognized for backwards compatibility
103 2 mingarao
                duration = duration || settings.speed || settings.duration;
104 2 mingarao
                // Make sure the settings are given right
105 2 mingarao
                settings.queue = settings.queue && settings.axis.length > 1;
106 2 mingarao
107 2 mingarao
                if( settings.queue )
108 2 mingarao
                        // Let's keep the overall duration
109 2 mingarao
                        duration /= 2;
110 2 mingarao
                settings.offset = both( settings.offset );
111 2 mingarao
                settings.over = both( settings.over );
112 2 mingarao
113 2 mingarao
                return this.scrollable().each(function(){
114 2 mingarao
                        var elem = this,
115 2 mingarao
                                $elem = $(elem),
116 2 mingarao
                                targ = target, toff, attr = {},
117 2 mingarao
                                win = $elem.is('html,body');
118 2 mingarao
119 2 mingarao
                        switch( typeof targ ){
120 2 mingarao
                                // A number will pass the regex
121 2 mingarao
                                case 'number':
122 2 mingarao
                                case 'string':
123 2 mingarao
                                        if( /^([+-]=)?\d+(\.\d+)?(px)?$/.test(targ) ){
124 2 mingarao
                                                targ = both( targ );
125 2 mingarao
                                                // We are done
126 2 mingarao
                                                break;
127 2 mingarao
                                        }
128 2 mingarao
                                        // Relative selector, no break!
129 2 mingarao
                                        targ = $(targ,this);
130 2 mingarao
                                case 'object':
131 2 mingarao
                                        // DOMElement / jQuery
132 2 mingarao
                                        if( targ.is || targ.style )
133 2 mingarao
                                                // Get the real position of the target
134 2 mingarao
                                                toff = (targ = $(targ)).offset();
135 2 mingarao
                        }
136 2 mingarao
                        $.each( settings.axis.split(''), function( i, axis ){
137 2 mingarao
                                var Pos        = axis == 'x' ? 'Left' : 'Top',
138 2 mingarao
                                        pos = Pos.toLowerCase(),
139 2 mingarao
                                        key = 'scroll' + Pos,
140 2 mingarao
                                        old = elem[key],
141 2 mingarao
                                        Dim = axis == 'x' ? 'Width' : 'Height';
142 2 mingarao
143 2 mingarao
                                if( toff ){// jQuery / DOMElement
144 2 mingarao
                                        attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );
145 2 mingarao
146 2 mingarao
                                        // If it's a dom element, reduce the margin
147 2 mingarao
                                        if( settings.margin ){
148 2 mingarao
                                                attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
149 2 mingarao
                                                attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
150 2 mingarao
                                        }
151 2 mingarao
152 2 mingarao
                                        attr[key] += settings.offset[pos] || 0;
153 2 mingarao
154 2 mingarao
                                        if( settings.over[pos] )
155 2 mingarao
                                                // Scroll to a fraction of its width/height
156 2 mingarao
                                                attr[key] += targ[Dim.toLowerCase()]() * settings.over[pos];
157 2 mingarao
                                }else
158 2 mingarao
                                        attr[key] = targ[pos];
159 2 mingarao
160 2 mingarao
                                // Number or 'number'
161 2 mingarao
                                if( /^\d+$/.test(attr[key]) )
162 2 mingarao
                                        // Check the limits
163 2 mingarao
                                        attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max(Dim) );
164 2 mingarao
165 2 mingarao
                                // Queueing axes
166 2 mingarao
                                if( !i && settings.queue ){
167 2 mingarao
                                        // Don't waste time animating, if there's no need.
168 2 mingarao
                                        if( old != attr[key] )
169 2 mingarao
                                                // Intermediate animation
170 2 mingarao
                                                animate( settings.onAfterFirst );
171 2 mingarao
                                        // Don't animate this axis again in the next iteration.
172 2 mingarao
                                        delete attr[key];
173 2 mingarao
                                }
174 2 mingarao
                        });
175 2 mingarao
176 2 mingarao
                        animate( settings.onAfter );
177 2 mingarao
178 2 mingarao
                        function animate( callback ){
179 2 mingarao
                                $elem.animate( attr, duration, settings.easing, callback && function(){
180 2 mingarao
                                        callback.call(this, target, settings);
181 2 mingarao
                                });
182 2 mingarao
                        };
183 2 mingarao
184 2 mingarao
                        // Max scrolling position, works on quirks mode
185 2 mingarao
                        // It only fails (not too badly) on IE, quirks mode.
186 2 mingarao
                        function max( Dim ){
187 2 mingarao
                                var scroll = 'scroll'+Dim;
188 2 mingarao
189 2 mingarao
                                if( !win )
190 2 mingarao
                                        return elem[scroll];
191 2 mingarao
192 2 mingarao
                                var size = 'client' + Dim,
193 2 mingarao
                                        html = elem.ownerDocument.documentElement,
194 2 mingarao
                                        body = elem.ownerDocument.body;
195 2 mingarao
196 2 mingarao
                                return Math.max( html[scroll], body[scroll] )
197 2 mingarao
                                         - Math.min( html[size]  , body[size]   );
198 2 mingarao
199 2 mingarao
                        };
200 2 mingarao
201 2 mingarao
                }).end();
202 2 mingarao
        };
203 2 mingarao
204 2 mingarao
        function both( val ){
205 2 mingarao
                return typeof val == 'object' ? val : { top:val, left:val };
206 2 mingarao
        };
207 2 mingarao
208 2 mingarao
})( jQuery );