/** * jquery.scrollto * copyright (c) 2008 ariel flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com * dual licensed under the mit (http://www.opensource.org/licenses/mit-license.php) * and gpl (http://www.opensource.org/licenses/gpl-license.php) licenses. * date: 2/19/2008 * * @projectdescription easy element scrolling using jquery. * tested with jquery 1.2.1. on ff 2.0.0.11, ie 6, opera 9.22 and safari 3 beta. on windows. * * @author ariel flesler * @version 1.3.3 * * @id jquery.scrollto * @id jquery.fn.scrollto * @param {string, number, domelement, jquery, object} target where to scroll the matched elements. * the different options for target are: * - a number position (will be applied to all axes). * - a string position ('44', '100px', '+=90', etc ) will be applied to all axes * - a jquery/dom element ( logically, child of the element to scroll ) * - a string selector, that will be relative to the element to scroll ( 'li:eq(2)', etc ) * - a hash { top:x, left:y }, x and y can be any kind of number/string like above. * @param {number} duration the overall length of the animation, this argument can be the settings object instead. * @param {object} settings hash of settings, optional. * @option {string} axis which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'. * @option {number} duration the overall length of the animation. * @option {string} easing the easing method for the animation. * @option {boolean} margin if true, the margin of the target element will be deducted from the final position. * @option {object, number} offset add/deduct from the end position. one number for both axes or { top:x, left:y }. * @option {object, number} over add/deduct the height/width multiplied by 'over', can be { top:x, left:y } when using both axes. * @option {boolean} queue if true, and both axis are given, the 2nd axis will only be animated after the first one ends. * @option {function} onafter function to be called after the scrolling ends. * @option {function} onafterfirst if queuing is activated, this function will be called after the first scrolling ends. * @return {jquery} returns the same jquery object, for chaining. * * @example $('div').scrollto( 340 ); * * @example $('div').scrollto( '+=340px', { axis:'y' } ); * * @example $('div').scrollto( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } ); * * @example var second_child = document.getelementbyid('container').firstchild.nextsibling; * $('#container').scrollto( second_child, { duration:500, axis:'x', onafter:function(){ * alert('scrolled!!'); * }}); * * @example $('div').scrollto( { top: 300, left:'+=200' }, { offset:-20 } ); * * notes: * - jquery.scrollto will make the whole window scroll, it accepts the same arguments as jquery.fn.scrollto. * - if you are interested in animated anchor navigation, check http://jquery.com/plugins/project/localscroll. * - the options margin, offset and over are ignored, if the target is not a jquery object or a dom element. * - the option 'queue' won't be taken into account, if only 1 axis is given. */ ;(function( $ ){ var $scrollto = $.scrollto = function( target, duration, settings ){ $scrollto.window().scrollto( target, duration, settings ); }; $scrollto.defaults = { axis:'y', duration:1 }; //returns the element that needs to be animated to scroll the window $scrollto.window = function(){ return $( $.browser.safari ? 'body' : 'html' ); }; $.fn.scrollto = function( target, duration, settings ){ if( typeof duration == 'object' ){ settings = duration; duration = 0; } settings = $.extend( {}, $scrollto.defaults, settings ); duration = duration || settings.speed || settings.duration;//speed is still recognized for backwards compatibility settings.queue = settings.queue && settings.axis.length > 1;//make sure the settings are given right if( settings.queue ) duration /= 2;//let's keep the overall speed, the same. settings.offset = both( settings.offset ); settings.over = both( settings.over ); return this.each(function(){ var elem = this, $elem = $(elem), t = target, toff, attr = {}, win = $elem.is('html,body'); switch( typeof t ){ case 'number'://will pass the regex case 'string': if( /^([+-]=)?\d+(px)?$/.test(t) ){ t = both( t ); break;//we are done } t = $(t,this);// relative selector, no break! case 'object': if( t.is || t.style )//dom/jquery toff = (t = $(t)).offset();//get the real position of the target } $.each( settings.axis.split(''), function( i, axis ){ var pos = axis == 'x' ? 'left' : 'top', pos = pos.tolowercase(), key = 'scroll' + pos, act = elem[key], dim = axis == 'x' ? 'width' : 'height', dim = dim.tolowercase(); if( toff ){//jquery/dom attr[key] = toff[pos] + ( win ? 0 : act - $elem.offset()[pos] ); if( settings.margin ){//if it's a dom element, reduce the margin attr[key] -= parseint(t.css('margin'+pos)) || 0; attr[key] -= parseint(t.css('border'+pos+'width')) || 0; } attr[key] += settings.offset[pos] || 0;//add/deduct the offset if( settings.over[pos] )//scroll to a fraction of its width/height attr[key] += t[dim]() * settings.over[pos]; }else attr[key] = t[pos];//remove the unnecesary 'px' if( /^\d+$/.test(attr[key]) )//number or 'number' attr[key] = attr[key] <= 0 ? 0 : math.min( attr[key], max(dim) );//check the limits if( !i && settings.queue ){//queueing each axis is required if( act != attr[key] )//don't waste time animating, if there's no need. animate( settings.onafterfirst );//intermediate animation delete attr[key];//don't animate this axis again in the next iteration. } }); animate( settings.onafter ); function animate( callback ){ $elem.animate( attr, duration, settings.easing, callback && function(){ callback.call(this, target); }); }; function max( dim ){ var el = win ? $.browser.opera ? document.body : document.documentelement : elem; return el['scroll'+dim] - el['client'+dim]; }; }); }; function both( val ){ return typeof val == 'object' ? val : { top:val, left:val }; }; })( jquery );