Skip to content

Commit

Permalink
Smooth scrolling behavior improvement and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Lane Sun authored and flolu committed Feb 13, 2023
1 parent b144e17 commit 0211294
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ export default class ScrollSpeed extends Plugin {

scrollWithAnimation(event: AugmentedWheelEvent) {
// TODO horizontal scrolling, too
this.positionY = this.target.scrollTop
if (!this.isMoving) {
this.positionY = this.target.scrollTop
}

const acceleration = event.altKey
? Math.pow(this.settings.speed * this.settings.altMultiplier, 1.1)
: Math.pow(this.settings.speed, 1.1)

this.positionY += event.deltaY * acceleration
this.scrollDistance = event.deltaY * acceleration
this.positionY = Math.max(0, Math.min(this.positionY, this.target.scrollHeight - this.target.clientHeight))

if (!this.isMoving) {
this.isMoving = true
Expand All @@ -98,7 +101,7 @@ export default class ScrollSpeed extends Plugin {

updateScrollAnimation() {
if (!this.isMoving || !this.target) {
this.stopScrollAnimation()
return this.stopScrollAnimation()
}

const divider = Math.pow(this.animationSmoothness, 1.3)
Expand All @@ -119,7 +122,7 @@ export default class ScrollSpeed extends Plugin {
}

// Stop when movement delta is approaching zero
if (Math.abs(delta) < this.scrollDistance * 0.015 || Math.abs(delta) < 1) {
if (Math.abs(delta) < this.scrollDistance * 0.015 || Math.abs(delta / divider) < 1) {
return this.stopScrollAnimation()
}

Expand All @@ -129,6 +132,7 @@ export default class ScrollSpeed extends Plugin {
stopScrollAnimation() {
this.isMoving = false
this.scrollDistance = 0
this.positionY = this.target.scrollTop
if (this.target) this.target = undefined
}

Expand Down

0 comments on commit 0211294

Please sign in to comment.