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