Skip to content

useElementVisibility

Category
Export Size
692 B
Last Changed
6 hours ago

Tracks the visibility of an element within the viewport.

Demo

Info on the right bottom corner
Target Element (scroll down)
Element outside the viewport

Usage

vue
<script setup>
import { useElementVisibility } from '@vueuse/core'
import { ref } from 'vue'

const target = ref(null)
const targetIsVisible = useElementVisibility(target)
</script>

<template>
  <div ref="target">
    <h1>Hello world</h1>
  </div>
</template>

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).

ts
const targetIsVisible = useElementVisibility(target, {
  rootMargin: '0 0 100px 0',
})

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

vue
<template>
  <UseElementVisibility v-slot="{ isVisible }">
    Is Visible: {{ isVisible }}
  </UseElementVisibility>
</template>

Directive Usage

This function also provides a directive version via the @vueuse/components package. Learn more about the usage.

vue
<script setup>
import { vElementVisibility } from '@vueuse/components'
import { ref } from 'vue'

const target = ref(null)
const isVisible = ref(false)

function onElementVisibility(state) {
  isVisible.value = state
}
</script>

<template>
  <div v-element-visibility="onElementVisibility">
    {{ isVisible ? 'inside' : 'outside' }}
  </div>

  <!-- with options -->
  <div ref="target">
    <div v-element-visibility="[onElementVisibility, { scrollTarget: target }]">
      {{ isVisible ? 'inside' : 'outside' }}
    </div>
  </div>
</template>

Type Declarations

typescript
export interface UseElementVisibilityOptions
  extends ConfigurableWindow,
    Pick<UseIntersectionObserverOptions, "threshold"> {
  /**
   * @see https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin
   */
  rootMargin?: MaybeRefOrGetter<string>
  /**
   * The element that is used as the viewport for checking visibility of the target.
   */
  scrollTarget?: MaybeRefOrGetter<HTMLElement | undefined | null>
}
/**
 * Tracks the visibility of an element within the viewport.
 *
 * @see https://vueuse.org/useElementVisibility
 */
export declare function useElementVisibility(
  element: MaybeComputedElementRef,
  options?: UseElementVisibilityOptions,
): Ref<boolean, boolean>

Source

SourceDemoDocs

Contributors

Anthony Fu
Scott Bedard
Anthony Fu
wheat
Amr Bashir
Dominik Ritter
huiliangShen
ziolko-appfire
erikwu
Curt Grimes
vaakian X
sun0day
三咲智子
Jelf
webfansplz
AllenYu
Ary Raditya
Chung, Lian
Carlos Yanes
Alex Kozack

Changelog

v12.1.0 on 12/22/2024
3a928 - feat: add rootMargin option (#4100)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.8.0 on 2/20/2024
ce9bb - fix(useElementVisiblity): can configurable threshold (#3715)
v10.7.0 on 12/5/2023
07d39 - fix: use last intersection entry (#3365)
v10.4.0 on 8/25/2023
429ed - fix: adjust threshold to 0 to fix visibility issue with large element (#3308)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
v10.0.0-beta.2 on 3/28/2023
74b00 - fix!: use useIntersectionObserver instead of scroll event handler (#2551)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)

Released under the MIT License.