Scrollspy
Fa'afou otometi le Bootstrap navigation po'o le lisi o vaega vaega e fa'atatau i le tulaga ta'ai e fa'ailoa ai po'o fea so'otaga o lo'o galue i le va'aiga.
E faapefea ona galue
Scrollspy e sui le .active
vasega i luga o le taula ( <a>
) elemene pe a o'o mai le elemene o id
lo'o fa'asino i ai le taula href
. Scrollspy e sili ona faʻaoga faʻatasi ma se vaega Bootstrap nav poʻo le lisi kulupu , ae o le a aoga foi ma soʻo se elemene taula i le itulau o loʻo i ai nei. O le auala lea e aoga ai.
-
Ina ia amata, scrollspy e manaʻomia ni mea se lua: o se faʻataʻitaʻiga, lisi vaega, poʻo se seti faigofie o fesoʻotaʻiga, faʻatasi ai ma se atigipusa e mafai ona taʻavale. O le atigipusa ta'ai e mafai ona avea ma
<body>
elemene masani ma se setiheight
maoverflow-y: scroll
. -
I luga o le koneteina e mafai ona ta'ai, fa'aopoopo
data-bs-spy="scroll"
ma odata-bs-target="#navId"
feanavId
e tulaga eseid
ai le fa'atautaiga fa'atasi. Ia mautinoa ia aofia ai foi setabindex="0"
e mautinoa ai le avanoa o le keyboard. -
A'o e ta'ai le koneteina "mata'i",
.active
e fa'aopoopoina se vasega ma 'ave'esea mai feso'ota'iga taula i totonu o le fa'asinomaga fa'atasi. O feso'ota'iga e tatau ona i ai ni fa'amoemoe e mafai ona fo'iaid
, a leai e le amana'ia. Mo se faʻataʻitaʻiga, e<a href="#home">home</a>
tatau ona fetaui ma se mea i le DOM pei<div id="home"></div>
-
O elemene autu e le o vaaia o le a le amanaiaina. Va'ai le vaega e le o va'aia elemene i lalo.
Faataitaiga
Navbar
Fa'asolo le vaega i lalo o le navbar ma matamata le suiga o le vasega. Tatala le lisi pa'u i lalo ma matamata i mea e pa'u i lalo o lo'o fa'amamafaina fo'i.
Uluai ulutala
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ulutala lona lua
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ulutala lona tolu
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ulutala lona fa
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ulutala lona lima
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
<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
E galue fo'i Scrollspy i fa'aputuga .nav
s. Afai o se faamoega , .nav
e .active
faapena foi ona matua .active
. Fa'asolo le vaega i tafatafa o le navbar ma matamata le suiga o le vasega.
Aitema 1
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ia manatua o le JavaScript plugin e taumafai e filifili le elemene saʻo i mea uma e mafai ona iloa. O le tele o mata'itusi ta'ai va'aia i le taimi e tasi e ono tupu ai ni fa'afitauli.
Aitema 1-1
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ia manatua o le JavaScript plugin e taumafai e filifili le elemene saʻo i mea uma e mafai ona iloa. O le tele o mata'itusi ta'ai va'aia i le taimi e tasi e ono tupu ai ni fa'afitauli.
Aitema 1-2
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ia manatua o le JavaScript plugin e taumafai e filifili le elemene saʻo i mea uma e mafai ona iloa. O le tele o mata'itusi ta'ai va'aia i le taimi e tasi e ono tupu ai ni fa'afitauli.
Aitema 2
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ia manatua o le JavaScript plugin e taumafai e filifili le elemene saʻo i mea uma e mafai ona iloa. O le tele o mata'itusi ta'ai va'aia i le taimi e tasi e ono tupu ai ni fa'afitauli.
Aitema 3
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ia manatua o le JavaScript plugin e taumafai e filifili le elemene saʻo i mea uma e mafai ona iloa. O le tele o mata'itusi ta'ai va'aia i le taimi e tasi e ono tupu ai ni fa'afitauli.
Aitema 3-1
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ia manatua o le JavaScript plugin e taumafai e filifili le elemene saʻo i mea uma e mafai ona iloa. O le tele o mata'itusi ta'ai va'aia i le taimi e tasi e ono tupu ai ni fa'afitauli.
Aitema 3-2
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Ia manatua o le JavaScript plugin e taumafai e filifili le elemene saʻo i mea uma e mafai ona iloa. O le tele o mata'itusi ta'ai va'aia i le taimi e tasi e ono tupu ai ni fa'afitauli.
<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>
Lisi vaega
E galue foi Scrollspy ma .list-group
s. Fa'asolo le vaega i talaane o le vaega o le lisi ma matamata le suiga o le vasega.
Aitema 1
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Aitema 2
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Aitema 3
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Aitema 4
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
<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>
Taula faigofie
Scrollspy e le gata i vaega nav ma lisi vaega, o lea o le a aoga i soʻo se <a>
elemene taula i le pepa o loʻo iai nei. Fa'asalalau le eria ma matamata le .active
suiga o le vasega.
Aitema 1
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Aitema 2
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Aitema 3
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Aitema 4
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
Aitema 5
O nisi nei o mea e tu'u ai avanoa mo le itulau scrollspy. Manatua a e fa'asolo i lalo le itulau, o lo'o fa'ailoaina le so'otaga fa'atautaiga talafeagai. E toe fai i le fa'ata'ita'iga vaega. Matou te faʻaopoopoina nisi faʻataʻitaʻiga kopi iinei e faʻamamafa ai le taʻavale ma faʻailoga.
<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>
O mea e le vaaia
O elemene fa'atatau e le o va'aia o le a le amana'ia ma o latou mea nav fetaui e le maua se .active
vasega. Scrollspy fa'ata'ita'iga amata i se afifi e le o va'aia o le a le amana'ia uma elemene fa'atatau. Fa'aaoga le refresh
metotia e siaki ai elemene e mata'ituina pe a iloa atu le afifi.
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()
})
})
Fa'aoga
E ala i fa'amaumauga uiga
Ina ia fa'afaigofie ona fa'aopoopo le amio ta'avale i lau ta'avale pito i luga, fa'aopoopo data-bs-spy="scroll"
i le elemene e te mana'o e sipai ai (sili ona masani o le <body>
). Ona faaopoopo lea o le data-bs-target
uiga ma le id
igoa po o le vasega o le matua elemene o soʻo se .nav
vaega 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>
E ala i le JavaScript
const scrollSpy = new bootstrap.ScrollSpy(document.body, {
target: '#navbar-example'
})
Filifiliga
A'o mafai ona pasia filifiliga e ala i fa'amatalaga uiga po'o le JavaScript, e mafai ona e fa'aopoopoina se igoa filifiliga i le data-bs-
, pei o le data-bs-animation="{value}"
. Ia mautinoa e sui le ituaiga mataupu o le igoa filifiliga mai le " CamelCase " i le " kebab-case " pe a pasia filifiliga e ala i faʻamatalaga uiga. Mo se faʻataʻitaʻiga, faʻaaoga data-bs-custom-class="beautifier"
nai lo le data-bs-customClass="beautifier"
.
E pei o le Bootstrap 5.2.0, o vaega uma e lagolagoina se faʻataʻitaʻiga faʻaagaga faʻamaumauga uiga data-bs-config
e mafai ona faʻapipiʻi faigofie vaega faʻapipiʻi o se manoa JSON. Afai ei ai se elemene data-bs-config='{"delay":0, "title":123}'
ma data-bs-title="456"
uiga, o le title
tau mulimuli o le ai ai 456
ma o faʻamatalaga faʻamatalaga uiga ese o le a faʻamalo ai tau o loʻo tuʻuina atu ile data-bs-config
. E le gata i lea, o faʻamaumauga o loʻo i ai nei e mafai ona faʻapipiʻi JSON tau pei o data-bs-delay='{"show":0,"hide":150}'
.
Igoa | Ituaiga | Fa'atonu | Fa'amatalaga |
---|---|---|---|
rootMargin |
manoa | 0px 0px -25% |
Intersection Observer rootMargin iunite aoga, pe a fuafuaina le tulaga o le tusitaai. |
smoothScroll |
boolean | false |
E mafai ai le sologa lelei o le ta'avale pe a kiliki se tagata fa'aoga i luga o se so'oga e fa'asino i le ScrollSpy observables. |
target |
manoa, elemene DOM | null |
Fa'amaoti elemene e fa'aoga ai le Scrollspy plugin. |
threshold |
fa'asologa | [0.1, 0.5, 1] |
IntersectionObserver fa'aoga aoga, pe a fa'atatau le tulaga ta'ai. |
Filifiliga Fa'ate'aina
Se'ia o'o i le v5.1.3 sa matou fa'aogaina offset
& method
filifiliga, lea ua le toe fa'aaogaina ma suia i le rootMargin
. Ina ia tausisia le fetaui i tua, o le a matou faʻaauau pea ona faʻasalalau se tuʻuina atu offset
i le rootMargin
, ae o lenei vaega o le a aveesea i le v6 .
Metotia
Metotia | Fa'amatalaga |
---|---|
dispose |
Fa'aleagaina le ta'avale a se elemene. (Ave'ese fa'amaumauga o lo'o teuina ile elemene DOM) |
getInstance |
Auala fa'amau e maua ai le fa'ata'ita'iga scrollspy e feso'ota'i ma se elemene DOM. |
getOrCreateInstance |
Metotia static e maua ai le scrollspy faʻataʻitaʻiga e fesoʻotaʻi ma se elemene DOM, poʻo le fatuina o se mea fou pe a leʻi amataina. |
refresh |
Pe a faʻaopoopo pe aveese elemene i le DOM, e tatau ona e valaʻau le auala faʻafouina. |
O se faʻataʻitaʻiga lea e faʻaaoga ai le auala faʻafouina:
const dataSpyList = document.querySelectorAll('[data-bs-spy="scroll"]')
dataSpyList.forEach(dataSpyEl => {
bootstrap.ScrollSpy.getInstance(dataSpyEl).refresh()
})
Mea na tutupu
Mea na tupu | Fa'amatalaga |
---|---|
activate.bs.scrollspy |
O lenei mea na tupu e mu i luga o le elemene taʻavale i soo se taimi e faʻagaoioia ai se taula e le scrollspy. |
const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
// do something...
})