Scrollspy
Whakahōu aunoatia te whakaterenga Bootstrap, whakarārangihia rānei ngā wāhanga rōpū i runga i te tūnga panuku hei tohu ko tēhea hononga e hohe ana i te tauranga tirohanga.
Pehea te mahi
Ka takahuri a Scrollspy i te .active
karaehe i runga i te punga ( <a>
) huānga ka panuku te huānga me te id
tohutoro a te punga href
ki te tirohanga. He pai te whakamahi i te Scrollspy me te Bootstrap nav wae , rarangi roopu ranei , engari ka mahi tahi me nga huānga punga o te wharangi o naianei. Anei te mahi.
-
Hei timata, e rua nga mea e hiahiatia ana e scrollspy: he whakatere, he roopu rarangi, he huinga hononga ngawari ranei, me tetahi ipu ka taea te panuku. Ko te ipu ka taea te panuku
<body>
he huānga ritenga me te huingaheight
me teoverflow-y: scroll
. -
I runga i te ipu ka taea te panuku, taapirihia kei
data-bs-spy="scroll"
headata-bs-target="#navId"
tenavId
ahureiid
o te whakatere e hono ana. Me whakauru ano hetabindex="0"
ki te whakarite i te uru papapātuhi. -
I a koe e panuku ana i te ipu "tutaki", ka taapirihia he
.active
karaehe ka tangohia mai i nga hononga punga i roto i te whakatere e hono ana. Ko nga hononga me whaiid
whaainga ka taea te whakatau, ki te kore e arohia. Hei tauira,<a href="#home">home</a>
me rite ki tetahi mea i roto i te DOM rite<div id="home"></div>
-
Ko nga huānga whainga kaore e kitea ka waihotia. Tirohia te waahanga huānga kore e kitea i raro.
Tauira
Navbar
Panuku i te waahi kei raro i te pae whakatere ka mataki i te huringa o te karaehe hohe. Whakatuwheratia te tahua takaiho ka matakitaki i nga mea takaiho kia miramirahia hoki.
Pane tuatahi
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Pane tuarua
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Upoko tuatoru
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Upoko tuawha
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Upoko tuarima
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
<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>
Kohanga nav
Ka mahi ano a Scrollspy me nga kohanga .nav
s. Mena he kohanga .nav
, .active
ka noho ano ona matua .active
. Panukuhia te waahi ki te taha o te pae whakaterenga ka matakitaki i te huringa o te akomanga hohe.
Te nama take 1
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Kia maumahara kei te ngana te mono JavaScript ki te kowhiri i te huānga tika i waenga i nga mea katoa ka kitea. Ko te maha o nga whaainga scrollspy ka kitea i te wa kotahi ka raru pea.
Te nama take 1-1
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Kia maumahara kei te ngana te mono JavaScript ki te kowhiri i te huānga tika i waenga i nga mea katoa ka kitea. Ko te maha o nga whaainga scrollspy ka kitea i te wa kotahi ka raru pea.
Te nama take 1-2
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Kia maumahara kei te ngana te mono JavaScript ki te kowhiri i te huānga tika i waenga i nga mea katoa ka kitea. Ko te maha o nga whaainga scrollspy ka kitea i te wa kotahi ka raru pea.
Te nama take 2
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Kia maumahara kei te ngana te mono JavaScript ki te kowhiri i te huānga tika i waenga i nga mea katoa ka kitea. Ko te maha o nga whaainga scrollspy ka kitea i te wa kotahi ka raru pea.
Te nama take 3
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Kia maumahara kei te ngana te mono JavaScript ki te kowhiri i te huānga tika i waenga i nga mea katoa ka kitea. Ko te maha o nga whaainga scrollspy ka kitea i te wa kotahi ka raru pea.
Te nama take 3-1
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Kia maumahara kei te ngana te mono JavaScript ki te kowhiri i te huānga tika i waenga i nga mea katoa ka kitea. Ko te maha o nga whaainga scrollspy ka kitea i te wa kotahi ka raru pea.
Te nama take 3-2
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Kia maumahara kei te ngana te mono JavaScript ki te kowhiri i te huānga tika i waenga i nga mea katoa ka kitea. Ko te maha o nga whaainga scrollspy ka kitea i te wa kotahi ka raru pea.
<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>
Rōpū rārangi
Ka mahi ano a Scrollspy me .list-group
s. Panukuhia te waahi ki te taha o te roopu rarangi ka matakitaki i te huringa o te akomanga kaha.
Te nama take 1
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Te nama take 2
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Te nama take 3
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Te nama take 4
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
<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>
Nga punga ngawari
Kaore a Scrollspy i whakawhäitihia ki nga waahanga nav me nga roopu rarangi, na reira ka mahi i runga i nga <a>
waahanga punga o te tuhinga o naianei. Panuku i te waahi ka matakitaki i te .active
huringa o te akomanga.
Te nama take 1
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Te nama take 2
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Te nama take 3
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Te nama take 4
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
Te nama take 5
Ko etahi ihirangi puri wahi tenei mo te wharangi scrollspy. Kia mahara ka panuku koe ki raro i te wharangi, ka tohuhia te hononga whakatere tika. Ka tukuruatia puta noa i te tauira waahanga. Ka taapirihia e matou etahi tauira tauira ki konei hei whakanui i te panuku me te miramira.
<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>
Nga huānga kore e kitea
Ko nga huānga whainga kare e kitea ka waihotia, ka kore e whiwhi .active
karaehe o ratou tuemi nav. Ko nga tauira Scrollspy i arawhitihia i roto i te takai kore-kitea ka warewarehia nga huānga e whaaia ana. Whakamahia te refresh
tikanga ki te tirotiro mo nga huānga ka kitea ka kitea te takai.
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()
})
})
Whakamahinga
Ma nga huanga raraunga
Kia ngawari te taapiri i te whanonga scrollspy ki to whakatere pae runga, taapiri atu data-bs-spy="scroll"
ki te huānga e hiahia ana koe ki te tutei (te nuinga ko te <body>
). Na ka taapirihia te data-bs-target
huanga me te id
ingoa akomanga ranei o te huānga matua o tetahi waahanga 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>
Ma te JavaScript
const scrollSpy = new bootstrap.ScrollSpy(document.body, {
target: '#navbar-example'
})
Kōwhiringa
I te mea ka tukuna nga whiringa ma nga huanga raraunga, JavaScript ranei, ka taea e koe te taapiri i tetahi ingoa whiringa ki data-bs-
, penei i te data-bs-animation="{value}"
. Kia mohio koe ki te whakarereke i te momo keehi o te ingoa whiringa mai i te " CamelCase " ki te " kebab-case " ka tukuna nga whiringa ma nga huanga raraunga. Hei tauira, whakamahia data-bs-custom-class="beautifier"
hei utu mo data-bs-customClass="beautifier"
.
Mai i te Bootstrap 5.2.0, ka tautoko nga waahanga katoa i tetahi huanga raraunga kua rahuitia hei whakamatautaudata-bs-config
ka taea te whakanoho i te whirihoranga waahanga ngawari hei aho JSON. Ina he huānga data-bs-config='{"delay":0, "title":123}'
me data-bs-title="456"
nga huanga, ko te title
uara whakamutunga 456
ko nga huanga raraunga motuhake ka takahia nga uara kua hoatu ki runga data-bs-config
. I tua atu, ka taea e nga huanga raraunga o mua te whakanoho i nga uara JSON penei i te data-bs-delay='{"show":0,"hide":150}'
.
Ingoa | Momo | Taunoa | Whakaahuatanga |
---|---|---|---|
rootMargin |
aho | 0px 0px -25% |
Intersection Observer rootMargin wae tika, i te tatau i te tuunga panuku. |
smoothScroll |
boolean | false |
Ka taea te panuku maeneene ina pao te kaiwhakamahi ki te hono e tohu ana ki nga tirohanga a ScrollSpy. |
target |
aho, huānga DOM | null |
Tautuhi huānga ki te tono Scrollspy mono. |
threshold |
huinga | [0.1, 0.5, 1] |
IntersectionObserver paepae whakauru whaimana, i te tatau i te tuunga panuku. |
Nga Kōwhiringa Whakakore
Tae noa ki te v5.1.3 kei te whakamahi matou offset
me method
nga whiringa, kua whakakorea inaianei ka whakakapihia e rootMargin
. Kia mau tonu te hototahi whakamuri, ka wetewete tonu tatou i tetahi kua hoatu offset
ki te rootMargin
, engari ka tangohia tenei ahuatanga i roto i te v6 .
Nga tikanga
Tikanga | Whakaahuatanga |
---|---|
dispose |
Ka whakangaro i te panuku o tetahi huānga. (Tangohia ai nga raraunga rongoa i te huānga DOM) |
getInstance |
Tikanga pateko hei tiki i te tauira scrollspy e hono ana ki tetahi huānga DOM. |
getOrCreateInstance |
Tikanga pateko hei tiki i te tauira scrollspy e hono ana ki tetahi huānga DOM, ki te hanga i tetahi mea hou mena kaore i arawhitia. |
refresh |
I te taapiri, te tango ranei i nga huānga o te DOM, me waea koe ki te tikanga whakahou. |
Anei tetahi tauira e whakamahi ana i te tikanga whakahou:
const dataSpyList = document.querySelectorAll('[data-bs-spy="scroll"]')
dataSpyList.forEach(dataSpyEl => {
bootstrap.ScrollSpy.getInstance(dataSpyEl).refresh()
})
Nga huihuinga
Takahanga | Whakaahuatanga |
---|---|
activate.bs.scrollspy |
Ka puhipuhi tenei takahanga ki te huānga panuku i nga wa katoa ka whakahohehia te punga e te scrollspy. |
const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => {
// do something...
})