Tsibela kumxholo ophambili Tsiba uye kukhangelo lwamaxwebhu
Check
in English

Scrollspy

Hlaziya ngokuzenzekelayo ukukhangela kweBootstrap okanye uluhlu lwamalungu eqela ngokusekelwe kwindawo yokuskrola ukubonisa ukuba leliphi ikhonkco elisebenzayo ngoku kwindawo yokujonga.

Ingaba isebenza kanjani

I-Scrollspy iqhobosha .activeiklasi kwi-ankile ( <a>) izinto xa isiqalelo idesinereferensi ye-ankile hrefsiskrolwa kwimboniselo. I-Scrollspy isetyenziswa kakuhle ngokudityaniswa ne-Bootstrap nav inxalenye okanye uluhlu lweqela , kodwa iyakusebenza nayo nayiphi na imiba yeankile kwiphepha langoku. Nantsi indlela esebenza ngayo.

  • Ukuqala, i-scrollspy ifuna izinto ezimbini: ukukhangela, uluhlu lweqela, okanye iseti elula yamakhonkco, kunye nesikhongozeli esiskrolekayo. Isikhongozeli esiskrolekayo sinokuba yi- <body>okanye into yesiko kunye neseti heightkunye overflow-y: scroll.

  • Kwisikhongozeli esiskrolekayo, yongeza data-bs-spy="scroll"kwaye data-bs-target="#navId"iphi navIdeyona ndlela ikhethekileyo yokukhangela idehambelana nayo. Qinisekisa ukuba uquka kwakhona a tabindex="0"ukuqinisekisa ukufikelela kwibhodi yezitshixo.

  • Njengoko uskrola isikhongozeli "sokuhlola", .activeiklasi iyongezwa kwaye isuswe kumakhonkco e-ankile ngaphakathi kokukhangela okuhambelanayo. Amakhonkco kufuneka abe idneethagethi ezinokusonjululwa, kungenjalo azihoywa. Umzekelo, <a href="#home">home</a>kufuneka ihambelane nento ekwiDOM efana<div id="home"></div>

  • Izinto ekujoliswe kuzo ezingabonakaliyo aziyi kuhoywa. Jonga icandelo lezinto ezingabonwayo ngezantsi.

Imizekelo

Skrolela indawo engezantsi kwe-navbar kwaye ubukele utshintsho lweklasi esebenzayo. Vula imenyu eyehlayo kwaye ubukele izinto ezihlayo ziphawulwe ngokunjalo.

Isihloko sokuqala

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Isihloko sesibini

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Isihloko sesithathu

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Isihloko sesine

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Isihloko sesihlanu

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

<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

I-Scrollspy iphinda isebenze kunye ne-nested .navs. Ukuba kukho indlwane .nav, .activeabazali bayo nabo baya kuba .active. Skrolela indawo ecaleni kwebar ye-nav kwaye ubukele utshintsho lweklasi esebenzayo.

Umba woku-1

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Gcina ukhumbula ukuba iplagi yeJavaScript izama ukukhetha into elungileyo phakathi kwayo yonke into enokubonakala. Iithagethi ezininzi ezibonakalayo zokuskrola ngaxeshanye zinokubangela imiba ethile.

Umba 1-1

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Gcina ukhumbula ukuba iplagi yeJavaScript izama ukukhetha into elungileyo phakathi kwayo yonke into enokubonakala. Iithagethi ezininzi ezibonakalayo zokuskrola ngaxeshanye zinokubangela imiba ethile.

Into 1-2

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Gcina ukhumbula ukuba iplagi yeJavaScript izama ukukhetha into elungileyo phakathi kwayo yonke into enokubonakala. Iithagethi ezininzi ezibonakalayo zokuskrola ngaxeshanye zinokubangela imiba ethile.

Into yesi-2

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Gcina ukhumbula ukuba iplagi yeJavaScript izama ukukhetha into elungileyo phakathi kwayo yonke into enokubonakala. Iithagethi ezininzi ezibonakalayo zokuskrola ngaxeshanye zinokubangela imiba ethile.

Into yesi-3

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Gcina ukhumbula ukuba iplagi yeJavaScript izama ukukhetha into elungileyo phakathi kwayo yonke into enokubonakala. Iithagethi ezininzi ezibonakalayo zokuskrola ngaxeshanye zinokubangela imiba ethile.

Umba 3-1

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Gcina ukhumbula ukuba iplagi yeJavaScript izama ukukhetha into elungileyo phakathi kwayo yonke into enokubonakala. Iithagethi ezininzi ezibonakalayo zokuskrola ngaxeshanye zinokubangela imiba ethile.

Into 3-2

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Gcina ukhumbula ukuba iplagi yeJavaScript izama ukukhetha into elungileyo phakathi kwayo yonke into enokubonakala. Iithagethi ezininzi ezibonakalayo zokuskrola ngaxeshanye zinokubangela imiba ethile.

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

Uluhlu lweqela

I-Scrollspy isebenza kunye no- .list-groups. Skrolela indawo ecaleni kweqela loluhlu kwaye ubukele utshintsho lweklasi esebenzayo.

