Tlolela ho litaba tsa mantlha Tlolela ho li-docs navigation
Check
in English

Scrollspy

Ntlafatsa ka boiketsetso ho tsamaea ha Bootstrap kapa lethathamo la likarolo tsa sehlopha ho ipapisitse le boemo ba moqolo ho bontša hore na ke sehokelo sefe se sebetsang hona joale boema-kepe.

Kamoo e sebetsang kateng

Scrollspy e totobatsa .activesehlopha linthong tsa ankora ( <a>) ha elemente e nang le idtšupiso ea ankora hrefe shejoa hore e shebelle. Scrollspy e sebelisoa hamolemo ka ho fetisisa hammoho le karolo ea Bootstrap nav kapa sehlopha sa lenane , empa hape e tla sebetsa le likarolo leha e le life tsa ankora leqepheng la hona joale. Mona ke kamoo e sebetsang kateng.

  • Ho qala, scrollspy e hloka lintho tse peli: ho tsamaea, sehlopha sa lenane, kapa sete e bonolo ea likhokahano, hammoho le setshelo se phuthollang. Sets'oants'o se phutholohang e ka ba ntho <body>kapa ntho e tloaelehileng e nang le sete heightle overflow-y: scroll.

  • Ka har'a sets'oants'o sa ho phutholla, eketsa data-bs-spy="scroll"le data-bs-target="#navId"hore navIdna mofuta o ikhethileng ido hokae. Etsa bonnete ba hore o kenyelletsa a tabindex="0"ho netefatsa phihlello ea keyboard.

  • Ha u ntse u tsamaisa sets'oants'o sa "spied", .activesehlopha sea eketsoa le ho tlosoa ho lihokelo tsa ankora ka har'a mokhoa o amanang le ona. Lihokelo li tlameha ho ba le lipehelo tse rarollehang id, ho seng joalo li tla hlokomolohuoa. Ka mohlala, e <a href="#home">home</a>tlameha ho lumellana le ntho e itseng e DOM e kang<div id="home"></div>

  • Lintlha tse reriloeng tse sa bonahaleng li tla hlokomolohuoa. Sheba karolo ea Lintho tse sa bonahaleng ka tlase.

Mehlala

Tsamaisetsa sebaka se ka tlase ho navbar 'me u shebelle phetoho ea sehlopha se sebetsang. Bula menu e theoha 'me u shebelle lintho tse theohang li totobatsoa hape.

Sehlooho sa pele

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Sehlooho sa bobeli

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Sehlooho sa boraro

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Sehlooho sa bone

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Sehlooho sa bohlano

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

<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 e boetse e sebetsa le sehlaha sa .navs. Haeba sehlaha .navse le teng .active, batsoali ba sona le bona ba tla .active. Tsamaisetsa sebaka se haufi le navbar 'me u shebelle phetoho ea sehlopha se sebetsang.

Ntlha ea 1

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Hopola hore plugin ea JavaScript e leka ho khetha ntho e nepahetseng har'a tsohle tse ka hlahang. Lipheo tse ngata tse bonahalang tsa scrollpy ka nako e le 'ngoe li ka baka mathata.

Ntlha ea 1-1

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Hopola hore plugin ea JavaScript e leka ho khetha ntho e nepahetseng har'a tsohle tse ka hlahang. Lipheo tse ngata tse bonahalang tsa scrollpy ka nako e le 'ngoe li ka baka mathata.

Ntlha ea 1-2

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Hopola hore plugin ea JavaScript e leka ho khetha ntho e nepahetseng har'a tsohle tse ka hlahang. Lipheo tse ngata tse bonahalang tsa scrollpy ka nako e le 'ngoe li ka baka mathata.

Ntlha ea 2

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Hopola hore plugin ea JavaScript e leka ho khetha ntho e nepahetseng har'a tsohle tse ka hlahang. Lipheo tse ngata tse bonahalang tsa scrollpy ka nako e le 'ngoe li ka baka mathata.

Ntlha ea 3

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Hopola hore plugin ea JavaScript e leka ho khetha ntho e nepahetseng har'a tsohle tse ka hlahang. Lipheo tse ngata tse bonahalang tsa scrollpy ka nako e le 'ngoe li ka baka mathata.

Ntlha ea 3-1

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Hopola hore plugin ea JavaScript e leka ho khetha ntho e nepahetseng har'a tsohle tse ka hlahang. Lipheo tse ngata tse bonahalang tsa scrollpy ka nako e le 'ngoe li ka baka mathata.

Ntlha ea 3-2

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Hopola hore plugin ea JavaScript e leka ho khetha ntho e nepahetseng har'a tsohle tse ka hlahang. Lipheo tse ngata tse bonahalang tsa scrollpy ka nako e le 'ngoe li ka baka mathata.

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

Sehlopha sa lenane

Scrollspy e boetse e sebetsa le .list-groups. Tsamaisetsa sebaka se haufi le sehlopha sa lenane mme o shebelle ho fetoha ha sehlopha se sebetsang.

Ntlha ea 1

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Ntlha ea 2

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Ntlha ea 3

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Ntlha ea 4

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

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

