Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Akryum/vue-observe-visibility
Detect when an element is becoming visible or hidden on the page.
https://github.com/Akryum/vue-observe-visibility
Last synced: about 20 hours ago
JSON representation
Detect when an element is becoming visible or hidden on the page.
- Host: GitHub
- URL: https://github.com/Akryum/vue-observe-visibility
- Owner: Akryum
- Created: 2016-12-23T15:09:16.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-11-08T02:15:08.000Z (almost 2 years ago)
- Last Synced: 2024-10-29T11:10:26.922Z (5 days ago)
- Language: JavaScript
- Homepage: https://jsfiddle.net/Akryum/ppt7endj/
- Size: 1.32 MB
- Stars: 1,651
- Watchers: 14
- Forks: 87
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
- awesome-github-vue - vue-observe-visibility - 当元素在页面上可见或隐藏时检测 (实用库)
- awesome-github-vue - vue-observe-visibility - 当元素在页面上可见或隐藏时检测 (实用库)
- awesome - vue-observe-visibility - 当元素在页面上可见或隐藏时检测 (实用库)
- awesome-vue - vue-observe-visibility - observe-visibility?style=social) - 当元素在页面上可见或隐藏时检测 (实用库)
README
vue-observe-visibility
Detect when an element is becoming visible or hidden on the page. Demo## Sponsors
[![sponsors logos](https://guillaume-chau.info/sponsors.png)](https://guillaume-chau.info/sponsors)
## Table of contents
- [Installation](#installation)
- [Usage](#usage)
- [Example](#example)# Installation
```
npm install --save vue-observe-visibility
```**⚠️ This plugin uses the [Intersection Observer API](http://caniuse.com/#feat=intersectionobserver) that is not supported in every browser (currently supported in Edge, Firefox and Chrome). You need to include a [polyfill](https://github.com/w3c/IntersectionObserver/tree/master/polyfill) to make it work on incompatible browsers.**
## Import
```javascript
import Vue from 'vue'
import VueObserveVisibility from 'vue-observe-visibility'Vue.use(VueObserveVisibility)
```Or:
```javascript
import Vue from 'vue'
import { ObserveVisibility } from 'vue-observe-visibility'Vue.directive('observe-visibility', ObserveVisibility)
```## Browser
```html
```
The plugin should be auto-installed. If not, you can install it manually with the instructions below.
Install all the directives:
```javascript
Vue.use(VueObserveVisibility)
```Use specific directives:
```javascript
Vue.directive('observe-visibility', VueObserveVisibility.ObserveVisibility)
```# Usage
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](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) object.
```javascript
visibilityChanged (isVisible, entry) {
this.isVisible = isVisible
console.log(entry)
}
```## IntersectionObserver options
It's possible to pass the [IntersectionObserver `options` object](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#Parameters) using the `intersection` attribute:
```html
```## 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
```## 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
```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
```## Passing custom arguments
You can add custom argument by using an intermediate function:
```html
```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
```# Example
```html
Toggle
Is visible?
Hello world!new Vue({
el: '#app',
data: {
show: true,
isVisible: true,
},
methods: {
visibilityChanged (isVisible, entry) {
this.isVisible = isVisible
console.log(entry)
},
},
})```
---
## License
[MIT](http://opensource.org/licenses/MIT)