Kaw
Toggle qhov pom ntawm cov ntsiab lus thoob plaws koj qhov project nrog ob peb chav kawm thiab peb cov JavaScript plugins.
Nws ua haujlwm li cas
Lub cev qhuav dej JavaScript plugin yog siv los qhia thiab zais cov ntsiab lus. Cov nyees khawm lossis cov thauj tog rau nkoj yog siv los ua cov cim uas tau teeb tsa rau cov ntsiab lus tshwj xeeb uas koj hloov pauv. Collapsing ib lub caij yuav animate height
los ntawm nws cov nqi tam sim no rau 0
. Muab CSS ua haujlwm li cas rau cov animations, koj siv tsis tau padding
ntawm ib lub .collapse
caij. Hloov chaw, siv cov chav kawm ua ib qho kev nthuav dav ntawm cov ntaub qhwv.
prefers-reduced-motion
lus nug xov xwm. Saib qhov
txo qis ntawm peb cov ntaub ntawv nkag mus tau .
Piv txwv
Nyem rau cov nyees khawm hauv qab no los qhia thiab zais lwm lub ntsiab lus ntawm kev hloov hauv chav kawm:
.collapse
zais cov ntsiab lus.collapsing
yog siv thaum lub sij hawm hloov.collapse.show
qhia cov ntsiab lus
Feem ntau, peb pom zoo kom siv lub pob nrog tus data-bs-target
cwj pwm. Thaum tsis pom zoo los ntawm qhov kev xav ntawm qhov pom, koj tuaj yeem siv qhov txuas nrog tus href
cwj pwm (thiab a role="button"
). Hauv ob qho xwm txheej, qhov data-bs-toggle="collapse"
yuav tsum tau ua.
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Button with data-bs-target
</button>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
Kab rov tav
Lub collapse plugin kuj txhawb txoj kab rov tav collapsing. Ntxiv cov .collapse-horizontal
chav kawm hloov kho kom hloov pauv qhov hloov pauv width
thiab height
teeb tsa tus width
me nyuam tam sim ntawd. Xav tias dawb sau koj tus kheej kev cai Sass, siv inline styles, lossis siv peb cov khoom siv dav dav .
min-height
txheej txheem kom tsis txhob rov ua dua ntau dhau hauv peb cov ntaub ntawv, qhov no tsis tas yuav tsum tau hais meej.
Tsuas yog width
tus me nyuam lub ntsiab yog yuav tsum tau.
<p>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseWidthExample" aria-expanded="false" aria-controls="collapseWidthExample">
Toggle width collapse
</button>
</p>
<div style="min-height: 120px;">
<div class="collapse collapse-horizontal" id="collapseWidthExample">
<div class="card card-body" style="width: 300px;">
This is some placeholder content for a horizontal collapse. It's hidden by default and shown when triggered.
</div>
</div>
</div>
Ntau lub hom phiaj
Ib <button>
lossis <a>
tuaj yeem qhia thiab zais ntau lub ntsiab lus los ntawm kev xa lawv nrog tus xaiv hauv nws href
lossis data-bs-target
tus cwj pwm. Ntau <button>
lossis <a>
tuaj yeem qhia thiab zais ib lub ntsiab lus yog tias lawv txhua tus siv nrog lawv href
lossis data-bs-target
tus cwj pwm
<p>
<a class="btn btn-primary" data-bs-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</a>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target=".multi-collapse" aria-expanded="false" aria-controls="multiCollapseExample1 multiCollapseExample2">Toggle both elements</button>
</p>
<div class="row">
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample1">
<div class="card card-body">
Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample2">
<div class="card card-body">
Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>
Kev siv tau
Nco ntsoov ntxiv aria-expanded
rau lub caij tswj. Cov cwj pwm no qhia meej meej txog lub xeev tam sim no ntawm lub caij sib tsoo khi rau kev tswj kom tshuaj xyuas cov nyeem thiab cov cuab yeej zoo sib xws. Yog tias lub caij collapsible raug kaw los ntawm lub neej ntawd, tus cwj pwm ntawm cov khoom tswj yuav tsum muaj tus nqi ntawm aria-expanded="false"
. Yog tias koj tau teeb tsa lub caij collapsible kom qhib los ntawm lub neej ntawd siv cov show
chav kawm, teem caij aria-expanded="true"
rau kev tswj hwm. Lub plugin yuav cia li toggle tus cwj pwm no ntawm kev tswj xyuas raws li seb puas tau qhib lossis kaw (ntawm JavaScript, lossis vim tias tus neeg siv tau ua rau lwm qhov kev tswj hwm tseem khi rau tib lub caij sib tsoo). Yog hais tias tus tswj hwm lub ntsiab HTML tsis yog khawm (piv txwv li, ib qho <a>
lossis <div>
), tus cwj pwmrole="button"
yuav tsum tau ntxiv rau lub caij.
Yog tias koj lub ntsiab tswj yog tsom rau ib qho kev sib tsoo - piv txwv li tus data-bs-target
cwj pwm yog taw tes rau tus id
xaiv - koj yuav tsum ntxiv tus aria-controls
cwj pwm rau cov khoom tswj, uas muaj cov id
khoom sib tsoo. Cov ntawv nyeem niaj hnub no thiab cov thev naus laus zis zoo sib xws ua rau siv cov cwj pwm no los muab cov neeg siv nrog cov shortcuts ntxiv rau kev taw qhia ncaj qha rau lub caij sib tsoo nws tus kheej.
Nco ntsoov tias Bootstrap qhov kev siv tam sim no tsis suav nrog ntau yam kev xaiv keyboard sib cuam tshuam tau piav qhia hauv ARIA Authoring Practices Guide accordion pattern - koj yuav tsum suav nrog koj tus kheej nrog kev cai JavaScript.
Sass
Hloov pauv
$transition-collapse: height .35s ease;
$transition-collapse-width: width .35s ease;
Cov chav kawm
Collapse hloov cov chav kawm tuaj yeem pom nyob rau hauv scss/_transitions.scss
raws li cov no tau muab faib thoob plaws ntau yam (collapse thiab accordion).
.collapse {
&:not(.show) {
display: none;
}
}
.collapsing {
height: 0;
overflow: hidden;
@include transition($transition-collapse);
&.collapse-horizontal {
width: 0;
height: auto;
@include transition($transition-collapse-width);
}
}
Kev siv
Lub cev qhuav dej siv ob peb chav kawm los daws qhov hnyav nqa:
.collapse
zais cov ntsiab lus.collapse.show
qhia cov ntsiab lus.collapsing
ntxiv thaum qhov kev hloov pauv pib, thiab tshem tawm thaum nws tiav
Cov chav kawm no tuaj yeem pom hauv _transitions.scss
.
Los ntawm cov ntaub ntawv attributes
Tsuas yog ntxiv data-bs-toggle="collapse"
thiab ib qho data-bs-target
rau lub caij kom tau txais kev tswj hwm ntawm ib lossis ntau lub ntsiab lus collapsible. Tus data-bs-target
cwj pwm lees txais CSS xaiv los siv lub cev qhuav dej rau. Nco ntsoov ntxiv cov chav kawm collapse
rau lub caij collapsible. Yog tias koj xav kom nws qhib lub neej ntawd, ntxiv cov chav kawm ntxiv show
.
Txhawm rau ntxiv accordion-zoo li pab pawg tswj hwm rau thaj tsam collapsible, ntxiv cov ntaub ntawv tus cwj pwm data-bs-parent="#selector"
. Xa mus rau nplooj ntawv accordion kom paub ntau ntxiv.
Ntawm JavaScript
Qhib manually nrog:
const collapseElementList = document.querySelectorAll('.collapse')
const collapseList = [...collapseElementList].map(collapseEl => new bootstrap.Collapse(collapseEl))
Kev xaiv
Raws li kev xaiv tuaj yeem dhau los ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript, koj tuaj yeem ntxiv ib qho kev xaiv npe rau data-bs-
, xws li hauv data-bs-animation="{value}"
. Nco ntsoov hloov cov ntaub ntawv ntawm lub npe xaiv los ntawm " camelCase " mus rau " kebab-case " thaum dhau cov kev xaiv ntawm cov ntaub ntawv cwj pwm. Piv txwv li, siv data-bs-custom-class="beautifier"
es tsis txhob data-bs-customClass="beautifier"
.
Raws li ntawm Bootstrap 5.2.0, tag nrho cov khoom txhawb nqa qhov kev sim khaws tseg cov ntaub ntawv tus cwj pwm data-bs-config
uas tuaj yeem ua tsev yooj yim kev teeb tsa raws li JSON txoj hlua. Thaum ib qho khoom muaj data-bs-config='{"delay":0, "title":123}'
thiab data-bs-title="456"
tus cwj pwm, tus title
nqi kawg yuav yog 456
thiab cov ntaub ntawv sib cais yuav dhau los ntawm cov txiaj ntsig tau muab rau data-bs-config
. Ntxiv mus, cov ntaub ntawv uas twb muaj lawm muaj peev xwm ua tsev JSON qhov tseem ceeb xws li data-bs-delay='{"show":0,"hide":150}'
.
Lub npe | Hom | Default | Kev piav qhia |
---|---|---|---|
parent |
selector, DOM element | null |
Yog tias niam txiv muab, ces tag nrho cov ntsiab lus collapsible nyob rau hauv cov niam txiv uas tau teev tseg yuav raug kaw thaum cov khoom sib tsoo no tshwm sim. (zoo ib yam li kev coj cwj pwm accordion - qhov no yog nyob ntawm card chav kawm). Tus cwj pwm yuav tsum tau teem rau ntawm lub hom phiaj collapsible. |
toggle |
boolean | true |
Toggles lub caij collapsible ntawm invocation. |
Cov txheej txheem
Asynchronous txoj kev thiab kev hloov
Txhua txoj kev API yog asynchronous thiab pib hloov pauv . Lawv rov qab mus rau tus neeg hu xov tooj sai li sai tau thaum qhov kev hloov pauv pib tab sis ua ntej nws xaus . Tsis tas li ntawd, ib txoj kev hu rau ib qho kev hloov pauv yuav raug tsis quav ntsej .
Activates koj cov ntsiab lus raws li ib tug collapsible caij. Txais ib qho kev xaiv xaiv object
.
Koj tuaj yeem tsim qhov sib tsoo nrog tus tsim, piv txwv li:
const bsCollapse = new bootstrap.Collapse('#myCollapse', {
toggle: false
})
Txoj kev | Kev piav qhia |
---|---|
dispose |
Destroys ib lub caij poob. (Tshem cov ntaub ntawv khaws cia ntawm DOM lub caij) |
getInstance |
Txoj kev zoo li qub uas tso cai rau koj kom tau txais qhov tawg piv txwv cuam tshuam rau DOM lub caij, koj tuaj yeem siv nws zoo li no: bootstrap.Collapse.getInstance(element) . |
getOrCreateInstance |
Txoj kev zoo li qub uas xa rov qab qhov piv txwv sib tsoo nrog rau DOM lub caij lossis tsim ib qho tshiab yog tias nws tsis tau pib. Koj tuaj yeem siv nws li no: bootstrap.Collapse.getOrCreateInstance(element) . |
hide |
Hide ib lub caij collapsible. Rov qab mus rau tus neeg hu ua ntej lub caij collapsible tau muab zais tiag tiag (xws li, ua ntej qhov hidden.bs.collapse xwm txheej tshwm sim). |
show |
Qhia ib lub caij collapsible. Rov qab mus rau tus neeg hu ua ntej lub caij collapsible tau tshwm sim tiag tiag (piv txwv li, ua ntej qhov shown.bs.collapse xwm txheej tshwm sim). |
toggle |
Toggles lub caij collapsible kom pom los yog zais. Rov qab mus rau tus neeg hu ua ntej lub caij collapsible tau tshwm sim los yog zais (piv txwv li ua ntej qhov shown.bs.collapse kev hidden.bs.collapse tshwm sim tshwm sim). |
Cov xwm txheej
Bootstrap's collapse class exposes ob peb txheej xwm rau hooking mus rau hauv vau functionality.
Hom xwm txheej | Kev piav qhia |
---|---|
hide.bs.collapse |
Qhov kev tshwm sim no raug rho tawm haujlwm tam sim ntawd thaum hide hu ua txoj kev. |
hidden.bs.collapse |
Qhov xwm txheej no raug rho tawm haujlwm thaum lub caij sib tsoo tau muab zais los ntawm tus neeg siv (yuav tos rau CSS kev hloov pauv kom tiav). |
show.bs.collapse |
Qhov xwm txheej no tua hluav taws tam sim ntawd thaum show hu ua piv txwv. |
shown.bs.collapse |
Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub caij sib tsoo tau ua kom pom rau tus neeg siv (yuav tos rau CSS kev hloov pauv kom tiav). |
const myCollapsible = document.getElementById('myCollapsible')
myCollapsible.addEventListener('hidden.bs.collapse', event => {
// do something...
})