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.1.0

Published

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

Downloads

2,476

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 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 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 - If there was a pause of this amount of milliseconds between two events, the current event is assumed to be user-triggered.

  • increasingDeltasThreshold - If wheelDelta 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! ⭐️

LICENSE

MIT