Blame jquery/jquery.scrollTo-1.4.2.js

Packit 1c1d7e
/**
Packit 1c1d7e
 * jQuery.ScrollTo
Packit 1c1d7e
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
Packit 1c1d7e
 * Dual licensed under MIT and GPL.
Packit 1c1d7e
 * Date: 5/25/2009
Packit 1c1d7e
 *
Packit 1c1d7e
 * @projectDescription Easy element scrolling using jQuery.
Packit 1c1d7e
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
Packit 1c1d7e
 * Works with jQuery +1.2.6. Tested on FF 2/3, IE 6/7/8, Opera 9.5/6, Safari 3, Chrome 1 on WinXP.
Packit 1c1d7e
 *
Packit 1c1d7e
 * @author Ariel Flesler
Packit 1c1d7e
 * @version 1.4.2
Packit 1c1d7e
 *
Packit 1c1d7e
 * @id jQuery.scrollTo
Packit 1c1d7e
 * @id jQuery.fn.scrollTo
Packit 1c1d7e
 * @param {String, Number, DOMElement, jQuery, Object} target Where to scroll the matched elements.
Packit 1c1d7e
 *	  The different options for target are:
Packit 1c1d7e
 *		- A number position (will be applied to all axes).
Packit 1c1d7e
 *		- A string position ('44', '100px', '+=90', etc ) will be applied to all axes
Packit 1c1d7e
 *		- A jQuery/DOM element ( logically, child of the element to scroll )
Packit 1c1d7e
 *		- A string selector, that will be relative to the element to scroll ( 'li:eq(2)', etc )
Packit 1c1d7e
 *		- A hash { top:x, left:y }, x and y can be any kind of number/string like above.
Packit 1c1d7e
*		- A percentage of the container's dimension/s, for example: 50% to go to the middle.
Packit 1c1d7e
 *		- The string 'max' for go-to-end. 
Packit 1c1d7e
 * @param {Number} duration The OVERALL length of the animation, this argument can be the settings object instead.
Packit 1c1d7e
 * @param {Object,Function} settings Optional set of settings or the onAfter callback.
Packit 1c1d7e
 *	 @option {String} axis Which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'.
Packit 1c1d7e
 *	 @option {Number} duration The OVERALL length of the animation.
Packit 1c1d7e
 *	 @option {String} easing The easing method for the animation.
Packit 1c1d7e
 *	 @option {Boolean} margin If true, the margin of the target element will be deducted from the final position.
Packit 1c1d7e
 *	 @option {Object, Number} offset Add/deduct from the end position. One number for both axes or { top:x, left:y }.
Packit 1c1d7e
 *	 @option {Object, Number} over Add/deduct the height/width multiplied by 'over', can be { top:x, left:y } when using both axes.
Packit 1c1d7e
 *	 @option {Boolean} queue If true, and both axis are given, the 2nd axis will only be animated after the first one ends.
Packit 1c1d7e
 *	 @option {Function} onAfter Function to be called after the scrolling ends. 
Packit 1c1d7e
 *	 @option {Function} onAfterFirst If queuing is activated, this function will be called after the first scrolling ends.
Packit 1c1d7e
 * @return {jQuery} Returns the same jQuery object, for chaining.
Packit 1c1d7e
 *
Packit 1c1d7e
 * @desc Scroll to a fixed position
Packit 1c1d7e
 * @example $('div').scrollTo( 340 );
Packit 1c1d7e
 *
Packit 1c1d7e
 * @desc Scroll relatively to the actual position
Packit 1c1d7e
 * @example $('div').scrollTo( '+=340px', { axis:'y' } );
Packit 1c1d7e
 *
Packit 1c1d7e
 * @dec Scroll using a selector (relative to the scrolled element)
Packit 1c1d7e
 * @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
Packit 1c1d7e
 *
Packit 1c1d7e
 * @ Scroll to a DOM element (same for jQuery object)
Packit 1c1d7e
 * @example var second_child = document.getElementById('container').firstChild.nextSibling;
Packit 1c1d7e
 *			$('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){
Packit 1c1d7e
 *				alert('scrolled!!');																   
Packit 1c1d7e
 *			}});
Packit 1c1d7e
 *
Packit 1c1d7e
 * @desc Scroll on both axes, to different values
Packit 1c1d7e
 * @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } );
Packit 1c1d7e
 */
