-
Notifications
You must be signed in to change notification settings - Fork 0
/
nestedscroll.js
478 lines (441 loc) · 14.2 KB
/
nestedscroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* The MIT License (MIT)
*
* Copyright (c) 2017, Roland Tapken.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/
(function (root, factory) {
if ( typeof define === 'function' && define.amd ) {
define([], factory(root));
} else if (typeof module !== 'undefined' && module.export) {
module.exports = factory(root)();
} else {
root.nestedScroll = factory(root)();
}
})(typeof global !== 'undefined' ? global : this.window || this.global, function (root) {
"use strict";
/**
* Default options
**/
var defaultOptions = {
animationMethod: undefined,
animationTimeout: 500,
force: false,
align: 'top',
withCssMargins: false,
marginTop: 0,
marginLeft: 0,
marginRight: 0,
marginBottom: 0
};
// Local shortcuts
var min = Math.min,
max = Math.max,
abs = Math.abs;
/**
* Returns a DOMRect compatible object
*/
function Rect(x, y, width, height) {
return {
left: x,
top: y,
width: width,
height: height,
right: x + width,
bottom: y + height
};
}
/**
* Helper function to check if an object is a function
**/
function isFunction(functionToCheck) {
// https://stackoverflow.com/questions/5999998/how-can-i-check-if-a-javascript-variable-is-function-type
var getType = {};
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}
/**
* Available animation algorithms. The algorithems get an argument
* between 0..1 and must return a result value between 0..1.
**/
var animationAlgorithms = {
linear: function(linearPos) {
return linearPos;
},
easeInOut: function(linearPos) {
if (linearPos < 1) {
return 0.5 * (1+Math.cos(Math.PI + linearPos * Math.PI));
} else {
return 1.0;
}
},
easeIn: function(linearPos) {
if (linearPos < 1) {
return (1+Math.cos(Math.PI + linearPos * 0.5*Math.PI));
} else {
return 1.0;
}
},
easeOut: function(linearPos) {
if (linearPos < 1) {
return Math.sin(linearPos * 0.5*Math.PI);
} else {
return 1.0;
}
}
};
/**
* Implements scrolling without animation.
*/
var defaultScrollHelper = function(target, left, top) {
if (!this.abort) {
target.scrollLeft += left;
target.scrollTop += top;
}
};
/**
* Implements scrolling with an animation. The parameter 'timeout'
* defines how long the animation should be active. The parameter
* 'animationAlgorithm' is a function that transforms a value between
* 0 and 1 (the fractional animation offset) into a target value
* between 0 and 1 (the fractional scrolling offset).
**/
var animatedScrollHelper = function(target, left, top, animationAlgorithm, timeout) {
var start;
var signX = left < 0 ? -1 : 1;
var signY = top < 0 ? -1 : 1;
left = abs(left);
top = abs(top);
var _this = this;
var sourceX = target.scrollLeft;
var sourceY = target.scrollTop;
function step(timestamp) {
if (_this.abort) {
return;
}
if (start === undefined) {
start = timestamp;
}
// Get relative position to timeout
if ((timestamp - start) < timeout) {
var pos = animationAlgorithm((timestamp - start)/timeout);
var deltaX = Math.round(min(left, left*pos));
var deltaY = Math.round(min(top, top*pos));
target.scrollLeft = sourceX + signX*deltaX;
target.scrollTop = sourceY + signY*deltaY;
window.requestAnimationFrame(step);
} else {
target.scrollLeft = sourceX + signX*left;
target.scrollTop = sourceY + signY*top;
}
}
window.requestAnimationFrame(step);
};
/**
* Chooses a scroll helper from this.options.animationMethod.
**/
var scrollHelper = function(target, left, top) {
if (left >= -1 && left <= 1 && top >= -1 && top <= 1) {
// No need to animate this...
target.scrollTop += top;
target.scrollLeft += left;
return;
}
var animationMethod = this.options['animationMethod'];
var animationTimeout = parseInt(this.options['animationTimeout']);
if (animationMethod && !isNaN(animationTimeout) && animationTimeout > 0) {
if (isFunction(animationMethod)) {
return animatedScrollHelper.call(this, target, left, top, animationMethod, animationTimeout);
} else {
var animationAlgorithm = animationAlgorithms[animationMethod];
if (animationAlgorithm !== undefined) {
return animatedScrollHelper.call(this, target, left, top, animationAlgorithm, animationTimeout);
}
}
}
return defaultScrollHelper.call(this, target, left, top);
};
/**
* Merge option objects
**/
var mergeObjects = function() {
var r = {};
for (var i = 0; i < arguments.length; i+=1) {
var arg = arguments[i];
if (typeof arg === 'object') {
for (var k in arg) {
if (arg.hasOwnProperty(k)) {
r[k] = arg[k];
}
}
}
}
return r;
};
/*
* How to detect which element is the scrolling element in charge of scrolling the viewport:
*
* - in Quirks mode the scrolling element is the "body"
* - in Standard mode the scrolling element is the "documentElement"
*
* webkit based browsers always use the "body" element, disrespectful of the specifications:
*
* http://dev.w3.org/csswg/cssom-view/#dom-element-scrolltop
*
* This feature detection helper allow cross-browser scroll operations on the viewport,
* it will guess which element to use in each browser both in Quirk and Standard modes.
* See how this can be used in a "smooth scroll to anchors references" example here:
*
* https://dl.dropboxusercontent.com/u/598365/scrollTo/scrollTo.html
*
* It is just a fix for possible differences between browsers versions (currently any Webkit).
* In case the Webkit bug get fixed someday, it will just work if they follow the specs. Win !
*
* Author: Diego Perini
* Updated: 2014/09/18
* License: MIT
*/
function getScrollingElement() {
var d = document;
if (d.scrollingElement !== undefined) {
// Roland Tapken: Use document.scrollingElement if available.
// https://developer.mozilla.org/de/docs/Web/API/document/scrollingElement
return d.scrollingElement;
}
return d.documentElement.scrollHeight > d.body.scrollHeight &&
d.compatMode.indexOf('CSS1') === 0 ?
d.documentElement :
d.body;
}
/**
* Returns true if the element should have scrollbars
**/
var hasScrollbar = function(element, computedStyle) {
if (element.scrollWidth > element.clientWidth || element.scrollHeight > element.clientHeight) {
computedStyle = computedStyle || window.getComputedStyle(element);
var overflow = computedStyle.getPropertyValue('overflow');
if (overflow === 'auto' || overflow === 'scroll') {
return true;
}
if (overflow !== 'hidden' && element === getScrollingElement()) {
return true;
}
}
return false;
};
/**
* Returns the relative bounding rect between the target and
* the container element. The container *must* be a parent
* element of target.
**/
var getRelativeBoundingRect = function(tRect, container) {
var cRect;
if (container === getScrollingElement()) {
cRect = new Rect(0, 0, container.clientWidth, container.clientHeight);
} else {
cRect = container.getBoundingClientRect();
}
return new Rect(
tRect.left - cRect.left,
tRect.top - cRect.top,
tRect.width,
tRect.height
);
};
/**
* Locates the nearest scrollable parent element. If the
* element doesn't have a scrollable parent (either because
* no scrollbars are visible, or the element is a child
* of an element with style 'position: fixed') this function
* returns undefined.
**/
var findScrollableParent = function(element) {
if (element === getScrollingElement()) {
// This is either body or html. Don't go further.
return;
}
var computedStyle = window.getComputedStyle(element);
while (computedStyle.getPropertyValue('position') !== 'fixed' && element.parentElement) {
element = element.parentElement;
computedStyle = window.getComputedStyle(element);
if (hasScrollbar(element, computedStyle)) {
return element;
}
}
return undefined;
};
/**
* Helper functions that return the scrolling deltas for
* different alignments.
**/
var alignments = {
left: function(rect, scrollable, force) {
if (!force && rect.left >= 0 && rect.right <= scrollable.clientWidth) {
return 0;
}
var maxLeft = scrollable.scrollWidth - scrollable.clientWidth;
return min(maxLeft, max(0, scrollable.scrollLeft + rect.left)) - scrollable.scrollLeft;
},
top: function(rect, scrollable, force) {
if (!force && rect.top >= 0 && rect.bottom <= scrollable.clientHeight) {
return 0;
}
var maxTop = scrollable.scrollHeight - scrollable.clientHeight;
return min(maxTop, max(0, scrollable.scrollTop + rect.top)) - scrollable.scrollTop;
},
right: function(rect, scrollable, force) {
if (!force && rect.left >= 0 && rect.right <= scrollable.clientWidth) {
return 0;
}
var maxLeft = scrollable.scrollWidth - scrollable.clientWidth;
return min(maxLeft, max(0, scrollable.scrollLeft + rect.right - scrollable.clientWidth)) -
scrollable.scrollLeft;
},
bottom: function(rect, scrollable, force) {
if (!force && rect.top >= 0 && rect.bottom <= scrollable.clientHeight) {
return 0;
}
var maxTop = scrollable.scrollHeight - scrollable.clientHeight;
return min(maxTop, max(0, scrollable.scrollTop + rect.bottom - scrollable.clientHeight)) -
scrollable.scrollTop;
},
autox: function(rect, scrollable, force) {
// Use this minimal offset
var delta1 = alignments.left(rect, scrollable, force),
delta2 = alignments.right(rect, scrollable, force);
if (abs(delta1) <= abs(delta2)) {
return delta1;
} else {
return delta2;
}
},
autoy: function(rect, scrollable, force) {
// Use this minimal offset
var delta1 = alignments.top(rect, scrollable, force),
delta2 = alignments.bottom(rect, scrollable, force);
if (abs(delta1) <= abs(delta2)) {
return delta1;
} else {
return delta2;
}
}
};
/**
* Calculates the target bounding client rect and adds optional
* CSS borders and margins, and if defined the extra margins
* from options.
**/
var getTargetBoundingClientRect = function(element, options) {
var rect = element.getBoundingClientRect();
var left = 0|parseInt(options.marginLeft),
top = 0|parseInt(options.marginTop),
right = 0|parseInt(options.marginRight),
bottom = 0|parseInt(options.marginBottom);
if (options.withCssMargins) {
var tStyle = window.getComputedStyle(element);
top += 0|parseInt(tStyle.marginTop, 10) + 0|parseInt(tStyle.borderTop, 10);
left += 0|parseInt(tStyle.marginLeft, 10) + 0|parseInt(tStyle.borderLeft, 10);
bottom += 0|parseInt(tStyle.marginBottom, 10) + 0|parseInt(tStyle.borderBottom, 10);
right += 0|parseInt(tStyle.marginRight, 10) + 0|parseInt(tStyle.borderRight, 10);
}
return new Rect(
rect.left - left,
rect.top - top,
rect.width + left + right,
rect.height + top + bottom
);
};
/**
* Implementation of NestedScroll.
**/
var currentScrollMonitor;
var nestedScroll = function(element, options) {
options = mergeObjects(defaultOptions, options);
// Stop running scroll process
if (currentScrollMonitor !== undefined) {
currentScrollMonitor.abort = true;
}
currentScrollMonitor = { abort: false, options: options };
var targetRect = getTargetBoundingClientRect(element, options);
var align = (options.align || '').split(' ');
var getOffsetX = align.indexOf('left') >= 0 ? alignments['left'] : (
align.indexOf('right') >= 0 ? alignments['right'] : alignments['autox']
);
var getOffsetY = align.indexOf('top') >= 0 ? alignments['top'] : (
align.indexOf('bottom') >= 0 ? alignments['bottom'] : alignments['autoy']
);
var scrollable = findScrollableParent(element);
var scrollings = [];
var totalOffsetLeft = 0,
totalOffsetTop = 0;
while (scrollable) {
var rect = getRelativeBoundingRect(targetRect, scrollable);
rect.left -= totalOffsetLeft;
rect.top -= totalOffsetTop;
rect.right -= totalOffsetLeft;
rect.bottom -= totalOffsetTop;
var offsetLeft = getOffsetX(rect, scrollable, options.force);
var offsetTop = getOffsetY(rect, scrollable, options.force);
totalOffsetLeft += offsetLeft;
totalOffsetTop += offsetTop;
scrollings.push([scrollable, offsetLeft, offsetTop]);
scrollable = findScrollableParent(scrollable);
}
// Invoke scroll tasks. This is done asynchonously to allow effects like easing.
for (var i = 0; i < scrollings.length; i+=1) {
scrollHelper.apply(currentScrollMonitor, scrollings[i]);
}
};
/**
* Getter/Setter for default options. Call this either without arguments
* to get a copy of all options, with a string argument to get a concrete
* value, with an object argument to update multiple options at once and
* with two arguments to set a concrete value.
**/
nestedScroll.config = function() {
switch (arguments.length) {
case 0:
// Return all options (but only as copy)
return mergeObjects(defaultOptions);
case 1:
if (typeof arguments[0] === 'string') {
// Getter call
return defaultOptions[arguments[0]];
} else {
// Mass setter
var obj = arguments[0];
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
defaultOptions[k] = obj[k];
}
}
}
break;
case 2:
// Setter call
defaultOptions[arguments[0]] = arguments[1];
break;
default:
throw "Illegal number of arguments: " + arguments.length;
}
};
return function() {
return nestedScroll;
};
});