Skip to content

ScrollSpy

ScrollSpy uses IntersectionObserver to monitor content section visibility and highlights the corresponding navigation item. Clicking a nav item smoothly scrolls to the target section.


Basic Usage

Scroll the content area on the right and observe the active navigation item change on the left.

vue

<script setup>
const sections = Array.from({ length: 6 }, (_, i) => ({
  id: `section-${i + 1}`,
  title: `Section ${i + 1}`,
}))
const activeId = ref('section-1')
</script>

<TScrollSpy :items="sections" v-model:active-id="activeId" :offset-top="20">
  <div v-for="item in sections" :key="item.id" :id="item.id" style="min-height: 300px">
    <h4>{{ item.title }}</h4>
    <p>Content...</p>
  </div>
</TScrollSpy>

API

Props

PropTypeDefaultDescription
itemsNavItem[][]Navigation data source, { id: string, title: string }
active-id / v-model:active-idstring''Currently active menu item id
offset-topnumber0Scroll offset for fixed headers

Emits

EventParametersDescription
update:active-idid: stringv-model update event
click-itemid: stringNavigation item click event

Slots

SlotDescription
defaultContent area to watch for scroll