Packit 1c1d7e
;(function( $ ){
Packit 1c1d7e
	
Packit 1c1d7e
	var $scrollTo = $.scrollTo = function( target, duration, settings ){
Packit 1c1d7e
		$(window).scrollTo( target, duration, settings );
Packit 1c1d7e
	};
Packit 1c1d7e

Packit 1c1d7e
	$scrollTo.defaults = {
Packit 1c1d7e
		axis:'xy',
Packit 1c1d7e
		duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1
Packit 1c1d7e
	};
Packit 1c1d7e

Packit 1c1d7e
	// Returns the element that needs to be animated to scroll the window.
Packit 1c1d7e
	// Kept for backwards compatibility (specially for localScroll & serialScroll)
Packit 1c1d7e
	$scrollTo.window = function( scope ){
Packit 1c1d7e
		return $(window)._scrollable();
Packit 1c1d7e
	};
Packit 1c1d7e

Packit 1c1d7e
	// Hack, hack, hack :)
Packit 1c1d7e
	// Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
Packit 1c1d7e
	$.fn._scrollable = function(){
Packit 1c1d7e
		return this.map(function(){
Packit 1c1d7e
			var elem = this,
Packit 1c1d7e
				isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;
Packit 1c1d7e

Packit 1c1d7e
				if( !isWin )
Packit 1c1d7e
					return elem;
Packit 1c1d7e

Packit 1c1d7e
			var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
Packit 1c1d7e
			
Packit 1c1d7e
			return $.browser.safari || doc.compatMode == 'BackCompat' ?
Packit 1c1d7e
				doc.body : 
Packit 1c1d7e
				doc.documentElement;
Packit 1c1d7e
		});
Packit 1c1d7e
	};
Packit 1c1d7e

Packit 1c1d7e
	$.fn.scrollTo = function( target, duration, settings ){
Packit 1c1d7e
		if( typeof duration == 'object' ){
Packit 1c1d7e
			settings = duration;
Packit 1c1d7e
			duration = 0;
Packit 1c1d7e
		}
Packit 1c1d7e
		if( typeof settings == 'function' )
Packit 1c1d7e
			settings = { onAfter:settings };
Packit 1c1d7e
			
Packit 1c1d7e
		if( target == 'max' )
Packit 1c1d7e
			target = 9e9;
Packit 1c1d7e
			
Packit 1c1d7e
		settings = $.extend( {}, $scrollTo.defaults, settings );
Packit 1c1d7e
		// Speed is still recognized for backwards compatibility
Packit 1c1d7e
		duration = duration || settings.speed || settings.duration;
Packit 1c1d7e
		// Make sure the settings are given right
Packit 1c1d7e
		settings.queue = settings.queue && settings.axis.length > 1;
Packit 1c1d7e
		
Packit 1c1d7e
		if( settings.queue )
Packit 1c1d7e
			// Let's keep the overall duration
Packit 1c1d7e
			duration /= 2;
Packit 1c1d7e
		settings.offset = both( settings.offset );
Packit 1c1d7e
		settings.over = both( settings.over );
Packit 1c1d7e

Packit 1c1d7e
		return this._scrollable().each(function(){
Packit 1c1d7e
			var elem = this,
Packit 1c1d7e
				$elem = $(elem),
Packit 1c1d7e
				targ = target, toff, attr = {},
Packit 1c1d7e
				win = $elem.is('html,body');
Packit 1c1d7e

Packit 1c1d7e
			switch( typeof targ ){
Packit 1c1d7e
				// A number will pass the regex
Packit 1c1d7e
				case 'number':
Packit 1c1d7e
				case 'string':
Packit 1c1d7e
					if( /^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(targ) ){
Packit 1c1d7e
						targ = both( targ );
Packit 1c1d7e
						// We are done
Packit 1c1d7e
						break;
Packit 1c1d7e
					}
Packit 1c1d7e
					// Relative selector, no break!
Packit 1c1d7e
					targ = $(targ,this);
Packit 1c1d7e
				case 'object':
Packit 1c1d7e
					// DOMElement / jQuery
Packit 1c1d7e
					if( targ.is || targ.style )
Packit 1c1d7e
						// Get the real position of the target 
Packit 1c1d7e
						toff = (targ = $(targ)).offset();
Packit 1c1d7e
			}
Packit 1c1d7e
			$.each( settings.axis.split(''), function( i, axis ){
Packit 1c1d7e
				var Pos	= axis == 'x' ? 'Left' : 'Top',
Packit 1c1d7e
					pos = Pos.toLowerCase(),
Packit 1c1d7e
					key = 'scroll' + Pos,
Packit 1c1d7e
					old = elem[key],
Packit 1c1d7e
					max = $scrollTo.max(elem, axis);
Packit 1c1d7e

Packit 1c1d7e
				if( toff ){// jQuery / DOMElement
Packit 1c1d7e
					attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );
Packit 1c1d7e

Packit 1c1d7e
					// If it's a dom element, reduce the margin
Packit 1c1d7e
					if( settings.margin ){
Packit 1c1d7e
						attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
Packit 1c1d7e
						attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
Packit 1c1d7e
					}
Packit 1c1d7e
					
Packit 1c1d7e
					attr[key] += settings.offset[pos] || 0;
Packit 1c1d7e
					
Packit 1c1d7e
					if( settings.over[pos] )
Packit 1c1d7e
						// Scroll to a fraction of its width/height
Packit 1c1d7e
						attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos];
Packit 1c1d7e
				}else{ 
Packit 1c1d7e
					var val = targ[pos];
Packit 1c1d7e
					// Handle percentage values
Packit 1c1d7e
					attr[key] = val.slice && val.slice(-1) == '%' ? 
Packit 1c1d7e
						parseFloat(val) / 100 * max
Packit 1c1d7e
						: val;
Packit 1c1d7e
				}
Packit 1c1d7e

Packit 1c1d7e
				// Number or 'number'
Packit 1c1d7e
				if( /^\d+$/.test(attr[key]) )
Packit 1c1d7e
					// Check the limits
Packit 1c1d7e
					attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );
Packit 1c1d7e

Packit 1c1d7e
				// Queueing axes
Packit 1c1d7e
				if( !i && settings.queue ){
Packit 1c1d7e
					// Don't waste time animating, if there's no need.
Packit 1c1d7e
					if( old != attr[key] )
Packit 1c1d7e
						// Intermediate animation
Packit 1c1d7e
						animate( settings.onAfterFirst );
Packit 1c1d7e
					// Don't animate this axis again in the next iteration.
Packit 1c1d7e
					delete attr[key];
Packit 1c1d7e
				}
Packit 1c1d7e
			});
Packit 1c1d7e

Packit 1c1d7e
			animate( settings.onAfter );			
Packit 1c1d7e

Packit 1c1d7e
			function animate( callback ){
Packit 1c1d7e
				$elem.animate( attr, duration, settings.easing, callback && function(){
Packit 1c1d7e
					callback.call(this, target, settings);
Packit 1c1d7e
				});
Packit 1c1d7e
			};
Packit 1c1d7e

Packit 1c1d7e
		}).end();
