Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lethargy-ts

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lethargy-ts

Distinguish between scroll events initiated by the user, and those by inertial scrolling

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
571
decreased by-58.26%
Maintainers
1
Weekly downloads
 
Created
Source

⭐ Lethargy-TS

lethargy-ts is a modern 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

yarn add lethargy-ts

or

npm install --save lethargy-ts

Usage

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

import { Lethargy } from "lethargy-ts";

const lethargy = new Lethargy();

You can customize parameters to better match your application's needs:

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

😉 If you found 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.

Lethargy focus on preventing false positives (saying it's a normal scroll event when it wasn't), but tolerates false negatives (saying it's not a normal scroll event when it is).

Options

All options 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 percents smaller that 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 scroll 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 two to three frames when the user only scrolled once.

How does it work?

Lethargy keeps a record of the last few wheelDelta values that are passed through it, it will then work out whether these values are decreasing (decaying), and if so, concludes that the scroll event originated from inertial scrolling, and not directly from the user.

Limitations

Not all trackpads work the same, some trackpads do not have a decaying wheelDelta value, so our method of decay detection would not work. Instead, to cater to this situation, we had to, grudgingly, set a very small time delay between when events will register. We have tested this and normal use does not affect user experience more than usual.

TypeScript

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

Contributing

Contributions, issues, and feature requests are welcome!

Show your support

Give a ⭐️ if you like this project!

LICENSE

MIT

Keywords

FAQs

Package last updated on 18 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc