lethargy-ts
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

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.

npm (scoped) Bundle Size type definition License: MIT

🌳 Tiny and easy to use

🦄 Written in TypeScript

🎏 Highly customizable

🏖 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: 2,
  delay: 100,
  inertiaDecay: 20,
});

😉 If you find optimizations for the defaults, please share them in this discussion!

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 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 has a high velocity and doesn't decrease.

  • The delta of the event doesn't 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. 👈

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! ⭐️

LICENSE

MIT

Package Sidebar

Install

npm i lethargy-ts

Weekly Downloads

611

Version

0.0.5

License

MIT

Unpacked Size

48.3 kB

Total Files

15

Last publish

Collaborators

  • snelsi