Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/herrmannplatz/vue-mixin-in-viewport

Vue 2.0 mixin to observe the visibility of a component
https://github.com/herrmannplatz/vue-mixin-in-viewport

Last synced: about 2 months ago
JSON representation

Vue 2.0 mixin to observe the visibility of a component

Awesome Lists containing this project

README

        

# vue-mixin-in-viewport
Vue 2.0 mixin to observe a components visibilty using the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)

## Note

This mixin requires the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API). In case you want to support older browsers, there is `vue-mixin-in-viewport.polyfill.js` which comes with a [polyfill](https://www.npmjs.com/package/intersection-observer).

## Installation

```bash
$ npm install vue-mixin-in-viewport
```

## Usage

```js
// without polyfill
import inViewportMixin from 'vue-mixin-in-viewport'

// with polyfill
import viewportMixin from 'vue-mixin-in-viewport/dist/vue-mixin-in-viewport.polyfill'

Vue.component('avatar-component', {
template: '#avatar-component',
mixins: [inViewportMixin]
})
```

```html

```

## Props

| Name | Type | Default | Description |
| :------------- | :--------- | :--------- | :------------- |
| root | String | null | Selector specifying element that is used as the viewport for checking visiblity of the target. |
| rootMargin | Number | 0 | Margin around the root. |
| threshold | Number | 0 | Indicates at what percentage of the target's visibility the observer's callback should be executed. |
| once | Boolean | false | Fire only once. |

## Data

| Name | Type | Description |
| :------------- | :--------- | :------------- |
| inViewport | Boolean | `true` if component is in viewport |

## Events

| Name | Description |
| :------------- | :------------- |
| viewport-enter | Fires when target enters the viewport/root element. |
| viewport-change | Fires when target enters or leaves the viewport/root element. |
| viewport-leave | Fires when target leavs the viewport/root element. |

## License

[MIT](http://opensource.org/licenses/MIT)