Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/hunghg255/window-onscroll

Window scroll
https://github.com/hunghg255/window-onscroll

npm scrolling

Last synced: 3 months ago
JSON representation

Window scroll

Awesome Lists containing this project

README

        



logo


A library make window scroll smooth and easy to use


NPM Version
NPM Downloads
Minizip
Contributors
License

## Usage

Get it from [npm](https://www.npmjs.com/package/window-onscroll):

```sh
npm install window-onscroll
```

## Import it and use it:

```js
import ScrollManager from 'window-onscroll';

const sm = new ScrollManager();

console.log(sm.getScrollPosition());
// -> { scrollPositionX: 0, scrollPositionY: 0 }

window.addEventListener('window-scroll', function (e) {
console.log('Vertical scroll position is: ' + e.detail.scrollPositionY);
console.log('Horizontal scroll position is: ' + e.detail.scrollPositionX);

requestAnimationFrame(function () {
// repaint logic goes here
// example:
// rotateDiv.style.transform = 'rotate(' + e.detail.scrollPositionY / 2 + 'deg)';
});
});
```

## Browser support

```js

var sm = new ScrollManager();

var valueDiv = document.querySelector('.ScrollPosition-value');
var rotateDiv = document.querySelector('.ScrollPosition-rotate');

window.addEventListener('window-onscroll', function (e) {
requestAnimationFrame(function () {
valueDiv.innerHTML = parseInt(e.detail.scrollPositionY, 10);
rotateDiv.style.transform = 'rotate(' + e.detail.scrollPositionY / 2 + 'deg)';
});
});

```

## API

- `getScrollPosition()` - returns current window scroll position object.
```js
{
scrollPositionX: 0,
scrollPositionY: 100,
}
```
- `removeListener()` - reduces internal reference counter by one, and if it reaches 0 destroys the instance. Reference counter is increased every time `new ScrollManager` is called. For example, this is useful when scroll manager is used in React high order component (to track if any component is still using it). Use with caution.
- `destroy()` - destroys the singleton instance and removes `scroll` listener. Use with caution, call `removeListener` do this for you.