Main content ah kal rawh Docs navigation ah kal rawh
Check
in English

Scrollspy hmanga ziah a ni

Viewport-a eng link nge active mek tih hriattir turin scroll position hmangin Bootstrap navigation emaw list group components emaw chu automatic-in update rawh.

A hnathawh dan

Scrollspy .activehian anchor ( ) elements-a class chu a toggle a, anchor’s -in a referenced <a>element chu view-ah a scroll a nih chuan. Scrollspy hi Bootstrap nav component emaw list group emaw nena hman dun a tha ber a, mahse tuna page-a anchor element awm zawng zawng nen pawh a thawk thei bawk ang. Hetah hian a hnathawh dan chu kan rawn tarlang e.idhref

  • A bul tan nan scrollspy hian thil pahnih a mamawh a: navigation, list group, emaw link set awlsam tak, chubakah scrollable container te pawh a awm bawk. Scrollable container chu the <body>emaw custom element set heightleh overflow-y: scroll.

  • Scrollable container-ah chuan add la data-bs-spy="scroll", data-bs-target="#navId"khawiah nge a kaihhnawih navigation-a navIdunique chu a awm. Keyboard hman theihna turin ida pawh telh ngei ang che .tabindex="0"

  • “Spied” container i scroll lai hian .activeclass pakhat a rawn dah belh a, a kaihhnawih navigation chhunga anchor link atang chuan a paih chhuak thin. Link te hian resolvable idtarget an nei tur a ni a, chuti lo chuan ngaihthah a ni. Entirnan, a <a href="#home">home</a>chu DOM a thil awm ang chi nen a inmil tur a ni<div id="home"></div>

  • Target element hmuh theih loh te chu ngaihthah a ni ang. A hnuaia Non-visible elements tih hi en la.

Entirna te

Navbar hnuaia area chu scroll la, active class inthlak dan chu en rawh. Dropdown menu hawng la, dropdown items te pawh highlight a nih lai pawh en bawk ang che.

Thupui hmasa ber

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Thupui pahnihna

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Thupui pathumna

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Thupui palina

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Thupui pangana

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

<nav id="navbar-example2" class="navbar bg-light px-3 mb-3">
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav nav-pills">
    <li class="nav-item">
      <a class="nav-link" href="#scrollspyHeading1">First</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#scrollspyHeading2">Second</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false">Dropdown</a>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#scrollspyHeading3">Third</a></li>
        <li><a class="dropdown-item" href="#scrollspyHeading4">Fourth</a></li>
        <li><hr class="dropdown-divider"></li>
        <li><a class="dropdown-item" href="#scrollspyHeading5">Fifth</a></li>
      </ul>
    </li>
  </ul>
</nav>
<div data-bs-spy="scroll" data-bs-target="#navbar-example2" data-bs-root-margin="0px 0px -40%" data-bs-smooth-scroll="true" class="scrollspy-example bg-light p-3 rounded-2" tabindex="0">
  <h4 id="scrollspyHeading1">First heading</h4>
  <p>...</p>
  <h4 id="scrollspyHeading2">Second heading</h4>
  <p>...</p>
  <h4 id="scrollspyHeading3">Third heading</h4>
  <p>...</p>
  <h4 id="scrollspyHeading4">Fourth heading</h4>
  <p>...</p>
  <h4 id="scrollspyHeading5">Fifth heading</h4>
  <p>...</p>
</div>

Nested nav a ni

Scrollspy hian nested .navs nen pawh a thawk thei bawk. Nested .nava nih chuan a .activenu leh pa te pawh an ni ang .active. Navbar bula area chu scroll la, active class inthlak dan chu en rawh.

Item 1-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

JavaScript plugin hian hmuh theih zawng zawng zingah element dik tak thlan a tum tih hre reng ang che. A ruala hmuh theih scrollspy target tam tak chuan harsatna engemaw zat a thlen thei.

Item 1-1-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

JavaScript plugin hian hmuh theih zawng zawng zingah element dik tak thlan a tum tih hre reng ang che. A ruala hmuh theih scrollspy target tam tak chuan harsatna engemaw zat a thlen thei.

Item 1-2-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

JavaScript plugin hian hmuh theih zawng zawng zingah element dik tak thlan a tum tih hre reng ang che. A ruala hmuh theih scrollspy target tam tak chuan harsatna engemaw zat a thlen thei.

Item 2-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

JavaScript plugin hian hmuh theih zawng zawng zingah element dik tak thlan a tum tih hre reng ang che. A ruala hmuh theih scrollspy target tam tak chuan harsatna engemaw zat a thlen thei.

Item 3-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

JavaScript plugin hian hmuh theih zawng zawng zingah element dik tak thlan a tum tih hre reng ang che. A ruala hmuh theih scrollspy target tam tak chuan harsatna engemaw zat a thlen thei.

Item 3-1-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

