Buuka ku bigambo ebikulu Buuka ku docs navigation
Check
in English

Ebiwandiiko ebiyitibwa Scrollspy

Okulongoosa mu ngeri ey’otoma ebitundu by’okutambuliramu kwa Bootstrap oba olukalala lw’ekibinja okusinziira ku kifo ky’okutambula okulaga enkolagana ki ekola mu kiseera kino mu kifo eky’okulaba.

Engeri gye kikola

Scrollspy ekyusa .activeekibiina ku elementi za anchor ( <a>) nga elementi idn'ejuliziddwa anchor's hrefegenda mu kulaba. Scrollspy esinga kukozesebwa wamu ne Bootstrap nav component oba list group , naye era ejja kukola n'ebintu byonna eby'ennanga mu lupapula oluliwo kati. Laba engeri gye kikola.

  • Okutandika, scrollspy yeetaaga ebintu bibiri: okutambulira, ekibinja ky’olukalala, oba ekibinja eky’enkolagana ennyangu, n’okwongerako ekintu ekiyinza okutambula. Ekintu ekiyinza okutambula kiyinza okuba <body>oba ekintu eky'ennono nga kiriko seti heightne overflow-y: scroll.

  • Ku kibya ekiyinza okutambula, yongera data-bs-spy="scroll"era data-bs-target="#navId"wa we navIdkiri eky’enjawulo ideky’okutambulirako okukwatagana. Kakasa nti era ossaamu a tabindex="0"okukakasa nti keyboard eyingira.

  • Nga bw’ogenda mu kifo ekiyitibwa “spied”, .activekiraasi eyongerwako era n’eggyibwa ku nkolagana z’ennanga munda mu kutambula okukwatagana. Links zirina okuba ne idtargets ezisobola okugonjoolwa, bwe kitaba ekyo zisiwuuka empisa. Okugeza, a <a href="#home">home</a>alina okukwatagana n’ekintu mu DOM nga<div id="home"></div>

  • Ebintu ebigendererwa ebitalabika bijja kubuusibwa amaaso. Laba ekitundu ky’ebintu ebitali birabika wansi.

Eby’okulabirako

Ssenda ekitundu wansi wa navbar era olabe enkyukakyuka ya kiraasi ekola. Ggulawo menu egwa wansi era olabe ebintu ebikka nga nabyo bikulaga.

Omutwe ogusooka

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Omutwe ogwokubiri

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Omutwe ogw’okusatu

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Omutwe ogw’okuna

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Omutwe ogw’okutaano

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

<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

Scrollspy era ekola ne nested .navs. Singa nested .naveba .active, bazadde baayo nabo bajja kuba .active. Ssenda ekitundu ekiriraanye navbar era olabe enkyukakyuka ya kiraasi ekola.

Ekintu 1

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Kuuma mu mutima nti JavaScript plugin egezaako okulonda ekintu ekituufu mu byonna ebiyinza okulabika. Ebiruubirirwa bya scrollspy ebirabika ebingi mu kiseera kye kimu biyinza okuleeta ensonga ezimu.

Ekintu 1-1

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Kuuma mu mutima nti JavaScript plugin egezaako okulonda ekintu ekituufu mu byonna ebiyinza okulabika. Ebiruubirirwa bya scrollspy ebirabika ebingi mu kiseera kye kimu biyinza okuleeta ensonga ezimu.

Ekintu 1-2

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Kuuma mu mutima nti JavaScript plugin egezaako okulonda ekintu ekituufu mu byonna ebiyinza okulabika. Ebiruubirirwa bya scrollspy ebirabika ebingi mu kiseera kye kimu biyinza okuleeta ensonga ezimu.

Ekintu 2

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Kuuma mu mutima nti JavaScript plugin egezaako okulonda ekintu ekituufu mu byonna ebiyinza okulabika. Ebiruubirirwa bya scrollspy ebirabika ebingi mu kiseera kye kimu biyinza okuleeta ensonga ezimu.

Ekintu 3

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Kuuma mu mutima nti JavaScript plugin egezaako okulonda ekintu ekituufu mu byonna ebiyinza okulabika. Ebiruubirirwa bya scrollspy ebirabika ebingi mu kiseera kye kimu biyinza okuleeta ensonga ezimu.

Ekintu 3-1

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Kuuma mu mutima nti JavaScript plugin egezaako okulonda ekintu ekituufu mu byonna ebiyinza okulabika. Ebiruubirirwa bya scrollspy ebirabika ebingi mu kiseera kye kimu biyinza okuleeta ensonga ezimu.

Ekintu 3-2

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Kuuma mu mutima nti JavaScript plugin egezaako okulonda ekintu ekituufu mu byonna ebiyinza okulabika. Ebiruubirirwa bya scrollspy ebirabika ebingi mu kiseera kye kimu biyinza okuleeta ensonga ezimu.

<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>

Olukalala lw'ekibinja

Scrollspy nayo ekola ne .list-groups. Ssenda ekitundu ekiriraanye ekibinja ky’olukalala era olabe enkyukakyuka ya kiraasi ekola.

