Ticket #6266: 6266-jquery-scroll-to.diff
| File 6266-jquery-scroll-to.diff, 8.5 KB (added by , 11 years ago) |
|---|
-
src/bp-core/js/jquery-scroll-to.js
1 /* jshint undef: false */2 /* jshint -W065 */3 4 1 /*! 5 2 * jQuery.ScrollTo 6 3 * Copyright (c) 2007-2014 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com … … 11 8 * @version 1.4.12 12 9 */ 13 10 14 (function(factory) { 15 // AMD 16 if (typeof define === 'function' && define.amd) { 17 define(['jquery'], factory); 18 // CommonJS 19 } else if (typeof exports === 'object') { 20 factory(require('jquery')); 21 // Browser globals 22 } else { 23 factory(jQuery); 24 } 11 ;(function(plugin) { 12 // AMD Support 13 if (typeof define === 'function' && define.amd) { 14 define(['jquery'], plugin); 15 } else { 16 plugin(jQuery); 17 } 25 18 }(function($) { 26 19 27 var $scrollTo = $.scrollTo = function( target, duration, settings) {28 return $(window).scrollTo( target, duration, settings);20 var $scrollTo = $.scrollTo = function( target, duration, settings ) { 21 return $(window).scrollTo( target, duration, settings ); 29 22 }; 30 23 31 24 $scrollTo.defaults = { 32 axis: 'xy',25 axis:'xy', 33 26 duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1, 34 limit: true27 limit:true 35 28 }; 36 29 37 30 // Returns the element that needs to be animated to scroll the window. 38 31 // Kept for backwards compatibility (specially for localScroll & serialScroll) 39 $scrollTo.window = function( ) {32 $scrollTo.window = function( scope ) { 40 33 return $(window)._scrollable(); 41 34 }; 42 35 … … 45 38 $.fn._scrollable = function() { 46 39 return this.map(function() { 47 40 var elem = this, 48 isWin = !elem.nodeName || $.inArray(elem.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) !== -1;41 isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1; 49 42 50 if (!isWin) { 51 return elem; 52 } 43 if (!isWin) 44 return elem; 53 45 54 46 var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem; 55 47 56 return /webkit/i.test(navigator.userAgent) || doc.compatMode == ='BackCompat' ?57 doc.body :58 doc.documentElement;48 return /webkit/i.test(navigator.userAgent) || doc.compatMode == 'BackCompat' ? 49 doc.body : 50 doc.documentElement; 59 51 }); 60 52 }; 61 53 62 $.fn.scrollTo = function( target, duration, settings) {63 if (typeof duration == ='object') {54 $.fn.scrollTo = function( target, duration, settings ) { 55 if (typeof duration == 'object') { 64 56 settings = duration; 65 57 duration = 0; 66 58 } 67 if (typeof settings === 'function') { 68 settings = {onAfter: settings}; 69 } 59 if (typeof settings == 'function') 60 settings = { onAfter:settings }; 70 61 71 if (target == = 'max') {62 if (target == 'max') 72 63 target = 9e9; 73 }74 64 75 settings = $.extend( {}, $scrollTo.defaults, settings);65 settings = $.extend( {}, $scrollTo.defaults, settings ); 76 66 // Speed is still recognized for backwards compatibility 77 67 duration = duration || settings.duration; 78 68 // Make sure the settings are given right 79 69 settings.queue = settings.queue && settings.axis.length > 1; 80 70 81 // Let's keep the overall duration82 if (settings.queue) {71 if (settings.queue) 72 // Let's keep the overall duration 83 73 duration /= 2; 84 } 74 settings.offset = both( settings.offset ); 75 settings.over = both( settings.over ); 85 76 86 settings.offset = both(settings.offset);87 settings.over = both(settings.over);88 89 77 return this._scrollable().each(function() { 90 91 78 // Null target yields nothing, just like jQuery does 92 if (target === null) { 93 return; 94 } 79 if (target == null) return; 95 80 96 81 var elem = this, 97 $elem = $(elem),98 targ = target, toff, attr = {},99 win = $elem.is('html,body');82 $elem = $(elem), 83 targ = target, toff, attr = {}, 84 win = $elem.is('html,body'); 100 85 101 86 switch (typeof targ) { 102 87 // A number will pass the regex … … 103 88 case 'number': 104 89 case 'string': 105 90 if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) { 106 targ = both( targ);91 targ = both( targ ); 107 92 // We are done 108 93 break; 109 94 } 110 95 // Relative/Absolute selector, no break! 111 96 targ = win ? $(targ) : $(targ, this); 112 if (!targ.length) { 113 return; 114 } 115 /* falls through */ 97 if (!targ.length) return; 116 98 case 'object': 117 99 // DOMElement / jQuery 118 if (targ.is || targ.style) {100 if (targ.is || targ.style) 119 101 // Get the real position of the target 120 102 toff = (targ = $(targ)).offset(); 121 }122 103 } 123 104 124 105 var offset = $.isFunction(settings.offset) && settings.offset(elem, targ) || settings.offset; 106 107 $.each( settings.axis.split(''), function( i, axis ) { 108 var Pos = axis == 'x' ? 'Left' : 'Top', 109 pos = Pos.toLowerCase(), 110 key = 'scroll' + Pos, 111 old = elem[key], 112 max = $scrollTo.max(elem, axis); 125 113 126 $.each(settings.axis.split(''), function(i, axis) {127 var Pos = axis === 'x' ? 'Left' : 'Top',128 pos = Pos.toLowerCase(),129 key = 'scroll' + Pos,130 old = elem[key],131 max = $scrollTo.max(elem, axis);132 133 114 if (toff) {// jQuery / DOMElement 134 attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos]);115 attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] ); 135 116 136 117 // If it's a dom element, reduce the margin 137 118 if (settings.margin) { 138 attr[key] -= parseInt(targ.css('margin' +Pos)) || 0;139 attr[key] -= parseInt(targ.css('border' + Pos +'Width')) || 0;119 attr[key] -= parseInt(targ.css('margin'+Pos)) || 0; 120 attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0; 140 121 } 141 122 142 123 attr[key] += offset[pos] || 0; 143 124 144 // Scroll to a fraction of its width/height 145 if (settings.over[pos]) { 146 attr[key] += targ[axis === 'x' ? 'width' : 'height']() * settings.over[pos]; 147 } 125 if(settings.over[pos]) 126 // Scroll to a fraction of its width/height 127 attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos]; 148 128 } else { 149 129 var val = targ[pos]; 150 130 // Handle percentage values 151 attr[key] = val.slice && val.slice(-1) == ='%' ?152 parseFloat(val) / 100 * max153 : val;131 attr[key] = val.slice && val.slice(-1) == '%' ? 132 parseFloat(val) / 100 * max 133 : val; 154 134 } 155 135 156 136 // Number or 'number' 157 if (settings.limit && /^\d+$/.test(attr[key])) {137 if (settings.limit && /^\d+$/.test(attr[key])) 158 138 // Check the limits 159 attr[key] = attr[key] <= 0 ? 0 : Math.min(attr[key], max); 160 } 139 attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max ); 161 140 162 141 // Queueing axes 163 142 if (!i && settings.queue) { 164 143 // Don't waste time animating, if there's no need. 165 if (old != = attr[key]) {144 if (old != attr[key]) 166 145 // Intermediate animation 167 animate(settings.onAfterFirst); 168 } 146 animate( settings.onAfterFirst ); 169 147 // Don't animate this axis again in the next iteration. 170 148 delete attr[key]; 171 149 } 172 150 }); 173 151 174 animate( settings.onAfter);152 animate( settings.onAfter ); 175 153 176 function animate( callback) {177 $elem.animate( attr, duration, settings.easing, callback && function() {154 function animate( callback ) { 155 $elem.animate( attr, duration, settings.easing, callback && function() { 178 156 callback.call(this, targ, settings); 179 157 }); 180 } 158 }; 181 159 182 160 }).end(); 183 161 }; … … 184 162 185 163 // Max scrolling position, works on quirks mode 186 164 // It only fails (not too badly) on IE, quirks mode. 187 $scrollTo.max = function( elem, axis) {188 var Dim = axis == ='x' ? 'Width' : 'Height',189 scroll = 'scroll' +Dim;165 $scrollTo.max = function( elem, axis ) { 166 var Dim = axis == 'x' ? 'Width' : 'Height', 167 scroll = 'scroll'+Dim; 190 168 191 if (!$(elem).is('html,body')) {169 if (!$(elem).is('html,body')) 192 170 return elem[scroll] - $(elem)[Dim.toLowerCase()](); 193 }194 171 195 172 var size = 'client' + Dim, 196 html = elem.ownerDocument.documentElement,197 body = elem.ownerDocument.body;173 html = elem.ownerDocument.documentElement, 174 body = elem.ownerDocument.body; 198 175 199 return Math.max(html[scroll], body[scroll]) - Math.min(html[size], body[size]); 176 return Math.max( html[scroll], body[scroll] ) 177 - Math.min( html[size] , body[size] ); 200 178 }; 201 179 202 function both( val) {203 return $.isFunction(val) || typeof val == = 'object' ? val : {top: val, left: val};204 } 180 function both( val ) { 181 return $.isFunction(val) || typeof val == 'object' ? val : { top:val, left:val }; 182 }; 205 183 206 // AMD requirement 207 return $scrollTo; 208 })); 209 No newline at end of file 184 // AMD requirement 185 return $scrollTo; 186 }));