Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dejorrit/scroll-data-hook

React Hook that returns information about scroll speed, time, distance, direction and more
https://github.com/dejorrit/scroll-data-hook

Last synced: about 1 month ago
JSON representation

React Hook that returns information about scroll speed, time, distance, direction and more

Awesome Lists containing this project

README

        

# Scroll Data Hook

[![NPM](https://img.shields.io/npm/v/scroll-data-hook.svg)](https://www.npmjs.com/package/scroll-data-hook)

The `useScrollData` hook returns information about scroll speed, distance, direction and more. Useful when building dynamic navigation bars or doing other scroll related UI updates.

Check out [the demo](https://dejorrit.github.io/scroll-data-hook/).

## Installation

```bash
yarn add scroll-data-hook
```

## Usage

```tsx
import * as React from "react";
import { useScrollData } from "scroll-data-hook";

const MyComponent = () => {
const {
scrolling,
time,
speed,
direction,
position,
relativeDistance,
totalDistance
} = useScrollData({
onScrollStart: () => {
console.log('Started scrolling');
},
onScrollEnd: () => {
console.log('Finished scrolling');
}
});

return (



{scrolling ? 'Scrolling' : 'Not scrolling'}


Scrolling time: {time} milliseconds


Horizontal speed: {speed.x} pixels per second


Vertical speed: {speed.y} pixels per second


Direction: {direction.x} {direction.y}


Relative distance: {relativeDistance.x}/{relativeDistance.y}


Total distance: {totalDistance.x}/{totalDistance.y}



)
};
```

## License

MIT © [dejorrit](https://github.com/dejorrit)

---

This hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).