Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useElementVisibility.md
1---2category: Elements3---45# useElementVisibility67Tracks the visibility of an element within the viewport.89## Usage1011```vue12<script setup lang="ts">13import { useElementVisibility } from '@vueuse/core'14import { useTemplateRef } from 'vue'1516const target = useTemplateRef('target')17const targetIsVisible = useElementVisibility(target)18</script>1920<template>21<div ref="target">22<h1>Hello world</h1>23</div>24</template>25```2627### rootMargin2829If you wish to trigger your callback sooner before the element is fully visible, you can use30the `rootMargin` option (See [MDN IntersectionObserver/rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin)).3132```ts33import { useElementVisibility } from '@vueuse/core'34// ---cut---35const targetIsVisible = useElementVisibility(target, {36rootMargin: '0px 0px 100px 0px',37})38```3940### threshold4142If you want to control the percentage of the visibility required to update the value, you can use the `threshold` option (See [MDN IntersectionObserver/threshold](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#threshold)).4344```ts45const targetIsVisible = useElementVisibility(target, {46threshold: 1.0, // 100% visible47})48```4950## Component Usage5152```vue53<template>54<UseElementVisibility v-slot="{ isVisible }">55Is Visible: {{ isVisible }}56</UseElementVisibility>57</template>58```5960## Directive Usage6162```vue63<script setup lang="ts">64import { vElementVisibility } from '@vueuse/components'65import { shallowRef, useTemplateRef } from 'vue'6667const target = useTemplateRef('target')68const isVisible = shallowRef(false)6970function onElementVisibility(state) {71isVisible.value = state72}73</script>7475<template>76<div v-element-visibility="onElementVisibility">77{{ isVisible ? 'inside' : 'outside' }}78</div>7980<!-- with options -->81<div ref="target">82<div v-element-visibility="[onElementVisibility, { scrollTarget: target }]">83{{ isVisible ? 'inside' : 'outside' }}84</div>85</div>86</template>87```8889## Type Declarations9091```ts92export interface UseElementVisibilityOptions93extends94ConfigurableWindow,95Pick<UseIntersectionObserverOptions, "rootMargin" | "threshold"> {96/**97* Initial value.98*99* @default false100*/101initialValue?: boolean102/**103* The element that is used as the viewport for checking visibility of the target.104*/105scrollTarget?: UseIntersectionObserverOptions["root"]106/**107* Stop tracking when element visibility changes for the first time108*109* @default false110*/111once?: boolean112}113export type UseElementVisibilityReturn = ShallowRef<boolean>114/**115* Tracks the visibility of an element within the viewport.116*117* @see https://vueuse.org/useElementVisibility118*/119export declare function useElementVisibility(120element: MaybeComputedElementRef,121options?: UseElementVisibilityOptions,122): UseElementVisibilityReturn123```124