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

https://github.com/kodedninja/lazytrigger

Notify when certain points of the DOM enter / leave the view
https://github.com/kodedninja/lazytrigger

Last synced: about 7 hours ago
JSON representation

Notify when certain points of the DOM enter / leave the view

Awesome Lists containing this project

README

          

# lazytrigger

NPM version

Notify when certain points of the DOM enter / leave the view using [on-intersect](https://github.com/yoshuawuyts/on-intersect).

## Installation
```
npm i --save lazytrigger
```

## Example

```javascript
var html = require('nanohtml')
var LazyTrigger = require('lazytrigger')

var trigger = new LazyTrigger(onEnter, onLeave)

module.exports = () => {
return html`


Scroll down, check the console...

${trigger.breakpoint('first')}
Even more...

${trigger.breakpoint('second')}

`
}

function onEnter (id) {
console.log(id + ' entered')
}

function onLeave (id) {
console.log(id + ' left')
}
```

## Why?
Put it to the bottom of an endless scrolling list, play sounds, change the background image, autoplay videos, you name it.

## API

### `trigger = new LazyTrigger(onEnter(id), onLeave(id))`
Initialize a new handler. `onEnter` is called when a breakpoint enters the view, `onLeave` is called when a breakpoint leaves the view; both get the `id` of the breakpoint.

### `trigger.breakpoint(id)`
Returns an empty element that will be observed. It has a comment inside (``) in order to be easily identifiable.

### `trigger.stop(id)`
Stop observing a specific breakpoint.

### `trigger.stopAll()`
Stop observing all the breakpoints.

## See Also
- [yoshuawuyts/on-intersect](https://github.com/yoshuawuyts/on-intersect)
- [jongacnik/monolazy](https://github.com/jongacnik/monolazy)
- [kodedninja/bik](https://github.com/kodedninja/bik)