1/** 2 * jQuery.ScrollTo 3 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com 4 * Dual licensed under MIT and GPL. 5 * Date: 3/9/2009 6 * 7 * @projectDescription Easy element scrolling using jQuery. 8 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html 9 * Works with jQuery +1.2.6. Tested on FF 2/3, IE 6/7, Opera 9.5/6, Safari 3, Chrome 1 on WinXP. 10 * 11 * @author Ariel Flesler 12 * @version 1.4.1 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,Function} settings Optional set of settings or the onAfter callback. 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 * @desc Scroll to a fixed position 37 * @example $('div').scrollTo( 340 ); 38 * 39 * @desc Scroll relatively to the actual position 40 * @example $('div').scrollTo( '+=340px', { axis:'y' } ); 41 * 42 * @dec Scroll using a selector (relative to the scrolled element) 43 * @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } ); 44 * 45 * @ Scroll to a DOM element (same for jQuery object) 46 * @example var second_child = document.getElementById('container').firstChild.nextSibling; 47 * $('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){ 48 * alert('scrolled!!'); 49 * }}); 50 * 51 * @desc Scroll on both axes, to different values 52 * @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } ); 53 */ 54;(function( $ ){ 55 56 var $scrollTo = $.scrollTo = function( target, duration, settings ){ 57 $(window).scrollTo( target, duration, settings ); 58 }; 59 60 $scrollTo.defaults = { 61 axis:'xy', 62 duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1 63 }; 64 65 // Returns the element that needs to be animated to scroll the window. 66 // Kept for backwards compatibility (specially for localScroll & serialScroll) 67 $scrollTo.window = function( scope ){ 68 return $(window).scrollable(); 69 }; 70 71 // Hack, hack, hack... stay away! 72 // Returns the real elements to scroll (supports window/iframes, documents and regular nodes) 73 $.fn.scrollable = function(){ 74 return this.map(function(){ 75 var elem = this, 76 isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1; 77 78 if( !isWin ) 79 return elem; 80 81 var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem; 82 83 return doc.compatMode == 'BackCompat' ? 84 doc.body : 85 doc.documentElement; 86 }); 87 }; 88 89 $.fn.scrollTo = function( target, duration, settings ){ 90 if( typeof duration == 'object' ){ 91 settings = duration; 92 duration = 0; 93 } 94 if( typeof settings == 'function' ) 95 settings = { onAfter:settings }; 96 97 if( target == 'max' ) 98 target = 9e9; 99 100 settings = $.extend( {}, $scrollTo.defaults, settings ); 101 // Speed is still recognized for backwards compatibility 102 duration = duration || settings.speed || settings.duration; 103 // Make sure the settings are given right 104 settings.queue = settings.queue && settings.axis.length > 1; 105 106 if( settings.queue ) 107 // Let's keep the overall duration 108 duration /= 2; 109 settings.offset = both( settings.offset ); 110 settings.over = both( settings.over ); 111 112 return this.scrollable().each(function(){ 113 var elem = this, 114 $elem = $(elem), 115 targ = target, toff, attr = {}, 116 win = $elem.is('html,body'); 117 118 switch( typeof targ ){ 119 // A number will pass the regex 120 case 'number': 121 case 'string': 122 if( /^([+-]=)?\d+(\.\d+)?(px)?$/.test(targ) ){ 123 targ = both( targ ); 124 // We are done 125 break; 126 } 127 // Relative selector, no break! 128 targ = $(targ,this); 129 case 'object': 130 // DOMElement / jQuery 131 if( targ.is || targ.style ) 132 // Get the real position of the target 133 toff = (targ = $(targ)).offset(); 134 } 135 $.each( settings.axis.split(''), function( i, axis ){ 136 var Pos = axis == 'x' ? 'Left' : 'Top', 137 pos = Pos.toLowerCase(), 138 key = 'scroll' + Pos, 139 old = elem[key], 140 Dim = axis == 'x' ? 'Width' : 'Height';
141 142 if( toff ){// jQuery / DOMElement 143 attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] ); 144 145 // If it's a dom element, reduce the margin 146 if( settings.margin ){ 147 attr[key] -= parseInt(targ.css('margin'+Pos)) || 0; 148 attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0; 149 } 150 151 attr[key] += settings.offset[pos] || 0; 152 153 if( settings.over[pos] ) 154 // Scroll to a fraction of its width/height 155 attr[key] += targ[Dim.toLowerCase()]() * settings.over[pos]; 156 }else 157 attr[key] = targ[pos]; 158 159 // Number or 'number' 160 if( /^\d+$/.test(attr[key]) ) 161 // Check the limits 162 attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max(Dim) ); 163 164 // Queueing axes 165 if( !i && settings.queue ){ 166 // Don't waste time animating, if there's no need. 167 if( old != attr[key] ) 168 // Intermediate animation 169 animate( settings.onAfterFirst ); 170 // Don't animate this axis again in the next iteration. 171 delete attr[key]; 172 } 173 }); 174 175 animate( settings.onAfter ); 176 177 function animate( callback ){ 178 $elem.animate( attr, duration, settings.easing, callback && function(){ 179 callback.call(this, target, settings); 180 }); 181 }; 182 183 // Max scrolling position, works on quirks mode 184 // It only fails (not too badly) on IE, quirks mode. 185 function max( Dim ){ 186 var scroll = 'scroll'+Dim; 187 188 if( !win ) 189 return elem[scroll]; 190 191 var size = 'client' + Dim, 192 html = elem.ownerDocument.documentElement, 193 body = elem.ownerDocument.body; 194 195 return Math.max( html[scroll], body[scroll] ) 196 - Math.min( html[size] , body[size] ); 197 198 }; 199 200 }).end(); 201 }; 202 203 function both( val ){ 204 return typeof val == 'object' ? val : { top:val, left:val }; 205 }; 206 207})( jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.