mirror of
https://github.com/NoCrypt/migu.git
synced 2026-04-18 15:12:04 +00:00
fix: non-fps bound scrolling
This commit is contained in:
parent
fda8fb3d2a
commit
2964882536
1 changed files with 18 additions and 4 deletions
|
|
@ -5,21 +5,35 @@ export default function (t, { speed = 120, smooth = 10 } = {}) {
|
|||
let moving = false
|
||||
let pos = 0
|
||||
let scrollTop = 0
|
||||
let lastTime = null
|
||||
t.addEventListener('wheel', e => {
|
||||
e.preventDefault()
|
||||
|
||||
pos = Math.max(0, Math.min(pos - Math.max(-1, Math.min(1, (e.delta || e.wheelDelta) ?? -e.detail)) * speed, (t.scrollHeight - t.clientHeight) + (smooth * 2)))
|
||||
if (!moving) update()
|
||||
if (!moving) {
|
||||
lastTime = null
|
||||
update()
|
||||
}
|
||||
}, { capture: true, passive: false })
|
||||
|
||||
// TODO: this needs to be the scrollend event once we update electron
|
||||
function getDeltaTime () {
|
||||
const now = performance.now()
|
||||
if (!lastTime) {
|
||||
lastTime = now
|
||||
return 1
|
||||
}
|
||||
const deltaTime = now - lastTime
|
||||
lastTime = now
|
||||
return deltaTime / 14
|
||||
}
|
||||
|
||||
t.addEventListener('pointerup', () => { pos = scrollTop = t.scrollTop })
|
||||
|
||||
function update () {
|
||||
const delta = pos - scrollTop === smooth * 2 ? 0 : ((pos - scrollTop) / smooth)
|
||||
const delta = pos - scrollTop === smooth * 2 ? 0 : ((pos - scrollTop) / smooth) * getDeltaTime()
|
||||
scrollTop += delta
|
||||
|
||||
t.scrollTo(0, scrollTop)
|
||||
moving = Math.abs(delta) > 0.5 && requestAnimationFrame(update)
|
||||
moving = Math.abs(delta) > 0.3 && requestAnimationFrame(update)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue