disable-scroll
Version:
Prevent page scroll
65 lines (43 loc) • 1.78 kB
Markdown
disable scroll
===
[](https://www.npmjs.com/package/disable-scroll) [](https://travis-ci.org/gilbarbara/disable-scroll) [](https://codeclimate.com/github/gilbarbara/disable-scroll/maintainability) [](https://codeclimate.com/github/gilbarbara/disable-scroll/test_coverage)
**Prevent page scrolling like a boss.**
Supports `scroll, wheel, touchmove, keydown` events.
[Demo](https://codesandbox.io/s/github/gilbarbara/disable-scroll/tree/master/demo)
## Setup
### npm
```bash
npm install --save disable-scroll
```
and import it
```typescript
import disableScroll from 'disable-scroll';
```
## Usage
```typescript
disableScroll.on(); // prevent scrolling
...
disableScroll.off(); // re-enable scroll
```
> If you need to support legacy browsers you need to include the scrollingelement polyfill.
## API
### .on(element?: Element, options?: Options)
Disable page scroll by adding event listeners and locking the scroll position.
Options defaults to:
```typescript
{
authorizedInInputs: [32, 37, 38, 39, 40],
disableKeys: true,
disableScroll: true,
disableWheel: true,
keyboardKeys: [32, 33, 34, 35, 36, 37, 38, 39, 40],
}
```
### .off()
Re-enable page scrolling and remove the listeners.
## Issues
If you find a bug, please file an issue on [our issue tracker on GitHub](https://github.com/gilbarbara/disable-scroll/issues).
## License
MIT
---
Inspired by [jquery-disablescroll](https://github.com/ultrapasty/jquery-disablescroll)