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
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
11 * @author Ariel Flesler
\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
36 * @desc Scroll to a fixed position
\r
37 * @example $('div').scrollTo( 340 );
\r
39 * @desc Scroll relatively to the actual position
\r
40 * @example $('div').scrollTo( '+=340px', { axis:'y' } );
\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
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
51 * @desc Scroll on both axes, to different values
\r
52 * @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } );
\r
56 var $scrollTo = $.scrollTo = function( target, duration, settings ){
\r
57 $(window).scrollTo( target, duration, settings );
\r
60 $scrollTo.defaults = {
\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
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
83 return elem.nodeName == 'IFRAME' || isWin && $.browser.safari ? doc.body
\r
84 : isWin ? doc.documentElement
\r
89 $.fn.scrollTo = function( target, duration, settings ){
\r
90 if( typeof duration == 'object' ){
\r
91 settings = duration;
\r
94 if( typeof settings == 'function' )
\r
95 settings = { onAfter:settings };
\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
103 if( settings.queue )
\r
104 // Let's keep the overall duration
\r
106 settings.offset = both( settings.offset );
\r
107 settings.over = both( settings.over );
\r
109 return this.scrollable().each(function(){
\r
112 targ = target, toff, attr = {},
\r
113 win = $elem.is('html,body');
\r
115 switch( typeof targ ){
\r
116 // A number will pass the regex
\r
119 if( /^([+-]=)?\d+(px)?$/.test(targ) ){
\r
120 targ = both( targ );
\r
124 // Relative selector, no break!
\r
125 targ = $(targ,this);
\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
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
137 Dim = axis == 'x' ? 'Width' : 'Height',
\r
138 dim = Dim.toLowerCase();
\r
140 if( toff ){// jQuery / DOMElement
\r
141 attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );
\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
149 attr[key] += settings.offset[pos] || 0;
\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
155 attr[key] = targ[pos];
\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
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
172 animate( settings.onAfter );
\r
174 function animate( callback ){
\r
175 $elem.animate( attr, duration, settings.easing, callback && function(){
\r
176 callback.call(this, target, settings);
\r
179 function max( Dim ){
\r
180 var attr ='scroll'+Dim,
\r
181 doc = elem.ownerDocument;
\r
184 ? Math.max( doc.documentElement[attr], doc.body[attr] )
\r
190 function both( val ){
\r
191 return typeof val == 'object' ? val : { top:val, left:val };
\r