# scrolltrap

> Prevent page from scrolling after having reached the end of a scrollable element

Latest version **1.0.4** (published 2017-01-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install scrolltrap
pnpm add scrolltrap
yarn add scrolltrap
bun add scrolltrap
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2017-01-18 |
| First published | 2017-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Gioele Antoci |
| Maintainers | gioele-antoci |
| Keywords | scrolltrap, trap, scroll, typescript, javascript |

## Links

- npm: https://www.npmjs.com/package/scrolltrap
- Issues: https://github.com/gioele-antoci/scrolltrap/issues
- npm.io page: https://npm.io/package/scrolltrap

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2017-01-18
- 1.0.2 — 2017-01-18
- 1.0.1 — 2017-01-16
- 1.0.0 — 2017-01-16

## README

# Scrolltrap

![Dependency Status](https://img.shields.io/badge/Dependencies-None-brightgreen.svg "No dependencies") [![npm version](https://img.shields.io/badge/npm%20package-1.0.3-brightgreen.svg "Go to NPM's website now!")](https://www.npmjs.com/package/scrolltrap)

Prevent the page from scrolling after having reached the end of a scrollable element.  
Super lightweight: ~1007 bytes gzipped.


### [DEMO](https://gioele-antoci.github.io/scrolltrap/ "Check me out!")



### Installing

**Npm**

```
npm install --save scrolltrap
```

...bundle it cozily with [Browserify](http://browserify.org/) or [Webpack](https://webpack.github.io/) and import it:

```js
import scrolltrap from 'scrolltrap'
```

or

```js
const scrolltrap = require("scrolltrap");
```

**Use a CDN**
```html
<script src="https://cdn.rawgit.com/gioele-antoci/scrolltrap/2ce75825/dist/scrolltrap.js"></script>
```

**Or grab the file directly** from the [dist](dist) folder
```html
<script src="scrolltrap.min.js"></script>
```

## How to use

Simply get an element and pass it to `scrolltrap.attach`. That is it.
```js
const trappableEl = document.getElementsByClassName("element-to-trap")[0];
scrolltrap.attach(trappableEl);
```

## API

**Attach trap**

This will inform _scrolltrap_ to listen for future scrolls on the element passed.
It returns a GUID-like token with which you can later destroy the trap.
```js
attach(el: HTMLElement, options?: scrolltrapOptions): string
```

Optionally a second parameter (options) can be passed.

**Options**

**scrolltrapOptions**

| Field                    | Type       | Default       | Description                                |
| -----------------        | ---------- | -----------   | ------------------------------------------ |
| `detectContentChanges` | `boolean` | `false`      | Detects eventual DOM changes inside trapped element. e.g. the element is `contentEditable`
| `classname`             | `string`  | `NONE`  | Class to add to trapped element once the trap engages. The class will be removed on trap disengagement |


**Destroy trap**

This will inform _scrolltrap_ to stop listening to scroll events. Requires the token that was returned when trap got attached.
```js
destroy(token: string): void
```
___
* If you need to troubleshoot eventual issues set `scrolltrap.debug` to `true`.
* The current version of _scrolltrap_ depends on mouse events. You have no mouse, you have no traps.
* Nested traps are to use to your own peril. 



## Authors

* **Gioele Antoci** - *Initial work* - [gioele-antoci](https://github.com/gioele-antoci)
* **Ian Schmitz** - *Initial work* - [ianSchmitz](https://github.com/ianSchmitz)

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

---
_Source: https://npm.io/package/scrolltrap · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