Umba woku-1

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Into yesi-2

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Into yesi-3

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Into yesi-4

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

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

Ii-ankile ezilula

I-Scrollspy ayikhawulelwanga kumacandelo e-nav kunye namaqela oluhlu, ngoko ke iyakusebenza kuyo nayiphi na <a>imiba yeankile kuxwebhu lwangoku. Skrola indawo kwaye ubukele .activeutshintsho lweklasi.

Umba woku-1

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Into yesi-2

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Into yesi-3

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Into yesi-4

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

Into yesi-5

Lo ngomnye umxholo wesibambi-ndawo wephepha lokuskrola. Qaphela ukuba njengoko uskrolela ezantsi iphepha, ikhonkco lokukhangela elifanelekileyo liyaphawulwa. Iphindwa kumzekelo wecandelo. Sihlala sisongeza ikopi yomzekelo apha ukugxininisa ukuskrola kunye nokuqaqambisa.

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

Izinto ezingabonakaliyo

Izinto ekujoliswe kuzo ezingabonakaliyo aziyi kuhoywa kwaye izinto zazo ze-nav ezihambelanayo aziyi kufumana .activeiklasi. Iziganeko ze-Scrollspy eziqaliswe kwisisongelo esingabonakaliyo ziya kuzihoya zonke izinto ekujoliswe kuzo. Sebenzisa refreshindlela yokukhangela izinto ezibonwayo emva kokuba isisongelo sibonakala.

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

Ukusetyenziswa

Ngeempawu zedatha

Ukongeza ngokulula isimilo sokuskrola kulawulo lwakho lwebar engaphezulu, yongeza data-bs-spy="scroll"kwinto ofuna ukuyihlola (ubukhulu becala le iya kuba yi <body>). Emva koko yongeza data-bs-targetuphawu kunye idokanye igama leklasi yelungu lomzali walo naliphi na ilungu leBootstrap .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>

NgeJavaScript

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

Iinketho

Njengoko iinketho zinokugqithiswa ngeempawu zedatha okanye iJavaScript, unokongeza igama lokhetho ku data-bs-, njengakwi data-bs-animation="{value}". Qinisekisa ukuba utshintshe uhlobo lwecala legama lokukhetha ukusuka kwi " camelCase " ukuya kwi " kebab-case " xa udlula iinketho ngeempawu zedatha. Umzekelo, sebenzisa data-bs-custom-class="beautifier"endaweni ye- data-bs-customClass="beautifier".

Ukusukela kwi-Bootstrap 5.2.0, onke amacandelo axhasa umlinganiso wedatha ogciniweyo wovavanyodata-bs-config onokubeka uqwalaselo lwecandelo olulula njengomtya we-JSON. Xa into ineempawu data-bs-config='{"delay":0, "title":123}'kunye data-bs-title="456"neempawu, ixabiso lokugqibela titleliya kuba 456kunye neempawu zedatha eyahlukileyo ziya kuwenza ngaphezulu amaxabiso anikiweyo data-bs-config. Ukongeza, iimpawu zedatha ezikhoyo ziyakwazi ukubeka ixabiso le-JSON njenge data-bs-delay='{"show":0,"hide":150}'.

Igama Uhlobo Ukuhlala kukho Inkcazo
rootMargin umtya 0px 0px -25% Ukudibana kwe-Observer rootMargin iiyunithi ezisebenzayo, xa kubalwa indawo yokuskrola.
smoothScroll boolean false Yenza ukuskrola okugudileyo xa umsebenzisi ecofa ikhonkco elibhekisa kwiScrollSpy ebonakalayo.
target umtya, DOM element null Ikhankanya into yokufaka iplagi yeScrollspy.
threshold uluhlu [0.1, 0.5, 1] IntersectionObserver ingeniso esebenzayo, xa kubalwa indawo yokuskrola.

Iinketho ezirhoxisiweyo

Ukuya kuthi ga kwi-v5.1.3 besiyisebenzisa offset& methodiinketho, eziyehliweyo ngoku kwaye endaweni yazo kufakwe rootMargin. Ukugcina umva ukuhambelana, siya kuqhubeka nokwahlula okunikiweyo offset, rootMarginkodwa oluphawu luyakususwa kwi- v6 .

Iindlela

Indlela Inkcazo
dispose Itshabalalisa iscrollspy ye element. (Isusa idatha egciniweyo kwisiqalelo seDOM)
getInstance Indlela engatshintshiyo yokufumana umzekelo wokuskrola onxulunyaniswa nento yeDOM.
getOrCreateInstance Indlela engatshintshiyo yokufumana umzekelo wokuskrola onxulunyaniswa nento yeDOM, okanye ukuyila entsha ukuba ayikhange iqalwe.
refresh Xa usongeza okanye ususa izinto kwi-DOM, kuya kufuneka ufowunele indlela yokuhlaziya.

Nanku umzekelo usebenzisa indlela yokuhlaziya:

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

Iziganeko

Isiganeko Inkcazo
activate.bs.scrollspy Esi siganeko sivutha kwinto yokuskrola nanini na xa i-ankile ivuliwe yi-scrollspy.
const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
  // do something...
})