Detect when an element is becoming visible or hidden on the page.
npm install vue3-observe-visibility
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![License][license-src]][license-href]
- Installation
- Import
- Browser
- Usage
- IntersectionObserver options
- Once
- Throttling visibility
- Passing custom arguments
- Disabling the observer
- Example
- License
```
npm install --save vue3-observe-visibility
⚠️ This plugin uses the Intersection Observer API that is not supported in every browser (currently supported in Edge, Firefox and Chrome). You need to include a polyfill to make it work on incompatible browsers.
`javascript
import { createApp } from "vue";
import VueObserveVisibility from "vue3-observe-visibility";
const app = createApp();
app.use(VueObserveVisibility);
`
Or:
`javascript`
import { ObserveVisibility as vObserveVisibility } from "vue3-observe-visibility";
`html`
The plugin should be auto-installed. If not, you can install it manually with the instructions below.
Install all the directives:
`javascript`
app.use(VueObserveVisibility);
Use specific directives:
`javascript`
app.directive("observe-visibility", VueObserveVisibility.ObserveVisibility);
The v-observe-visibility directive is very easy to use. Just pass a function as the value:
`html`
This also works on components:
`html`
The function will be called whenever the visiblity of the element changes with the argument being a boolean (true means the element is visible on the page, false means that it is not).
The second argument is the corresponding IntersectionObserverEntry object.
`javascript`
visibilityChanged (isVisible, entry) {
this.isVisible = isVisible
console.log(entry)
}
It's possible to pass the IntersectionObserver options object using the intersection attribute:
`html
v-observe-visibility="{
callback: visibilityChanged,
intersection: {
root: ...,
rootMargin: ...,
threshold: 0.3,
},
}"
>
Once
It can be useful to listen for when the element is visible only once, for example to build introduction animations. Set the
once option to true:`html
v-observe-visibility="{
callback: visibilityChanged,
once: true,
}"
>Throttling visibility
You can use the
throttle options (in ms) specifying minimal state duration after which an event will be fired. It's useful when you are tracking visibility while scrolling and don't want events from fastly scrolled out elements.`html
v-observe-visibility="{
callback: visibilityChanged,
throttle: 300,
}"
>You can also pass a
leading option to trigger the callback the first time when the visibility changes without waiting for the throttle delay.
I can either be visible, hidden or both.`html
v-observe-visibility="{
callback: visibilityChanged,
throttle: 300,
throttleOptions: {
leading: 'visible',
},
}"
>Passing custom arguments
You can add custom argument by using an intermediate function:
`html
v-observe-visibility="(isVisible, entry) => visibilityChanged(isVisible, entry, customArgument)"
>Here
visibilityChanged will be call with a third custom argument customArgument.Disabling the observer
Passing a falsy value to the directive will disable the observer:
`html
v-for="(item, index) of items"
:key="item.id"
v-observe-visibility="index === items.length - 1 ? visibilityChanged : false"
>Example
`html
Hello world!
``---
[npm-version-src]: https://img.shields.io/npm/v/vue3-observe-visibility/latest.svg?style=flat&colorA=18181B&colorB=64B692
[npm-version-href]: https://npmjs.com/package/vue3-observe-visibility
[npm-downloads-src]: https://img.shields.io/npm/dm/vue3-observe-visibility.svg?style=flat&colorA=18181B&colorB=64B692
[npm-downloads-href]: https://npmjs.com/package/vue3-observe-visibility
[license-src]: https://img.shields.io/github/license/ManukMinasyan/vue3-observe-visibility.svg?style=flat&colorA=18181B&colorB=64B692
[license-href]: https://github.com/ManukMinasyan/vue3-observe-visibility/blob/main/LICENSE