PageSourceSearch

http://chelnyltd.ru/js/jquery.scrollto.js

js chelnyltd.ru collected 2026-10-02 17:05:57 UTC 7,590 bytes, 195 lines download raw bytes

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

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.