Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/tpkn/viewability

Calculate DOM element viewability
https://github.com/tpkn/viewability

Last synced: 6 days ago
JSON representation

Calculate DOM element viewability

Awesome Lists containing this project

README

        

# Viewability
Calculate DOM element viewability

## API

### Viewability(stage, element[, options])

### stage
**Type**: _Object_
`document.documentElement` or `window` object.

### element
**Type**: _Object_
DOM element

### options.percentage
**Type**: _Number_
**Default**: : `50`
Visibility of the block in percentage after which the callback will be triggered.

### options.iab
**Type**: _Boolean_
**Default**: `false`
The callback function will be triggered when block has 50% viewability for 1 second. `percentage` option overrides standard (50%) value.

### .check()
**Type**: _Function_
**Returns**: `Object`
Returned object contains `{ x, y, square }` params, where `x` and `y` are the vertical and horizontal viewability (just incase only one axis needed), and `square` is the visible square area.

## Usage
```javascript
var va;
var stage = document.documentElement
var element = document.getElementById('block');

var viewability = new Viewability(stage, element, {
iab: true,
percentage: 22,
on_visible: onVisible
});

window.onscroll = window.onresize = checkViewability;
checkViewability();

function onVisible(){
console.log('On visible');
}

function checkViewability(){
va = viewability.check();
console.log(va.x, va.y, va.square);
}
```