Ekintu 1

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Ekintu 2

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Ekintu 3

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Ekintu 4

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

<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>

Ennanga ennyangu

Scrollspy tekoma ku bitundu bya nav n'ebibinja by'olukalala, kale ejja kukola ku <a>bintu byonna eby'ennanga mu kiwandiiko ekiriwo kati. Ssenda ekitundu ekyo olabe .activeekibiina nga kikyuka.

Ekintu 1

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Ekintu 2

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Ekintu 3

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Ekintu 4

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

Ekintu 5

Kino kye kimu ku bikwata ku kifo eky'olupapula lwa scrollspy. Weetegereze nti bw’ogenda wansi ku lupapula, enkolagana esaanira ey’okutambuliramu eraga. Kiddibwamu mu kyokulabirako kyonna eky'ekitundu. Tusigala nga twongerako kkopi endala ez’ekyokulabirako wano okuggumiza okuzingulula n’okulaga.

<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>

Ebintu ebitali birabika

Ebintu ebigendererwa ebitalabika bijja kubuusibwa amaaso era ebintu byabwe ebya nav ebikwatagana tebijja kufuna .activekiraasi. Scrollspy instances ezitandikibwawo mu wrapper etalabika zijja kubuusa amaaso elements zonna target. Kozesa refreshenkola eno okukebera ebintu ebirabika ng’ekizingirizi kimaze okulabika.

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()
  })
})

Enkozesa

Okuyita mu bikwata ku data

Okusobola okwanguyirwa okwongerako enneeyisa ya scrollspy ku topbar navigation yo, yongera data-bs-spy="scroll"ku elementi gy’oyagala okuketta (okusinga kino kyandibadde <body>). Oluvannyuma yongera ku data-bs-targetattribute idn'erinnya oba ekibiina ky'ekintu ekizadde eky'ekitundu kyonna ekya Bootstrap .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>

Okuyita mu JavaScript

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

Eby’okulondako

Nga eby'okulonda bwe bisobola okuyisibwa nga biyita mu bikwata ku data oba JavaScript, osobola okugattako erinnya ly'okulonda ku data-bs-, nga mu data-bs-animation="{value}". Kakasa nti okyusa ekika kya case eky’erinnya ly’okulonda okuva ku “ camelCase ” okudda ku “ kebab-case ” ng’oyisa eby’okulonda ng’oyita mu data attributes. Okugeza, kozesa data-bs-custom-class="beautifier"mu kifo kya data-bs-customClass="beautifier".

Okuva ku Bootstrap 5.2.0, ebitundu byonna biwagira ekintu kya data ekiterekeddwa eky'okugezesadata-bs-config ekiyinza okuteeka ensengeka y'ebitundu ennyangu ng'olunyiriri lwa JSON. Ekintu bwe kiba data-bs-config='{"delay":0, "title":123}'n'ebintu data-bs-title="456", omuwendo ogusembayo titlegujja kuba 456era ebifaananyi bya data eby'enjawulo bijja kusazaamu emiwendo egyaweebwa ku data-bs-config. Okugatta ku ekyo, ebikwata ku data ebiriwo bisobola okuteeka emiwendo gya JSON nga data-bs-delay='{"show":0,"hide":150}'.

Erinnya Okuwandiika Okukosamu Okunnyonnyola
rootMargin akaguwa 0px 0px -25% Intersection Observer rootMargin yuniti entuufu, nga obala ekifo ky'okuzingulula.
smoothScroll boolean false Esobozesa okutambula obulungi ng'omukozesa anyiga ku link ejuliza ScrollSpy observables.
target omuguwa, ekintu kya DOM null Laga elementi okukozesa Scrollspy plugin.
threshold ensengekera [0.1, 0.5, 1] IntersectionObserver threshold valid input, nga obala ekifo ky'okuzingulula.

Ebyokulonda Ebitakozesebwa

Okutuuka ku v5.1.3 twali tukozesa offset& methodoptions, kati ezitakozesebwa era ne zikyusibwamu rootMargin. Okukuuma okukwatagana okudda emabega, tujja kwongera okusengejja ekiweereddwa offsetku rootMargin, naye ekintu kino kijja kuggyibwawo mu v6 .

Enkola

Engeri Okunnyonnyola
dispose Esaanyaawo scrollspy ya elementi. (Eggyawo data eterekeddwa ku elementi ya DOM)
getInstance Enkola ya static okufuna scrollspy instance ekwatagana ne DOM element.
getOrCreateInstance Enkola ya static okufuna scrollspy instance ekwatagana ne DOM element, oba okukola ekipya singa kiba nga tekitandikibwawo.
refresh Nga oyongera oba okuggyawo ebintu mu DOM, ojja kwetaaga okuyita enkola y'okuzza obuggya.

Wano waliwo ekyokulabirako ng’okozesa enkola y’okuzza obuggya:

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

Ebibaddewo

Omukolo Okunnyonnyola
activate.bs.scrollspy Ekintu kino kikuba amasasi ku elementi y'omuzingo buli nnanga lw'ekolebwa scrollspy.
const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
  // do something...
})