{"id":15031538,"url":"https://github.com/akryum/vue-observe-visibility","last_synced_at":"2025-05-13T23:08:02.850Z","repository":{"id":41207516,"uuid":"77232800","full_name":"Akryum/vue-observe-visibility","owner":"Akryum","description":"Detect when an element is becoming visible or hidden on the page.","archived":false,"fork":false,"pushed_at":"2022-11-08T02:15:08.000Z","size":1384,"stargazers_count":1649,"open_issues_count":30,"forks_count":88,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-05-11T02:48:26.165Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://jsfiddle.net/Akryum/ppt7endj/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Akryum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"Akryum"}},"created_at":"2016-12-23T15:09:16.000Z","updated_at":"2025-05-07T19:05:04.000Z","dependencies_parsed_at":"2023-01-24T08:46:22.453Z","dependency_job_id":null,"html_url":"https://github.com/Akryum/vue-observe-visibility","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akryum%2Fvue-observe-visibility","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akryum%2Fvue-observe-visibility/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akryum%2Fvue-observe-visibility/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Akryum%2Fvue-observe-visibility/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Akryum","download_url":"https://codeload.github.com/Akryum/vue-observe-visibility/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254041474,"owners_count":22004730,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T20:15:57.572Z","updated_at":"2025-05-13T23:07:57.821Z","avatar_url":"https://github.com/Akryum.png","language":"JavaScript","funding_links":["https://github.com/sponsors/Akryum","https://www.patreon.com/akryum"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"./logo.png\" alt=\"vue-observe-visibility logo\"/\u003e\n\u003c/p\u003e\n\n\u003ch1 align=\"center\"\u003evue-observe-visibility\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://www.npmjs.com/package/vue-observe-visibility\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/vue-observe-visibility.svg\"/\u003e \u003cimg src=\"https://img.shields.io/npm/dm/vue-observe-visibility.svg\"/\u003e\u003c/a\u003e \u003ca href=\"https://vuejs.org/\"\u003e\u003cimg src=\"https://img.shields.io/badge/vue-2.x-brightgreen.svg\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\nDetect when an element is becoming visible or hidden on the page. \u003ca href=\"https://jsfiddle.net/Akryum/ppt7endj/\"\u003eDemo\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.patreon.com/akryum\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://c5.patreon.com/external/logo/become_a_patron_button.png\" alt=\"Become a Patreon\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Sponsors\n\n[![sponsors logos](https://guillaume-chau.info/sponsors.png)](https://guillaume-chau.info/sponsors)\n\n\u003cbr\u003e\n\n## Table of contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Example](#example)\n\n# Installation\n\n```\nnpm install --save vue-observe-visibility\n```\n\n**⚠️ 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.**\n\n## Import\n\n```javascript\nimport Vue from 'vue'\nimport VueObserveVisibility from 'vue-observe-visibility'\n\nVue.use(VueObserveVisibility)\n```\n\nOr:\n\n```javascript\nimport Vue from 'vue'\nimport { ObserveVisibility } from 'vue-observe-visibility'\n\nVue.directive('observe-visibility', ObserveVisibility)\n```\n\n## Browser\n\n```html\n\u003cscript src=\"vue.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://unpkg.com/vue-observe-visibility/dist/vue-observe-visibility.min.js\"\u003e\u003c/script\u003e\n```\n\nThe plugin should be auto-installed. If not, you can install it manually with the instructions below.\n\nInstall all the directives:\n\n```javascript\nVue.use(VueObserveVisibility)\n```\n\nUse specific directives:\n\n```javascript\nVue.directive('observe-visibility', VueObserveVisibility.ObserveVisibility)\n```\n\n# Usage\n\nThe `v-observe-visibility` directive is very easy to use. Just pass a function as the value:\n\n```html\n\u003cdiv v-observe-visibility=\"visibilityChanged\"\u003e\n```\n\nThis also works on components:\n\n```html\n\u003cMyComponent v-observe-visibility=\"visibilityChanged\" /\u003e\n```\n\nThe 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).\n\nThe second argument is the corresponding [IntersectionObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) object.\n\n```javascript\nvisibilityChanged (isVisible, entry) {\n  this.isVisible = isVisible\n  console.log(entry)\n}\n```\n\n## IntersectionObserver options\n\nIt's possible to pass the [IntersectionObserver `options` object](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#Parameters) using the `intersection` attribute:\n\n```html\n\u003cdiv v-observe-visibility=\"{\n  callback: visibilityChanged,\n  intersection: {\n    root: ...,\n    rootMargin: ...,\n    threshold: 0.3,\n  },\n}\"\u003e\n```\n\n## Once\n\nIt 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`:\n\n```html\n\u003cdiv v-observe-visibility=\"{\n  callback: visibilityChanged,\n  once: true,\n}\"\u003e\n```\n\n## Throttling visibility\n\nYou 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.\n\n```html\n\u003cdiv v-observe-visibility=\"{\n  callback: visibilityChanged,\n  throttle: 300,\n}\"\u003e\n```\n\nYou can also pass a `leading` option to trigger the callback the first time when the visibility changes without waiting for the throttle delay.\nI can either be `visible`, `hidden` or `both`.\n\n```html\n\u003cdiv v-observe-visibility=\"{\n  callback: visibilityChanged,\n  throttle: 300,\n  throttleOptions: {\n    leading: 'visible',\n  },\n}\"\u003e\n```\n\n## Passing custom arguments\n\nYou can add custom argument by using an intermediate function:\n\n```html\n\u003cdiv v-observe-visibility=\"(isVisible, entry) =\u003e visibilityChanged(isVisible, entry, customArgument)\"\u003e\n```\n\nHere `visibilityChanged` will be call with a third custom argument `customArgument`.\n\n## Disabling the observer\n\nPassing a falsy value to the directive will disable the observer:\n\n```html\n\u003cdiv\n  v-for=\"(item, index) of items\"\n  :key=\"item.id\"\n  v-observe-visibility=\"index === items.length - 1 ? visibilityChanged : false\"\n\u003e\n```\n\n# Example\n\n```html\n\u003cdiv id=\"app\"\u003e\n  \u003cbutton @click=\"show = !show\"\u003eToggle\u003c/button\u003e\n  \u003clabel\u003e\n    \u003cinput type=\"checkbox\" v-model=\"isVisible\" disabled/\u003e Is visible?\n  \u003c/label\u003e\n  \u003cdiv ref=\"test\" v-show=\"show\" v-observe-visibility=\"visibilityChanged\"\u003eHello world!\u003c/div\u003e\n\u003c/div\u003e\n\n\u003cscript\u003e\nnew Vue({\n  el: '#app',\n  data: {\n    show: true,\n    isVisible: true,\n  },\n  methods: {\n    visibilityChanged (isVisible, entry) {\n      this.isVisible = isVisible\n      console.log(entry)\n    },\n  },\n})\n\u003c/script\u003e\n```\n\n---\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakryum%2Fvue-observe-visibility","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakryum%2Fvue-observe-visibility","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakryum%2Fvue-observe-visibility/lists"}