Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/xg4/in-viewport

Get callback when a DOM element enters or leaves the viewport.
https://github.com/xg4/in-viewport

dom element in-viewport intersection intersection-observer observer viewport

Last synced: 7 days ago
JSON representation

Get callback when a DOM element enters or leaves the viewport.

Awesome Lists containing this project

README

        

# In Viewport

[![Build Status](https://www.travis-ci.com/xg4/in-viewport.svg?branch=master)](https://www.travis-ci.com/xg4/in-viewport)
[![npm](https://img.shields.io/npm/v/@xg4/in-viewport.svg)](https://www.npmjs.com/package/@xg4/in-viewport)
[![GitHub](https://img.shields.io/github/license/xg4/in-viewport.svg)](https://github.com/xg4/in-viewport/blob/master/LICENSE)

> Get callback when a DOM element enters or leaves the viewport.

## Installation

### Install with npm or Yarn

```bash
# npm
$ npm install @xg4/in-viewport --save
```

```bash
# yarn
$ yarn add @xg4/in-viewport
```

## Usage

```js
import InViewport from '@xg4/in-viewport'

const iv = new InViewport()
// or
const options = {} // IntersectionObserverInit options
const iv = new InViewport(options)

// entry: IntersectionObserverEntry
// observer: IntersectionObserver
function onEnter(entry, observer) {
console.log('el enter the viewport')
}

function onLeave(entry, observer) {
console.log('el leave the viewport')
}

// on
iv.on(el, onEnter)
iv.on(el, onEnter, onLeave)
iv.on(el, {
onEnter,
onLeave
})

// off
iv.off(el)

// once
iv.once(el, onEnter)
iv.once(el, onEnter, onLeave)
iv.once(el, {
onEnter,
onLeave,
once: true
})

// also use it, like this
iv.on(el)
.on(el2)
.once(el3)
.off(el4)
```

## Example

> [https://xg4.github.io/in-viewport/](https://xg4.github.io/in-viewport/)

## Contributing

Welcome

- Fork it

- Submit pull request

## Browsers support

Modern browsers and IE10.

| [IE / Edge](http://godban.github.io/browsers-support-badges/)IE / Edge | [Firefox](http://godban.github.io/browsers-support-badges/)Firefox | [Chrome](http://godban.github.io/browsers-support-badges/)Chrome | [Safari](http://godban.github.io/browsers-support-badges/)Safari | [Opera](http://godban.github.io/browsers-support-badges/)Opera |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| IE10, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |

## LICENSE

MIT