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