Tsallake zuwa babban abun ciki Tsallake zuwa kewayawa na takardu
Check
in English

Scrollspy

Sabunta kewayawa ta Bootstrap ta atomatik ko jera abubuwan rukuni bisa ga matsayin gungura don nuna wace hanyar haɗin ke aiki a halin yanzu a tashar kallo.

Yadda yake aiki

Scrollspy yana jujjuya .activeajin akan abubuwan anga ( <a>) lokacin da aka gungura abubuwan da aka idambata ta anga hrefzuwa gani. Scrollspy shine mafi kyawun amfani da haɗin gwiwa tare da bangaren Bootstrap nav ko jerin rukuni , amma kuma zai yi aiki tare da kowane abubuwan anga a cikin shafin na yanzu. Ga yadda yake aiki.

  • Don farawa, scrollspy yana buƙatar abubuwa biyu: kewayawa, ƙungiyar lissafi, ko saitin hanyoyin haɗin yanar gizo mai sauƙi, da akwati mai gungurawa. Kwandon da za a iya gungurawa na iya zama <body>ko na al'ada tare da saiti heightda overflow-y: scroll.

  • A kan ganga mai gungurawa, ƙara data-bs-spy="scroll"kuma data-bs-target="#navId"ina navIdkeɓaɓɓen idkewayawa mai alaƙa. Tabbatar cewa kun haɗa da tabindex="0"don tabbatar da samun damar madannai.

  • Yayin da kake gungurawa kwandon "leken asiri", .activeana ƙara wani aji kuma ana cire shi daga mahaɗan anga cikin kewayawa mai alaƙa. Dole ne hanyoyin haɗin gwiwa su kasance da maƙasudai masu warwarewa id, in ba haka ba za a yi watsi da su. Misali, <a href="#home">home</a>dole ne ya dace da wani abu a cikin DOM kamar<div id="home"></div>

  • Abubuwan da ba a gani ba za a yi watsi da su. Duba sashin abubuwan da ba a iya gani a ƙasa.

Misalai

Gungura wurin da ke ƙasa da maɓallin kewayawa kuma duba canjin aji mai aiki. Bude menu na zaɓuka kuma duba abubuwan da aka zazzage ana haskaka su ma.

Tafarki na farko

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Take na biyu

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Take na uku

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Tafi na hudu

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Tafi na biyar

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

<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 kuma yana aiki tare da nsted .navs. Idan gida .navne .active, iyayensa ma za su kasance .active. Gungura wurin da ke kusa da maɓallin kewayawa kuma duba canjin aji mai aiki.

Abu na 1

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Ka tuna cewa plugin ɗin JavaScript yana ƙoƙarin ɗaukar abin da ya dace tsakanin duk abin da za a iya gani. Manufofin scrollspy da yawa na bayyane a lokaci guda na iya haifar da wasu batutuwa.

Abu na 1-1

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Ka tuna cewa plugin ɗin JavaScript yana ƙoƙarin ɗaukar abin da ya dace tsakanin duk abin da za a iya gani. Manufofin scrollspy da yawa na bayyane a lokaci guda na iya haifar da wasu batutuwa.

Abu na 1-2

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Ka tuna cewa plugin ɗin JavaScript yana ƙoƙarin ɗaukar abin da ya dace tsakanin duk abin da za a iya gani. Manufofin scrollspy da yawa na bayyane a lokaci guda na iya haifar da wasu batutuwa.

Abu na 2

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Ka tuna cewa plugin ɗin JavaScript yana ƙoƙarin ɗaukar abin da ya dace tsakanin duk abin da za a iya gani. Manufofin scrollspy da yawa na bayyane a lokaci guda na iya haifar da wasu batutuwa.

Abu na 3

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Ka tuna cewa plugin ɗin JavaScript yana ƙoƙarin ɗaukar abin da ya dace tsakanin duk abin da za a iya gani. Manufofin scrollspy da yawa na bayyane a lokaci guda na iya haifar da wasu batutuwa.

Abu na 3-1

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Ka tuna cewa plugin ɗin JavaScript yana ƙoƙarin ɗaukar abin da ya dace tsakanin duk abin da za a iya gani. Manufofin scrollspy da yawa na bayyane a lokaci guda na iya haifar da wasu batutuwa.

Abu na 3-2

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Ka tuna cewa plugin ɗin JavaScript yana ƙoƙarin ɗaukar abin da ya dace tsakanin duk abin da za a iya gani. Manufofin scrollspy da yawa na bayyane a lokaci guda na iya haifar da wasu batutuwa.

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

Jerin rukuni

Scrollspy kuma yana aiki tare da .list-groups. Gungura yankin kusa da rukunin jerin kuma duba canjin aji mai aiki.

Abu na 1

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Abu na 2

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Abu na 3

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Abu na 4

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

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

