2

GitHub - snelsi/lethargy-ts: 👾 Distinguish between intentional wheel events and...

 1 year ago
source link: https://github.com/snelsi/lethargy-ts
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

star Lethargy-TS

lethargy-ts is a modern TypeScript rewrite of lethargy – a popular JavaScript library to help distinguish between scroll events initiated by the user, and those by inertial scrolling.

deciduous_tree Tiny and easy to use

unicorn Written in TypeScript

flags Highly customizable

beach_umbrella No external dependencies

Install

Install with Yarn:

yarn add lethargy-ts

Or with npm:

npm install --save lethargy-ts

Usage

Import and create an instance of Lethargy. It will remember previously checked wheelEvents to help determine if they are inertial or not:

import { Lethargy } from "lethargy-ts";

const lethargy = new Lethargy();

You can customize the sensitivity, delay, and inertia decay parameters to better match your application's needs:

const lethargy = new Lethargy({
  sensitivity: 20,
  delay: 100,
  inertiaDecay: 10,
});

wink If you find optimizations for the defaults, please share them in this ticket!

Bind the wheel event and pass the event to Lethargy:

const checkWheelEvent = (e: WheelEvent) => {
  const isIntentional = lethargy.check(e);

  if (isIntentional) {
    // Do something with the scroll event
  }
};

window.addEventListener("wheel", checkWheelEvent, { passive: true });

lethargy.check(e) will return true if it's a normal wheel event initiated by the user, and false if it's initiated by inertial scrolling.

Options

All parameters are optional:

  • sensitivity - Specifies the minimum value for wheelDelta for it to register as a valid scroll event. Because the tail of the curve has low wheelDelta values, this will stop them from registering as valid scroll events.

  • delay - Threshold for the amount of time between mouse wheel events for them to be deemed separate.

  • inertiaDecay - Inertia event may be no more than this percent smaller than the previous event.

What problem does it solve?

Scroll plugins such as smartscroll, jquery-mousewheel, or fullPage.js work by detecting scroll events and then doing something with them, such as scrolling to the next frame. However, inertial scrolling continues to emit scroll events even after the user stopped, and this can often lead to problems, such as scrolling multiple frames when the user only intended to scroll once. lethargy-ts helps distinguish between scroll events initiated by the user and those by inertial scrolling, making it easier to create smooth and accurate scrolling experiences.

How it works

lethargy-ts uses a set of checks to determine if a scroll event is initiated by the user or by inertial scrolling. The following checks are performed:

  • Enough time has passed between two events.

  • The delta of the event is bigger than the delta of the previous event.

  • The vector of the event differs from the previous event.

  • The delta of the event is above 100 and it does not decrease.

  • The event does not decrease and immediately follows a known human event.

  • The speed of the delta's change suddenly jumped.

If any of these checks are true, the event is considered intentional. Otherwise, it is considered to be initiated by inertial scrolling.

Limitations

Not all trackpads work the same, so Lethargy makes its best effort to cover as many use cases as possible, but full coverage is not guaranteed. Lethargy focuses on preventing false positives (i.e., saying it's a normal scroll event when it wasn't) but tolerates false negatives (i.e., saying it's not a normal scroll event when it is).

Sadly, right now there is no easy native way to tell if the wheel event was generated by the user or by inertia. You can change this by leaving an upvote and a comment in the w3c proposal ticket. point_left

TypeScript

The module is written in TypeScript and type definitions are included.

Contributing

Contributions, issues, and feature requests are welcome!

Show your support

If you find this project useful, please give it a star on GitHub! star

LICENSE

MIT


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK