Skip to content
On this page

useElementVisibility

Category
Last Changed
3 months 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

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

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

export default {
  setup() {
    const target = ref(null)
    const targetIsVisible = useElementVisibility(target)

    return {
      target,
      targetIsVisible,
    }
  }
}
</script>
<template>
  <div ref="target">
    <h1>Hello world</h1>
  </div>
</template>

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

export default {
  setup() {
    const target = ref(null)
    const targetIsVisible = useElementVisibility(target)

    return {
      target,
      targetIsVisible,
    }
  }
}
</script>

Component

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.
<UseElementVisibility v-slot="{ isVisible }">
  Is Visible: {{ isVisible }}
</UseElementVisibility>
<UseElementVisibility v-slot="{ isVisible }">
  Is Visible: {{ isVisible }}
</UseElementVisibility>

Type Declarations

export interface VisibilityScrollTargetOptions extends ConfigurableWindow {
  scrollTarget?: Ref<Element | null | undefined>
}
/**
 * Tracks the visibility of an element within the viewport.
 *
 * @see https://vueuse.org/useElementVisibility
 * @param element
 * @param options
 */
export declare function useElementVisibility(
  element: Ref<Element | null | undefined>,
  { window, scrollTarget }?: VisibilityScrollTargetOptions
): Ref<boolean>
export interface VisibilityScrollTargetOptions extends ConfigurableWindow {
  scrollTarget?: Ref<Element | null | undefined>
}
/**
 * Tracks the visibility of an element within the viewport.
 *
 * @see https://vueuse.org/useElementVisibility
 * @param element
 * @param options
 */
export declare function useElementVisibility(
  element: Ref<Element | null | undefined>,
  { window, scrollTarget }?: VisibilityScrollTargetOptions
): Ref<boolean>

Source

SourceDemoDocs

Contributors

Anthony Fu
Scott Bedard
wheat
Amr Bashir
Ary Raditya
Chung, Lian
Carlos Yanes
Alex Kozack

Changelog

v6.7.5 on 11/3/2021
b58cc - fix: prop 'as' does not work as expected (#889)
v6.4.0 on 9/17/2021
e557f - feat: added 'as' prop to renderable components (#742)
v5.0.0-beta.2 on 5/25/2021
5bede - feat: introduce components & directives (#486)
useElementVisibility has loaded