JavaScript plugin hian hmuh theih zawng zawng zingah element dik tak thlan a tum tih hre reng ang che. A ruala hmuh theih scrollspy target tam tak chuan harsatna engemaw zat a thlen thei.

Item 3-2-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

JavaScript plugin hian hmuh theih zawng zawng zingah element dik tak thlan a tum tih hre reng ang che. A ruala hmuh theih scrollspy target tam tak chuan harsatna engemaw zat a thlen thei.

<div class="row">
  <div class="col-4">
    <nav id="navbar-example3" class="h-100 flex-column align-items-stretch pe-4 border-end">
      <nav class="nav nav-pills flex-column">
        <a class="nav-link" href="#item-1">Item 1</a>
        <nav class="nav nav-pills flex-column">
          <a class="nav-link ms-3 my-1" href="#item-1-1">Item 1-1</a>
          <a class="nav-link ms-3 my-1" href="#item-1-2">Item 1-2</a>
        </nav>
        <a class="nav-link" href="#item-2">Item 2</a>
        <a class="nav-link" href="#item-3">Item 3</a>
        <nav class="nav nav-pills flex-column">
          <a class="nav-link ms-3 my-1" href="#item-3-1">Item 3-1</a>
          <a class="nav-link ms-3 my-1" href="#item-3-2">Item 3-2</a>
        </nav>
      </nav>
    </nav>
  </div>

  <div class="col-8">
    <div data-bs-spy="scroll" data-bs-target="#navbar-example3" data-bs-smooth-scroll="true" class="scrollspy-example-2" tabindex="0">
      <div id="item-1">
        <h4>Item 1</h4>
        <p>...</p>
      </div>
      <div id="item-1-1">
        <h5>Item 1-1</h5>
        <p>...</p>
      </div>
      <div id="item-1-2">
        <h5>Item 1-2</h5>
        <p>...</p>
      </div>
      <div id="item-2">
        <h4>Item 2</h4>
        <p>...</p>
      </div>
      <div id="item-3">
        <h4>Item 3</h4>
        <p>...</p>
      </div>
      <div id="item-3-1">
        <h5>Item 3-1</h5>
        <p>...</p>
      </div>
      <div id="item-3-2">
        <h5>Item 3-2</h5>
        <p>...</p>
      </div>
    </div>
  </div>
</div>

List group a awm

Scrollspy pawh hian .list-groups. List group bula area chu scroll la, active class inthlak dan chu en rawh.

Item 1-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Item 2-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Item 3-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Item 4-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

<div class="row">
  <div class="col-4">
    <div id="list-example" class="list-group">
      <a class="list-group-item list-group-item-action" href="#list-item-1">Item 1</a>
      <a class="list-group-item list-group-item-action" href="#list-item-2">Item 2</a>
      <a class="list-group-item list-group-item-action" href="#list-item-3">Item 3</a>
      <a class="list-group-item list-group-item-action" href="#list-item-4">Item 4</a>
    </div>
  </div>
  <div class="col-8">
    <div data-bs-spy="scroll" data-bs-target="#list-example" data-bs-smooth-scroll="true" class="scrollspy-example" tabindex="0">
      <h4 id="list-item-1">Item 1</h4>
      <p>...</p>
      <h4 id="list-item-2">Item 2</h4>
      <p>...</p>
      <h4 id="list-item-3">Item 3</h4>
      <p>...</p>
      <h4 id="list-item-4">Item 4</h4>
      <p>...</p>
    </div>
  </div>
</div>

Anchor awlsam tak tak te

Scrollspy hi nav component leh list group-ah chauh a innghat lo va, chuvangin <a>tuna document awm mek anchor element eng pawhah a thawk ang. Area chu scroll la, .activeclass inthlak dan chu en rawh.

Item 1-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Item 2-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Item 3-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Item 4-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

Item 5-na a ni

Hei hi scrollspy page atana placeholder content thenkhat a ni. Page i scroll down lai chuan navigation link dik tak chu a rawn lang chhuak tih hre reng ang che. Component entirnan a repeat vek a ni. Hetah hian scroll leh highlighting ngaih pawimawh nan entirnan copy engemaw zat kan dah belh zel a ni.

<div class="row">
  <div class="col-4">
    <div id="simple-list-example" class="d-flex flex-column gap-2 simple-list-example-scrollspy text-center">
      <a class="p-1 rounded" href="#simple-list-item-1">Item 1</a>
      <a class="p-1 rounded" href="#simple-list-item-2">Item 2</a>
      <a class="p-1 rounded" href="#simple-list-item-3">Item 3</a>
      <a class="p-1 rounded" href="#simple-list-item-4">Item 4</a>
      <a class="p-1 rounded" href="#simple-list-item-5">Item 5</a>
    </div>
  </div>
  <div class="col-8">
    <div data-bs-spy="scroll" data-bs-target="#simple-list-example" data-bs-offset="0" data-bs-smooth-scroll="true" class="scrollspy-example" tabindex="0">
      <h4 id="simple-list-item-1">Item 1</h4>
      <p>...</p>
      <h4 id="simple-list-item-2">Item 2</h4>
      <p>...</p>
      <h4 id="simple-list-item-3">Item 3</h4>
      <p>...</p>
      <h4 id="simple-list-item-4">Item 4</h4>
      <p>...</p>
      <h4 id="simple-list-item-5">Item 5</h4>
      <p>...</p>
    </div>
  </div>
