/** * jquery.scrollto * copyright (c) 2007-2008 ariel flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com * dual licensed under mit and gpl. * date: 9/11/2008 * * @projectdescription easy element scrolling using jquery. * http://flesler.blogspot.com/2007/10/jqueryscrollto.html * tested with jquery 1.2.6. on ff 2/3, ie 6/7, opera 9.2/5 and safari 3. on windows. * * @author ariel flesler * @version 1.4 * * @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,function} settings optional set of settings or the onafter callback. * @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. * * @desc scroll to a fixed position * @example $('div').scrollto( 340 ); * * @desc scroll relatively to the actual position * @example $('div').scrollto( '+=340px', { axis:'y' } ); * * @dec scroll using a selector (relative to the scrolled element) * @example $('div').scrollto( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } ); * * @ scroll to a dom element (same for jquery object) * @example var second_child = document.getelementbyid('container').firstchild.nextsibling; * $('#container').scrollto( second_child, { duration:500, axis:'x', onafter:function(){ * alert('scrolled!!'); * }}); * * @desc scroll on both axes, to different values * @example $('div').scrollto( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } ); */ ;(function( $ ){ var $scrollto = $.scrollto = function( target, duration, settings ){ $(window).scrollto( target, duration, settings ); }; $scrollto.defaults = { axis:'y', duration:1 }; // returns the element that needs to be animated to scroll the window. // kept for backwards compatibility (specially for localscroll & serialscroll) $scrollto.window = function( scope ){ return $(window).scrollable(); }; // hack, hack, hack... stay away! // returns the real elements to scroll (supports window/iframes, documents and regular nodes) $.fn.scrollable = function(){ return this.map(function(){ // just store it, we might need it var win = this.parentwindow || this.defaultview, // if it's a document, get its iframe or the window if it's the document elem = this.nodename == '#document' ? win.frameelement || win : this, // get the corresponding document doc = elem.contentdocument || (elem.contentwindow || elem).document, iswin = elem.setinterval; return elem.nodename == 'iframe' || iswin && $.support ? doc.body : iswin ? doc.documentelement : this; }); }; $.fn.scrollto = function( target, duration, settings ){ if( typeof duration == 'object' ){ settings = duration; duration = 0; } if( typeof settings == 'function' ) settings = { onafter:settings }; settings = $.extend( {}, $scrollto.defaults, settings ); // speed is still recognized for backwards compatibility duration = duration || settings.speed || settings.duration; // make sure the settings are given right settings.queue = settings.queue && settings.axis.length > 1; if( settings.queue ) // let's keep the overall duration duration /= 2; settings.offset = both( settings.offset ); settings.over = both( settings.over ); return this.scrollable().each(function(){ var elem = this, $elem = $(elem), targ = target, toff, attr = {}, win = $elem.is('html,body'); switch( typeof targ ){ // a number will pass the regex case 'number': case 'string': if( /^([+-]=)?\d+(px)?$/.test(targ) ){ targ = both( targ ); // we are done break; } // relative selector, no break! targ = $(targ,this); case 'object': // domelement / jquery if( targ.is || targ.style ) // get the real position of the target toff = (targ = $(targ)).offset(); } $.each( settings.axis.split(''), function( i, axis ){ var pos = axis == 'x' ? 'left' : 'top', pos = pos.tolowercase(), key = 'scroll' + pos, old = elem[key], dim = axis == 'x' ? 'width' : 'height', dim = dim.tolowercase(); if( toff ){// jquery / domelement attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] ); // if it's a dom element, reduce the margin if( settings.margin ){ attr[key] -= parseint(targ.css('margin'+pos)) || 0; attr[key] -= parseint(targ.css('border'+pos+'width')) || 0; } attr[key] += settings.offset[pos] || 0; if( settings.over[pos] ) // scroll to a fraction of its width/height attr[key] += targ[dim]() * settings.over[pos]; }else attr[key] = targ[pos]; // number or 'number' if( /^\d+$/.test(attr[key]) ) // check the limits attr[key] = attr[key] <= 0 ? 0 : math.min( attr[key], max(dim) ); // queueing axes if( !i && settings.queue ){ // don't waste time animating, if there's no need. if( old != attr[key] ) // intermediate animation animate( settings.onafterfirst ); // don't animate this axis again in the next iteration. delete attr[key]; } }); animate( settings.onafter ); function animate( callback ){ $elem.animate( attr, duration, settings.easing, callback && function(){ callback.call(this, target, settings); }); }; function max( dim ){ var attr ='scroll'+dim, doc = elem.ownerdocument; return win ? math.max( doc.documentelement[attr], doc.body[attr] ) : elem[attr]; }; }).end(); }; function both( val ){ return typeof val == 'object' ? val : { top:val, left:val }; }; })( jquery );