-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (43 loc) · 1.09 KB
/
index.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
var requestFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function requestAnimationFallback (callback) {
window.setTimeout(callback, 1000 / 60)
}
})()
function ease (pos) {
return ((pos /= 0.5) < 1)
? (0.5 * Math.pow(pos, 5))
: (0.5 * (Math.pow((pos - 2), 5) + 2))
}
module.exports = function scrollToY (el, Y, speed) {
var isWindow = !!el.alert
var scrollY = isWindow ? el.scrollY : el.scrollTop
var pos = Math.abs(scrollY - Y)
var time = Math.max(0.1, Math.min(pos / speed, 0.8))
let currentTime = 0
function setY () {
module.exports.scrolling = true
currentTime += 1 / 60
var p = currentTime / time
var t = ease(p)
if (p < 1) {
var y = scrollY + ((Y - scrollY) * t)
requestFrame(setY)
if (isWindow) {
el.scrollTo(0, y)
} else {
el.scrollTop = y
}
return
}
if (isWindow) {
el.scrollTo(0, Y)
} else {
el.scrollTop = Y
}
module.exports.scrolling = false
}
setY()
}