</div>

Hmuh theih loh element te

Target element hmuh theih loh te chu ngaihthah a ni ang a, an inmil nav item te chuan .activeclass an dawng dawn lo. Scrollspy instance hmuh theih loh wrapper-a initialized chuan target element zawng zawng a ngaihthah vek ang. refreshWrapper hmuh theih a nih veleh observable elements awm leh awm loh enfiah nan method hmang ang che .

document.querySelectorAll('#nav-tab>[data-bs-toggle="tab"]').forEach(el => {
  el.addEventListener('shown.bs.tab', () => {
    const target = el.getAttribute('data-bs-target')
    const scrollElem = document.querySelector(`${target} [data-bs-spy="scroll"]`)
    bootstrap.ScrollSpy.getOrCreateInstance(scrollElem).refresh()
  })
})

Hman dan tur

Data attribute hmangin a ni

I topbar navigation-a awlsam taka scrollspy behavior dah belh data-bs-spy="scroll"tur chuan, i spy duh element-ah dah la (most typically this would be the <body>). Tichuan Bootstrap component eng pawha parent element or class hming data-bs-targetnen attribute chu add ang che.id.nav

<body data-bs-spy="scroll" data-bs-target="#navbar-example">
  ...
  <div id="navbar-example">
    <ul class="nav nav-tabs" role="tablist">
      ...
    </ul>
  </div>
  ...
</body>

JavaScript hmangin

const scrollSpy = new bootstrap.ScrollSpy(document.body, {
  target: '#navbar-example'
})

Thil thlan theih a ni

Options chu data attribute emaw JavaScript hmanga pass theih a nih avangin option hming chu data-bs-, ah hian i append thei ang data-bs-animation="{value}". Data attribute hmanga options pass dawnin option hming case type chu “ camelCase ” atanga “ kebab-case ” ah thlak ngei ngei tur a ni. Entirnan, data-bs-custom-class="beautifier"tih aiah hmang data-bs-customClass="beautifier"rawh.

Bootstrap 5.2.0 atang khan component zawng zawng hian experimental reserved data attribute an support data-bs-configa, chu chuan JSON string angin component configuration awlsam tak a dah thei a ni. Element pakhatin data-bs-config='{"delay":0, "title":123}'leh data-bs-title="456"attribute a neih chuan a tawp ber titlevalue chu a ni ang a 456, data attribute hrang hrangte chuan , a value pekte chu a override ang data-bs-config. Chu bakah, data attribute awmsa te hian JSON value te chu data-bs-delay='{"show":0,"hide":150}'.

Hming Lampang Hlawhchhamna Hrilhfiahna
rootMargin hrui 0px 0px -25% Intersection Observer rootMargin unit dik tak, scroll awmna chhiar hunah.
smoothScroll boolean a ni false User in ScrollSpy observables sawina link a click chuan smooth scrolling a ti thei.
target string, DOM element hmanga siam a ni null Scrollspy plugin hman tur element a tarlang.
threshold array a ni [0.1, 0.5, 1] IntersectionObserver threshold valid input, scroll awmna tur chhiar laiin.

Thil hman tawh loh Options

v5.1.3 thleng khan offset& methodoptions kan hmang a, tunah chuan deprecated a ni tawh a, rootMargin. Backwards compatibility vawng reng tur chuan given offsetto kan parse chhunzawm zel ang a rootMargin, mahse he feature hi v6 ah hian paih chhuah a ni ang .

Thiltih dan tur

Tihdan Hrilhfiahna
dispose Element pakhat scrollspy a tichhia. (DOM element-a data dahkhawmte a paih chhuak)
getInstance DOM element nena inzawm scrollspy instance hmuh theihna tur static method.
getOrCreateInstance DOM element nena inzawm scrollspy instance hmuh theihna tur static method, a nih loh leh initialized loh palh hlauh chuan a thar siamna tur.
refresh DOM-a elements i dah emaw, i paih emaw hian refresh method i ko a ngai ang.

Refresh method hmanga entirnan hetiang hi a ni:

const dataSpyList = document.querySelectorAll('[data-bs-spy="scroll"]')
dataSpyList.forEach(dataSpyEl => {
  bootstrap.ScrollSpy.getInstance(dataSpyEl).refresh()
})

Thil thlengte

Hunbik Hrilhfiahna
activate.bs.scrollspy He event hian scrollspy-in anchor a activate apiangin scroll element-ah a kap thin.
const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
  // do something...
})