|
1 (function($) { |
|
2 var j = $.scrollTo = function(a, b, c) { |
|
3 return $(window).scrollTo(a, b, c) |
|
4 }; |
|
5 j.defaults = { |
|
6 axis: 'xy', |
|
7 duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1, |
|
8 limit: true |
|
9 }; |
|
10 j.window = function(a) { |
|
11 return $(window)._scrollable() |
|
12 }; |
|
13 $.fn._scrollable = function() { |
|
14 return this.map(function() { |
|
15 var a = this, |
|
16 isWin = !a.nodeName || $.inArray(a.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) != -1; |
|
17 if (!isWin) return a; |
|
18 var b = (a.contentWindow || a).document || a.ownerDocument || a; |
|
19 return /webkit/i.test(navigator.userAgent) || b.compatMode == 'BackCompat' ? b.body : b.documentElement |
|
20 }) |
|
21 }; |
|
22 $.fn.scrollTo = function(f, g, h) { |
|
23 if (typeof g == 'object') { |
|
24 h = g; |
|
25 g = 0 |
|
26 } |
|
27 if (typeof h == 'function') h = { |
|
28 onAfter: h |
|
29 }; |
|
30 if (f == 'max') f = 9e9; |
|
31 h = $.extend({}, j.defaults, h); |
|
32 g = g || h.duration; |
|
33 h.queue = h.queue && h.axis.length > 1; |
|
34 if (h.queue) g /= 2; |
|
35 h.offset = both(h.offset); |
|
36 h.over = both(h.over); |
|
37 return this._scrollable().each(function() { |
|
38 if (f == null) return; |
|
39 var d = this, |
|
40 $elem = $(d), |
|
41 targ = f, |
|
42 toff, attr = {}, |
|
43 win = $elem.is('html,body'); |
|
44 switch (typeof targ) { |
|
45 case 'number': |
|
46 case 'string': |
|
47 if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) { |
|
48 targ = both(targ); |
|
49 break |
|
50 } |
|
51 targ = $(targ, this); |
|
52 if (!targ.length) return; |
|
53 case 'object': |
|
54 if (targ.is || targ.style) toff = (targ = $(targ)).offset() |
|
55 } |
|
56 var e = $.isFunction(h.offset) && h.offset(d, targ) || h.offset; |
|
57 $.each(h.axis.split(''), function(i, a) { |
|
58 var b = a == 'x' ? 'Left' : 'Top', |
|
59 pos = b.toLowerCase(), |
|
60 key = 'scroll' + b, |
|
61 old = d[key], |
|
62 max = j.max(d, a); |
|
63 if (toff) { |
|
64 attr[key] = toff[pos] + (win ? 0 : old - $elem.offset()[pos]); |
|
65 if (h.margin) { |
|
66 attr[key] -= parseInt(targ.css('margin' + b)) || 0; |
|
67 attr[key] -= parseInt(targ.css('border' + b + 'Width')) || 0 |
|
68 } |
|
69 attr[key] += e[pos] || 0; |
|
70 if (h.over[pos]) attr[key] += targ[a == 'x' ? 'width' : 'height']() * h.over[pos] |
|
71 } else { |
|
72 var c = targ[pos]; |
|
73 attr[key] = c.slice && c.slice(-1) == '%' ? parseFloat(c) / 100 * max : c |
|
74 } |
|
75 if (h.limit && /^\d+$/.test(attr[key])) attr[key] = attr[key] <= 0 ? 0 : Math.min(attr[key], max); |
|
76 if (!i && h.queue) { |
|
77 if (old != attr[key]) animate(h.onAfterFirst); |
|
78 delete attr[key] |
|
79 } |
|
80 }); |
|
81 animate(h.onAfter); |
|
82 |
|
83 function animate(a) { |
|
84 $elem.animate(attr, g, h.easing, a && |
|
85 function() { |
|
86 a.call(this, targ, h) |
|
87 }) |
|
88 } |
|
89 }).end() |
|
90 }; |
|
91 j.max = function(a, b) { |
|
92 var c = b == 'x' ? 'Width' : 'Height', |
|
93 scroll = 'scroll' + c; |
|
94 if (!$(a).is('html,body')) return a[scroll] - $(a)[c.toLowerCase()](); |
|
95 var d = 'client' + c, |
|
96 html = a.ownerDocument.documentElement, |
|
97 body = a.ownerDocument.body; |
|
98 return Math.max(html[scroll], body[scroll]) - Math.min(html[d], body[d]) |
|
99 }; |
|
100 |
|
101 function both(a) { |
|
102 return $.isFunction(a) || typeof a == 'object' ? a : { |
|
103 top: a, |
|
104 left: a |
|
105 } |
|
106 }; |
|
107 return j |
|
108 })(jQuery); |