Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f245eb5

Browse files
committed
feat: scroll
1 parent f420220 commit f245eb5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

scroll.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export const raf = (() => {
2+
return window.requestAnimationFrame ||
3+
window.webkitRequestAnimationFrame ||
4+
window.mozRequestAnimationFrame ||
5+
window.oRequestAnimationFrame ||
6+
window.msRequestAnimationFrame ||
7+
function (callback) {
8+
window.setTimeout(callback, 1000 / 60)
9+
}
10+
})()
11+
12+
let isScrolling = false
13+
let stop = false
14+
15+
export function scroll (el, type, targetPos, time, next) {
16+
if (isScrolling) {
17+
stop = true
18+
}
19+
isScrolling = true
20+
const start = Date.now()
21+
const currentPos = el[type]
22+
23+
raf(step)
24+
25+
function step () {
26+
const current = Date.now()
27+
const rate = Math.min(1, (current - start) / time)
28+
if (rate === 1) {
29+
el[type] = targetPos
30+
isScrolling = false
31+
next && next()
32+
} else {
33+
el[type] = currentPos + (targetPos - currentPos) * rate
34+
if (!stop) {
35+
raf(step)
36+
} else {
37+
stop = false
38+
}
39+
}
40+
}
41+
}
42+
43+
document.addEventListener('visibilitychange', () => {
44+
if (!document.hidden) {
45+
isScrolling = false
46+
stop = false
47+
}
48+
})

0 commit comments

Comments
 (0)