|
| 1 | +# IntersectionObserver |
| 2 | + |
| 3 | +> The [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API). |
| 4 | +
|
| 5 | +## State |
| 6 | + |
| 7 | +The `useIntersectionObserver` function exposes the following reactive state: |
| 8 | + |
| 9 | +```js |
| 10 | +import { useIntersectionObserver } from "vue-composable"; |
| 11 | + |
| 12 | +const { elements, isIntersecting } = useIntersectionObserver(options); |
| 13 | +``` |
| 14 | + |
| 15 | +| State | Type | Description | |
| 16 | +| -------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------- | |
| 17 | +| elements | `IntersectionObserverEntry[]` | [IntersectionObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) | |
| 18 | +| isIntersecting | `Boolean` | Returns true if **all** observed elements are intersection | |
| 19 | + |
| 20 | +## Methods |
| 21 | + |
| 22 | +The `useIntersectionObserver` function exposes the following methods: |
| 23 | + |
| 24 | +```js |
| 25 | +import { useIntersectionObserver } from "vue-composable"; |
| 26 | + |
| 27 | +const { observe, unobserve, disconnect, debug } = useIntersectionObserver(); |
| 28 | +``` |
| 29 | + |
| 30 | +| Signature | Description | |
| 31 | +| ------------------- | ------------------------------------------------------------------------------------------------------------------- | |
| 32 | +| `observe(Element)` | Starts observing Element | |
| 33 | +| `unobserve(Element` | Stops observing Element | |
| 34 | +| `disconnect()` | [IntersectionObserver.disconnect](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/disconnect) | |
| 35 | + |
| 36 | +<!-- | `debug()` | Provides some debug information: [WIP] | --> |
| 37 | + |
| 38 | +## Example |
| 39 | + |
| 40 | +```vue |
| 41 | +<template> |
| 42 | + <div> |
| 43 | + Visible: {{ isIntersecting }} |
| 44 | +
|
| 45 | + <p>Scroll down</p> |
| 46 | + <div style="height:500px" /> |
| 47 | +
|
| 48 | + <div ref="el" style="background:lightgreen"> |
| 49 | + Hide me |
| 50 | + </div> |
| 51 | +
|
| 52 | + <p>Scroll up</p> |
| 53 | + </div> |
| 54 | +</template> |
| 55 | +
|
| 56 | +<script> |
| 57 | +import { ref, watch, reactive } from "@vue/composition-api"; |
| 58 | +import { useIntersectionObserver } from "vue-composable"; |
| 59 | +
|
| 60 | +export default { |
| 61 | + name: "intersection-observer-example", |
| 62 | + setup() { |
| 63 | + const el = ref(null); |
| 64 | + const o = useIntersectionObserver(el); |
| 65 | + return { |
| 66 | + ...o, |
| 67 | + el |
| 68 | + }; |
| 69 | + } |
| 70 | +}; |
| 71 | +</script> |
| 72 | +``` |
| 73 | + |
| 74 | +### Code |
| 75 | + |
| 76 | +<ClientOnly> |
| 77 | +<intersection-observer-example/> |
| 78 | +</ClientOnly> |
0 commit comments