Packit 1c1d7e
	};
Packit 1c1d7e
	
Packit 1c1d7e
	// Max scrolling position, works on quirks mode
Packit 1c1d7e
	// It only fails (not too badly) on IE, quirks mode.
Packit 1c1d7e
	$scrollTo.max = function( elem, axis ){
Packit 1c1d7e
		var Dim = axis == 'x' ? 'Width' : 'Height',
Packit 1c1d7e
			scroll = 'scroll'+Dim;
Packit 1c1d7e
		
Packit 1c1d7e
		if( !$(elem).is('html,body') )
Packit 1c1d7e
			return elem[scroll] - $(elem)[Dim.toLowerCase()]();
Packit 1c1d7e
		
Packit 1c1d7e
		var size = 'client' + Dim,
Packit 1c1d7e
			html = elem.ownerDocument.documentElement,
Packit 1c1d7e
			body = elem.ownerDocument.body;
Packit 1c1d7e

Packit 1c1d7e
		return Math.max( html[scroll], body[scroll] ) 
Packit 1c1d7e
			 - Math.min( html[size]  , body[size]   );
Packit 1c1d7e
			
Packit 1c1d7e
	};
Packit 1c1d7e

Packit 1c1d7e
	function both( val ){
Packit 1c1d7e
		return typeof val == 'object' ? val : { top:val, left:val };
Packit 1c1d7e
	};
Packit 1c1d7e

Packit 1c1d7e
})( jQuery );