npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lethargy-ts

v0.0.5

Published

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

Downloads

1,987

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.

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