--- category: Elements --- # useElementVisibility Tracks the visibility of an element within the viewport. ## Usage ```vue ``` ### rootMargin If you wish to trigger your callback sooner before the element is fully visible, you can use the `rootMargin` option (See [MDN IntersectionObserver/rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin)). ```ts import { useElementVisibility } from '@vueuse/core' // ---cut--- const targetIsVisible = useElementVisibility(target, { rootMargin: '0px 0px 100px 0px', }) ``` ### threshold If 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)). ```ts const targetIsVisible = useElementVisibility(target, { threshold: 1.0, // 100% visible }) ``` ## Component Usage ```vue ``` ## Directive Usage ```vue ``` ## Type Declarations ```ts export interface UseElementVisibilityOptions extends ConfigurableWindow, Pick { /** * Initial value. * * @default false */ initialValue?: boolean /** * @see https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin */ rootMargin?: MaybeRefOrGetter /** * The element that is used as the viewport for checking visibility of the target. */ scrollTarget?: MaybeRefOrGetter /** * Stop tracking when element visibility changes for the first time * * @default false */ once?: boolean } /** * Tracks the visibility of an element within the viewport. * * @see https://vueuse.org/useElementVisibility */ export declare function useElementVisibility( element: MaybeComputedElementRef, options?: UseElementVisibilityOptions, ): ShallowRef export type UseElementVisibilityReturn = ReturnType ```