Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
lethargy-ts
Advanced tools
Distinguish between scroll events initiated by the user, and those by inertial scrolling
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.
🌳 Tiny and Easy to use
🦄 Written in TypeScript
🎏 Highly Customizable
🏖 No external dependencies
yarn add lethargy-ts
or
npm install --save lethargy-ts
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({
stability: 8,
sensitivity: 100,
tolerance: 0.1,
delay: 150,
});
😉 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).
All options are optional:
stability
- Specifies the length of the rolling average. In effect, the larger the value, the smoother the curve will be. This attempts to prevent anomalies from firing 'real' events. Valid values are all positive integers, but in most cases, you would need to stay between 5
and around 30
.
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. The unofficial standard wheelDelta
is 120
, so valid values are positive integers below 120
.
tolerance
- Prevent small fluctuations from affecting results. Valid values are decimals from 0
, but should ideally be between 0.05
and 0.3
.
delay
- Threshold for the amount of time between mouse wheel events for them to be deemed separate.
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.
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.
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.
The module is written in TypeScript and type definitions are included.
Contributions, issues, and feature requests are welcome!
Give a ⭐️ if you like this project!
FAQs
Distinguish between scroll events initiated by the user, and those by inertial scrolling
The npm package lethargy-ts receives a total of 1,164 weekly downloads. As such, lethargy-ts popularity was classified as popular.
We found that lethargy-ts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.