Sauƙaƙe anka

Scrollspy ba'a iyakance ga abubuwan haɗin nav da jerin ƙungiyoyi ba, don haka zai yi aiki akan kowane <a>abubuwan anga a cikin takaddar yanzu. Gungura wurin kuma kalli .activecanjin ajin.

Abu na 1

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Abu na 2

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Abu na 3

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Abu na 4

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

Abu na 5

Wannan wasu abun ciki ne mai riƙe wuri don shafin scrollspy. Lura cewa yayin da kuke gungurawa ƙasa shafin, ana haskaka hanyar kewayawa da ta dace. Ana maimaita shi a cikin misalin bangaren. Muna ci gaba da ƙara wasu kwafin misali anan don jaddada gungurawa da haskakawa.

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

Abubuwan da ba a gani

Abubuwan da ba a ganuwa ba za a yi watsi da su kuma abubuwan da suka dace ba za su sami .activeaji ba. Misalai na Scrollspy da aka fara a cikin abin da ba a iya gani ba za su yi watsi da duk abubuwan da aka yi niyya. Yi amfani da refreshhanyar don bincika abubuwan da za a iya gani da zarar abin nannade ya bayyana.

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

Amfani

Ta hanyar bayanan halayen

Don ƙara halayyar scrollspy cikin sauƙi zuwa kewayawa na saman mashaya, ƙara data-bs-spy="scroll"zuwa ɓangaren da kuke son yin rahõto a kai (mafi yawanci wannan shine <body>). Sannan ƙara data-bs-targetsifa tare da sunan idko aji na ɓangaren iyaye na kowane .navɓangaren Bootstrap.

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

Ta hanyar JavaScript

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

Zabuka

Kamar yadda za a iya wuce zaɓuɓɓuka ta hanyar sifofin bayanai ko JavaScript, zaku iya ƙara sunan zaɓi zuwa data-bs-, kamar a cikin data-bs-animation="{value}". Tabbatar canza nau'in shari'ar sunan zaɓi daga " CamelCase " zuwa " kebab-case " lokacin ƙaddamar da zaɓuɓɓuka ta hanyar halayen bayanai. Misali, amfani data-bs-custom-class="beautifier"maimakon data-bs-customClass="beautifier".

Dangane da Bootstrap 5.2.0, duk abubuwan da aka gyara suna goyan bayan sifa da aka keɓance na gwajidata-bs-config wanda zai iya daidaita tsarin sassa mai sauƙi azaman kirtani JSON. Lokacin da kashi yana data-bs-config='{"delay":0, "title":123}'da data-bs-title="456"sifofi, titleƙimar ƙarshe za ta kasance 456kuma keɓantattun halayen bayanan za su soke ƙimar da aka bayar akan data-bs-config. Bugu da ƙari, halayen bayanan da ke akwai suna iya ɗaukar ƙimar JSON kamar data-bs-delay='{"show":0,"hide":150}'.

Suna Nau'in Default Bayani
rootMargin kirtani 0px 0px -25% Intersection Observer rootMargin ingantattun raka'a, lokacin da ake ƙididdige matsayin gungurawa.
smoothScroll boolean false Yana ba da damar gungurawa santsi lokacin da mai amfani ya danna hanyar haɗin yanar gizon da ke nufin ScrollSpy abubuwan lura.
target kirtani, DOM element null Yana ƙayyade kashi don amfani da Scrollspy plugin.
threshold tsararru [0.1, 0.5, 1] IntersectionObserver ingantaccen shigarwar bakin kofa , lokacin ƙididdige matsayin gungurawa.

Zaɓuɓɓukan da aka soke

Har zuwa v5.1.3 muna amfani offsetda & methodzažužžukan, waɗanda yanzu an soke su kuma an maye gurbinsu da rootMargin. Don ci gaba da dacewa da baya, za mu ci gaba da tantance abin da aka ba offset, rootMarginamma za a cire wannan fasalin a v6 .

Hanyoyin

Hanya Bayani
dispose Yana lalata rubutun gungurawa. (Yana cire bayanan da aka adana akan ɓangaren DOM)
getInstance Hanya madaidaiciya don samun misalin gungurawa mai alaƙa da abun DOM.
getOrCreateInstance Hanya madaidaiciya don samun misalin scrollspy mai alaƙa da abun DOM, ko ƙirƙirar sabo idan ba a fara shi ba.
refresh Lokacin ƙara ko cire abubuwa a cikin DOM, kuna buƙatar kiran hanyar wartsakewa.

Ga misali ta amfani da hanyar wartsakewa:

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

Abubuwan da suka faru

Lamarin Bayani
activate.bs.scrollspy Wannan taron yana yin wuta akan ɓangaren gungura duk lokacin da aka kunna anga ta gungurawa.
const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
  // do something...
})