Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smooth scrolling behavior improvement #4

Closed
LaneSun opened this issue Feb 11, 2023 · 4 comments
Closed

Smooth scrolling behavior improvement #4

LaneSun opened this issue Feb 11, 2023 · 4 comments

Comments

@LaneSun
Copy link

LaneSun commented Feb 11, 2023

I made some changes with your code, which makes the behavior in smooth scrolling mode more accurate (no more sluggishness when scrolling fast).

I hope you can adopt it

  scrollWithAnimation(event) {
// --     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;
      this.updateScrollAnimation();
    }
  }
@LaneSun
Copy link
Author

LaneSun commented Feb 11, 2023

There were some bugs when working with Typewriter Scroll, fixed.

  scrollWithAnimation(event) {
// <++
    if (!this.isMoving) {
        this.positionY = this.target.scrollTop;
    }
// ++>
// --     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;
      this.updateScrollAnimation();
    }
  }
  updateScrollAnimation() {
    if (!this.isMoving || !this.target) {
// --       this.stopScrollAnimation();
// <++
      return this.stopScrollAnimation();
// ++>
    }
    const divider = Math.pow(this.animationSmoothness, 1.3);
    const delta = this.positionY - this.target.scrollTop;
    this.target.scrollTop += delta / divider;
    if (delta < 0 && this.positionY < 0 && this.target.scrollTop === 0) {
      return this.stopScrollAnimation();
    }
    if (delta > 0 && this.positionY > this.target.scrollHeight - this.target.clientHeight / 2 - this.scrollDistance) {
      return this.stopScrollAnimation();
    }
// --     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();
    }
    window.requestAnimationFrame(this.updateScrollAnimation.bind(this));
  }
  stopScrollAnimation() {
    this.isMoving = false;
    this.scrollDistance = 0;
// <++
    this.positionY = this.target.scrollTop;
// ++>
    if (this.target)
      this.target = void 0;
  }

@flolu
Copy link
Owner

flolu commented Feb 12, 2023

@LaneSun I really appreciate your contributions! Can you please create a pull request? If everything works fine, I will add your changes

@LaneSun
Copy link
Author

LaneSun commented Feb 12, 2023

It's done 😄

@flolu
Copy link
Owner

flolu commented Feb 13, 2023

Thank you so much! Merged with #5

@flolu flolu closed this as completed Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants