Project

General

Profile

Download (6.35 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * jQuery.ScrollTo
3
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
4
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
5
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
6
 * Date: 2/19/2008
7
 *
8
 * @projectDescription Easy element scrolling using jQuery.
9
 * Tested with jQuery 1.2.1. On FF 2.0.0.11, IE 6, Opera 9.22 and Safari 3 beta. on Windows.
10
 *
11
 * @author Ariel Flesler
12
 * @version 1.3.3
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} settings Hash of settings, optional.
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
 * @example $('div').scrollTo( 340 );
37
 *
38
 * @example $('div').scrollTo( '+=340px', { axis:'y' } );
39
 *
40
 * @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
41
 *
42
 * @example var second_child = document.getElementById('container').firstChild.nextSibling;
43
 *			$('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){
44
 *				alert('scrolled!!');																   
45
 *			}});
46
 *
47
 * @example $('div').scrollTo( { top: 300, left:'+=200' }, { offset:-20 } );
48
 *
49
 * Notes:
50
 *  - jQuery.scrollTo will make the whole window scroll, it accepts the same arguments as jQuery.fn.scrollTo.
51
 *	- If you are interested in animated anchor navigation, check http://jquery.com/plugins/project/LocalScroll.
52
 *	- The options margin, offset and over are ignored, if the target is not a jQuery object or a DOM element.
53
 *	- The option 'queue' won't be taken into account, if only 1 axis is given.
54
 */
55
;(function( $ ){
56

    
57
	var $scrollTo = $.scrollTo = function( target, duration, settings ){
58
		$scrollTo.window().scrollTo( target, duration, settings );
59
	};
60

    
61
	$scrollTo.defaults = {
62
		axis:'y',
63
		duration:1
64
	};
65

    
66
	//returns the element that needs to be animated to scroll the window
67
	$scrollTo.window = function(){
68
		return $( $.browser.safari ? 'body' : 'html' );
69
	};
70

    
71
	$.fn.scrollTo = function( target, duration, settings ){
72
		if( typeof duration == 'object' ){
73
			settings = duration;
74
			duration = 0;
75
		}
76
		settings = $.extend( {}, $scrollTo.defaults, settings );
77
		duration = duration || settings.speed || settings.duration;//speed is still recognized for backwards compatibility
78
		settings.queue = settings.queue && settings.axis.length > 1;//make sure the settings are given right
79
		if( settings.queue )
80
			duration /= 2;//let's keep the overall speed, the same.
81
		settings.offset = both( settings.offset );
82
		settings.over = both( settings.over );
83

    
84
		return this.each(function(){
85
			var elem = this, $elem = $(elem),
86
				t = target, toff, attr = {},
87
				win = $elem.is('html,body');
88
			switch( typeof t ){
89
				case 'number'://will pass the regex
90
				case 'string':
91
					if( /^([+-]=)?\d+(px)?$/.test(t) ){
92
						t = both( t );
93
						break;//we are done
94
					}
95
					t = $(t,this);// relative selector, no break!
96
				case 'object':
97
					if( t.is || t.style )//DOM/jQuery
98
						toff = (t = $(t)).offset();//get the real position of the target 
99
			}
100
			$.each( settings.axis.split(''), function( i, axis ){
101
				var Pos	= axis == 'x' ? 'Left' : 'Top',
102
					pos = Pos.toLowerCase(),
103
					key = 'scroll' + Pos,
104
					act = elem[key],
105
					Dim = axis == 'x' ? 'Width' : 'Height',
106
					dim = Dim.toLowerCase();
107

    
108
				if( toff ){//jQuery/DOM
109
					attr[key] = toff[pos] + ( win ? 0 : act - $elem.offset()[pos] );
110

    
111
					if( settings.margin ){//if it's a dom element, reduce the margin
112
						attr[key] -= parseInt(t.css('margin'+Pos)) || 0;
113
						attr[key] -= parseInt(t.css('border'+Pos+'Width')) || 0;
114
					}
115
					
116
					attr[key] += settings.offset[pos] || 0;//add/deduct the offset
117
					
118
					if( settings.over[pos] )//scroll to a fraction of its width/height
119
						attr[key] += t[dim]() * settings.over[pos];
120
				}else
121
					attr[key] = t[pos];//remove the unnecesary 'px'
122

    
123
				if( /^\d+$/.test(attr[key]) )//number or 'number'
124
					attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max(Dim) );//check the limits
125

    
126
				if( !i && settings.queue ){//queueing each axis is required					
127
					if( act != attr[key] )//don't waste time animating, if there's no need.
128
						animate( settings.onAfterFirst );//intermediate animation
129
					delete attr[key];//don't animate this axis again in the next iteration.
130
				}
131
			});			
132
			animate( settings.onAfter );			
133

    
134
			function animate( callback ){
135
				$elem.animate( attr, duration, settings.easing, callback && function(){
136
					callback.call(this, target);
137
				});
138
			};
139
			function max( Dim ){
140
				var el = win ? $.browser.opera ? document.body : document.documentElement : elem;
141
				return el['scroll'+Dim] - el['client'+Dim];
142
			};
143
		});
144
	};
145

    
146
	function both( val ){
147
		return typeof val == 'object' ? val : { top:val, left:val };
148
	};
149

    
150
})( jQuery );
(4-4/4)