/*! * jquery.scrollto * copyright (c) 2007-2012 ariel flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com * dual licensed under mit and gpl. * date: 4/09/2012 * * @projectdescription easy element scrolling using jquery. * http://flesler.blogspot.com/2007/10/jqueryscrollto.html * @author ariel flesler * @version 1.4.3.1 * * @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. * - a percentage of the container's dimension/s, for example: 50% to go to the middle. * - the string 'max' for go-to-end. * @param {number, function} 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, function} 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' } ); * * @desc scroll using a selector (relative to the scrolled element) * @example $('div').scrollto( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } ); * * @desc 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:'xy', duration: parsefloat($.fn.jquery) >= 1.3 ? 0 : 1, limit:true }; // 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 :) // returns the real elements to scroll (supports window/iframes, documents and regular nodes) $.fn._scrollable = function(){ return this.map(function(){ var elem = this, iswin = !elem.nodename || $.inarray( elem.nodename.tolowercase(), ['iframe','#document','html','body'] ) != -1; if( !iswin ) return elem; var doc = (elem.contentwindow || elem).document || elem.ownerdocument || elem; return /webkit/i.test(navigator.useragent) || doc.compatmode == 'backcompat' ? doc.body : doc.documentelement; }); }; $.fn.scrollto = function( target, duration, settings ){ if( typeof duration == 'object' ){ settings = duration; duration = 0; } if( typeof settings == 'function' ) settings = { onafter:settings }; if( target == 'max' ) target = 9e9; settings = $.extend( {}, $scrollto.defaults, settings ); // speed is still recognized for backwards compatibility duration = duration || 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(){ // null target yields nothing, just like jquery does if (target == null) return; 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+(\.\d+)?(px|%)?$/.test(targ) ){ targ = both( targ ); // we are done break; } // relative selector, no break! targ = $(targ,this); if (!targ.length) return; 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], max = $scrollto.max(elem, axis); 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[axis=='x'?'width':'height']() * settings.over[pos]; }else{ var val = targ[pos]; // handle percentage values attr[key] = val.slice && val.slice(-1) == '%' ? parsefloat(val) / 100 * max : val; } // number or 'number' if( settings.limit && /^\d+$/.test(attr[key]) ) // check the limits attr[key] = attr[key] <= 0 ? 0 : math.min( attr[key], max ); // 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); }); }; }).end(); }; // max scrolling position, works on quirks mode // it only fails (not too badly) on ie, quirks mode. $scrollto.max = function( elem, axis ){ var dim = axis == 'x' ? 'width' : 'height', scroll = 'scroll'+dim; if( !$(elem).is('html,body') ) return elem[scroll] - $(elem)[dim.tolowercase()](); var size = 'client' + dim, html = elem.ownerdocument.documentelement, body = elem.ownerdocument.body; return math.max( html[scroll], body[scroll] ) - math.min( html[size] , body[size] ); }; function both( val ){ return typeof val == 'object' ? val : { top:val, left:val }; }; })( jquery );