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