Skip to content

useScrollParent

Intro

Get the closest parent element that is scrollable.

Usage

Basic Usage

html
<div ref="root" />
js
import { ref, watch } from 'vue';
import { useScrollParent, useEventListener } from '@vant/use';

export default {
  setup() {
    const root = ref();
    const scrollParent = useScrollParent(root);

    useEventListener(
      'scroll',
      () => {
        console.log('scroll');
      },
      { target: scrollParent },
    );

    return { root };
  },
};

API

Type Declarations

ts
function useScrollParent(
  element: Ref<Element | undefined>,
): Ref<Element | Window | undefined>;

Params

NameDescriptionTypeDefault Value
elementThe current elementRef<Element>-

Return Value

NameDescriptionType
scrollParentThe closest parent element that is scrollableRef<Element>

Enterprise-level mobile solution based on Vant