lethargy-ts
v0.1.0
Published
Distinguish between scroll events initiated by the user, and those by inertial scrolling
Downloads
2,476
Maintainers
Readme
⭐ 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.
🌳 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 other parameters to better match your application's needs:
const lethargy = new Lethargy({
sensitivity: 2,
delay: 100,
increasingDeltasThreshold: 3,
});
😉 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 forwheelDelta
for it to register as a valid scroll event. Because the tail of the curve has lowwheelDelta
values, this will stop them from registering as valid scroll events.delay
- If there was a pause of this amount of milliseconds between two events, the current event is assumed to be user-triggered.increasingDeltasThreshold
- IfwheelDelta
has been increasing for this amount of consecutive events, the current event is assumed to be user-triggered.
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 doesn't decrease and immediately follows a known human event.
The delta of the event has been increasing for
n
consecutive events.
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! ⭐️