Category | Elements |
---|---|
Export Size | 572 B |
Last Changed | last week |
Detects that a target element's visibility.
<div ref="target"> <h1>Hello world</h1> </div>
import { ref } from 'vue' import { useIntersectionObserver } from '@vueuse/core' export default { setup() { const target = ref(null) const targetIsVisible = ref(false) const { stop } = useIntersectionObserver( target, ([{ isIntersecting }], observerElement) => { targetIsVisible.value = isIntersecting }, ) return { target, targetIsVisible, } }, }
This function also provides a directive version via the
@vueuse/components
package. Learn more about the usage.
<script setup lang="ts"> import { ref } from 'vue' import { vIntersectionObserver } from '@vueuse/components' const root = ref(null) const isVisible = ref(false) function onIntersectionObserver([{ isIntersecting }]) { isVisible.value = isIntersecting } </script> <template> <div> <p> Scroll me down! </p> <div v-intersection-observer="onIntersectionObserver"> <p>Hello world!</p> </div> </div> <!-- with options --> <div ref="root"> <p> Scroll me down! </p> <div v-intersection-observer="[onIntersectionObserver, { root }]"> <p>Hello world!</p> </div> </div> </template>
export interface UseIntersectionObserverOptions extends ConfigurableWindow { /** * Start the IntersectionObserver immediately on creation * * @default true */ immediate?: boolean /** * The Element or Document whose bounds are used as the bounding box when testing for intersection. */ root?: MaybeComputedElementRef /** * A string which specifies a set of offsets to add to the root's bounding_box when calculating intersections. */ rootMargin?: string /** * Either a single number or an array of numbers between 0.0 and 1. */ threshold?: number | number[] } export interface UseIntersectionObserverReturn extends Pausable { isSupported: Ref<boolean> stop: () => void } /** * Detects that a target element's visibility. * * @see https://vueuse.org/useIntersectionObserver * @param target * @param callback * @param options */ export declare function useIntersectionObserver( target: | MaybeComputedElementRef | MaybeRefOrGetter<MaybeElement[]> | MaybeComputedElementRef[], callback: IntersectionObserverCallback, options?: UseIntersectionObserverOptions ): UseIntersectionObserverReturn
© 2019-present Anthony Fu
Licensed under the MIT License.
https://vueuse.org/core/useIntersectionObserver/