forked from iamdustan/smoothscroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmoothscroll.js
More file actions
150 lines (112 loc) · 3.93 KB
/
smoothscroll.js
File metadata and controls
150 lines (112 loc) · 3.93 KB
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
(function () {
'use strict';
if ('scrollBehavior' in document.documentElement.style) return;
// TODO: make this intelligent according to distance.
var SCROLL_TIME = 300;
var originalScrollTo = window.scrollTo;
var originalScrollBy = window.scrollBy;
var originalScrollIntoView = Element.prototype.scrollIntoView;
// store generally accessible frame id in case a new scroll animation is triggered before the previous
// completes, we can cancel the previous scroll.
var frame;
var startY, startX, endX, endY;
function now() {
return window.performance !== undefined && window.performance.now !== undefined ? window.performance.now() : Date.now !== undefined ? Date.now() : new Date().getTime();
}
// ease-in-out
function ease(k) {
return 0.5 * (1 - Math.cos(Math.PI * k));
}
function smoothScroll(x, y) {
var sx = window.pageXOffset;
var sy = window.pageYOffset;
if (typeof startX === 'undefined') {
startX = sx;
startY = sy;
endX = x;
endY = y;
}
var startTime = now();
// TODO: look into polyfilling scroll-behavior: smooth css property
var step = function() {
var time = now();
var elapsed = (time - startTime) / SCROLL_TIME;
elapsed = elapsed > 1 ? 1 : elapsed;
var value = ease(elapsed);
var cx = sx + ( x - sx ) * value;
var cy = sy + ( y - sy ) * value;
originalScrollTo(cx, cy);
if (cx === endX && cy === endY) {
startX = startY = endX = endY = undefined;
return;
}
frame = requestAnimationFrame(step);
};
if (frame) cancelAnimationFrame(frame);
frame = requestAnimationFrame(step);
}
window.scroll = window.scrollTo = function(x, y, scrollOptions) {
if (typeof(scrollOptions) === 'undefined' || scrollOptions.behavior !== 'smooth')
return originalScrollTo(x, y);
return smoothScroll(x, y);
};
window.scrollBy = function(x, y, scrollOptions) {
if (typeof(scrollOptions) === 'undefined' || scrollOptions.behavior !== 'smooth')
return originalScrollBy(x, y);
var sx = window.pageXOffset;
var sy = window.pageYOffset;
return smoothScroll(x + sx, y + sy);
};
var elementRects, scrollableParent;
function scrollElement(el, x, y) {
el.scrollTop = y;
el.scrollLeft = x;
}
function scroll(el, endCoords) {
var sx = el.scrollLeft;
var sy = el.scrollTop;
var x = endCoords.left;
var y = endCoords.top;
if (typeof startX === 'undefined') {
startX = sx;
startY = sy;
endX = endCoords.left;
endY = endCoords.top;
}
var startTime = now();
var step = function() {
var time = now();
var elapsed = (time - startTime) / SCROLL_TIME;
elapsed = elapsed > 1 ? 1 : elapsed;
var value = ease(elapsed);
var cx = sx + ( x - sx ) * value;
var cy = sy + ( y - sy ) * value;
scrollElement(el, cx, cy);
if (cx === endX && cy === endY) {
startX = startY = endX = endY = undefined;
return;
}
frame = requestAnimationFrame(step);
};
if (frame) cancelAnimationFrame(frame);
frame = requestAnimationFrame(step);
}
function findScrollableParent(el) {
if (el.clientHeight < el.scrollHeight ||
el.clientWidth < el.scrollWidth)
return el;
return findScrollableParent(el.parentNode);
}
Element.prototype.scrollIntoView = function(toTop, scrollOptions) {
if (typeof(scrollOptions) === 'undefined' || scrollOptions.behavior !== 'smooth') return;
scrollableParent = findScrollableParent(this);
var style = window.getComputedStyle(scrollableParent, null);
var paddingLeft = parseInt(style.getPropertyValue('padding-left'), 10);
var paddingTop = parseInt(style.getPropertyValue('padding-top'), 10);
elementRects = {
top: this.offsetTop - (paddingTop * 2),
left: this.offsetLeft - (paddingLeft * 2)
}
return scroll(scrollableParent, elementRects);
};
}());