Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/darmikon/visi

Small js library to assign events when html element becomes visible in viewport
https://github.com/darmikon/visi

events javascript-events visi visibility-event

Last synced: about 1 hour ago
JSON representation

Small js library to assign events when html element becomes visible in viewport

Awesome Lists containing this project

README

        

## Visi.js ##

### Description ###
Visi.js is a small library written on vanilla.js.
It doesn't require any dependencies.
Visi.js provides functionality to monitor when dom element is partially in a viewport
It adds some methods to simplify your work with handling of such event.

### Demo ###
[Visi + animation](http://visi.mana.pro/ "Visi.js")

[Visi on jsfiddler](https://jsfiddle.net/Hellfrom/ksn8je0o/ "Visi on jsfiddler") (Watch console)

### Installation ###

1. Remote

```

```
2. npm
```
npm install visi
```
3. bower
```
bower install visi
```

### How to use ###
Event names
`visible` and `hidden`

Short api
- add
```
.add(domElements); //single or multiple dom elements
```

Short api
- add
```
.add(domElements); //single or multiple dom elements
```
- on
```
.on(eventName,function(changedElement,off){
//off() removes event listeners from fired element
});
```
- once (unregisters all events from fired element after first triggering)
```
.once(eventName,function(changedElement){
//...
});
```
- off
```
var visi = Visi.add(/*dom elements*/);
visi.off(); //remove all event listeners from selection
```
- getOff
```
var off = Visi.add(dom)
.on('visible',callback)
.getOff();
off(); //remove all listeners from selection
```

HTML
```


Lorem...














Lorem ipsum...














Lorem ipsum dolor...

```

JS
```
var visi = Visi.add(document.getElementById('id1'))
.on('visible',function(){
console.log('visible id1');
});

visi.off();
```

Add event to the same instance again
```
visi.on('visible',function(){
console.log('visible again');
});

```

Add event only once - after first triggering it will automatically unregister
all event listeners
```
Visi.add(document.getElementById('id1')).once('hidden',function(element,off){
//off() is not necessary for this use case
//...
});

```

Listen if element becomes hidden every time with ```.on```
```
Visi.add(document.querySelectorAll('.p1'))
.on('hidden',function(element,off){
console.log('hidden id1',element);
console.log(arguments);

//unregister listeners only for element which
//became hidden after some scrolling etc.
off();
});
```

Get unregisters
```
var off = Visi.add(document.getElementById('id1'))
.on('visible',function(){
console.log('visible id1');
}).getOff();

//remove all events from all dom elements for current selection
off();

```