Liankora tse bonolo

Scrollspy ha e felle feela ho likarolo tsa nav le lihlopha tsa lenane, kahoo e tla sebetsa ho <a>likarolo life kapa life tsa ankora tokomaneng ea hajoale. Tsamaisetsa sebaka seo 'me u shebelle hore .activesehlopha se fetoha.

Ntlha ea 1

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Ntlha ea 2

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Ntlha ea 3

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Ntlha ea 4

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

Ntlha ea 5

Tsena ke litaba tsa setšoantšisi bakeng sa leqephe la scrollspy. Hlokomela hore ha u ntse u theosa leqephe, sehokelo se loketseng sa ho sesa sea totobatsoa. E phetoa ho pholletsa le mohlala oa karolo. Re lula re eketsa kopi ea mohlala mona ho totobatsa ho phenya le ho totobatsa.

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

Lintho tse sa bonahaleng

Lintlha tse shebiloeng tse sa bonahaleng li tla hlokomolohuoa 'me lintho tse tsamaisanang le tsona li ke ke tsa fumana .activesehlopha. Maemo a Scrollspy a qaliloeng ka har'a sephutheloana se sa bonahaleng a tla hlokomoloha likarolo tsohle tse shebiloeng. Sebelisa refreshmokhoa ho hlahloba lintlha tse hlokomelehang hang ha wrapper e bonahala.

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

Tšebeliso

Ka litšobotsi tsa data

Ho eketsa habonolo boits'oaro ba scrollspy ho navigation ea topbar ea hau, eketsa nthong data-bs-spy="scroll"eo u batlang ho e hloela (haholo-holo ena e tla ba eona <body>). Ebe u eketsa data-bs-targettšobotsi ka lebitso idkapa sehlopha sa karolo ea motsoali ea karolo efe kapa efe ea 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>

Ka JavaScript

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

Dikgetho

Kaha likhetho li ka fetisoa ka lintlha tsa data kapa JavaScript, u ​​ka kenyelletsa lebitso la khetho ho data-bs-, joalo ka data-bs-animation="{value}". Etsa bonnete ba hore o fetola mofuta oa nyeoe ea lebitso la khetho ho tloha " camelCase "ho " kebab-case " ha o fetisa likhetho ka litšobotsi tsa data. Ka mohlala, sebelisa data-bs-custom-class="beautifier"ho e-na le data-bs-customClass="beautifier".

Ho tloha ka Bootstrap 5.2.0, likarolo tsohle li ts'ehetsa tlhahlobo ea data e bolokiloeng data-bs-confige ka bolokang tlhophiso e bonolo ea karolo joalo ka khoele ea JSON. Ha element e na data-bs-config='{"delay":0, "title":123}'le data-bs-title="456"litšoaneleho, boleng ba ho qetela titlebo tla ba 456'me lintlha tse arohaneng tsa data li tla feta boleng bo fanoeng ho data-bs-config. Ntle le moo, litšoaneleho tse teng tsa data li khona ho boloka boleng ba JSON joalo ka data-bs-delay='{"show":0,"hide":150}'.

Lebitso Mofuta Ea kamehla Tlhaloso
rootMargin khoele 0px 0px -25% Intersection Observer rootMargin diyuniti tse sebetsang, ha ho balwa boemo ba moqolo.
smoothScroll boolean false E nolofalletsa ho tsamaea ha bonolo ha mosebelisi a tobetsa sehokelo se buang ka lintho tse bonoang ke ScrollSpy.
target khoele, karolo ea DOM null E hlakisa ntho eo u ka e sebelisang ho sebelisa plugin ea Scrollspy.
threshold array [0.1, 0.5, 1] IntersectionObserver threshold e sebetsang hantle, ha ho baloa boemo ba moqolo.

Likhetho tse Fokotsoeng

Ho fihlela v5.1.3 re ne re sebelisa offset& methodlikhetho, tse seng li sa sebetse 'me li nkeloa sebaka ke rootMargin. Ho boloka tšebelisano ea morao-rao, re tla tsoelapele ho bapisa e fanoeng offsetho rootMargin, empa tšobotsi ena e tla tlosoa ho v6 .

Mekhoa

Mokhoa Tlhaloso
dispose E senya scrollpy ea element. (E tlosa lintlha tse bolokiloeng karolong ea DOM)
getInstance Mokhoa o tsitsitseng oa ho fumana mohlala oa scrollspy o amanang le ntho ea DOM.
getOrCreateInstance Mokhoa o tsitsitseng oa ho etsa hore mohlala oa scrollspy o amanang le ntho ea DOM, kapa ho theha e ncha haeba e sa qalisoa.
refresh Ha o eketsa kapa o tlosa likarolo ho DOM, o tla hloka ho letsetsa mokhoa oa ho khatholla.

Mona ke mohlala o sebelisang mokhoa oa ho hlasimolla:

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

Liketsahalo

Ketsahalo Tlhaloso
activate.bs.scrollspy Ketsahalo ena e chesa karolo ea moqolo neng kapa neng ha ankora e kentsoe ke scrollspy.
const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
  // do something...
})