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