Ukuwa
Guqula ukubonakala komxholo kuyo yonke iprojekthi yakho ngeeklasi ezimbalwa kunye neeplagi zethu zeJavaScript.
Ingaba isebenza kanjani
Iplagi yeJavaScript yokudilika isetyenziselwa ukubonisa nokufihla umxholo. Amaqhosha okanye iiankile zisetyenziswa njengezinto ezibangela ukuba zenziwe kwimaphu kwizinto ezithile oziguqulayo. Ukuthoba isiqalelo kuyakuphilisa ixabiso height
layo langoku ukuya kwi 0
. Unikwe indlela iCSS ephatha ngayo oopopayi, awukwazi ukusebenzisa padding
into .collapse
ethile. Endaweni yoko, sebenzisa iklasi njengento ezimeleyo yokusonga.
prefers-reduced-motion
kumbuzo wemidiya. Jonga icandelo
leentshukumo ezincitshisiweyo kuxwebhu lwethu lofikelelo .
Umzekelo
Cofa amaqhosha angezantsi ukuze ubonise kwaye ufihle enye into ngotshintsho lweklasi:
.collapse
ifihla umxholo.collapsing
isetyenziswa ngexesha lotshintsho.collapse.show
ibonisa umxholo
Ngokuqhelekileyo, sincoma ukusebenzisa iqhosha data-bs-target
elinophawu. Ngelixa ungacetyiswanga ukusuka kwindawo yokujonga yesemantic, ungasebenzisa ikhonkco kunye href
nophawu (kunye ne role="button"
). Kuzo zombini ezi meko, data-bs-toggle="collapse"
kuyafuneka.
<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>
Ukuthe tye
Iplagin yokudilika ikwaxhasa ukudilika okuthe tye. Yongeza .collapse-horizontal
udidi lwesilungisi kwinguqu width
endaweni height
kwaye usete u a width
kwinto yomntwana kwangoko. Zive ukhululekile ukubhala eyakho iSass yesiko, sebenzisa izitayile ezingaphakathi, okanye usebenzise izixhobo zethu zobubanzi .
min-height
uneseti yokuphepha ukupeyintwa kwakhona okugqithisileyo kumaxwebhu ethu, oku akufuneki ngokucacileyo.
Kuphela width
into efunekayo kumntwana.
<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>
Iithagethi ezininzi
I-A <button>
okanye <a>
ingabonisa kwaye ifihle izinto ezininzi ngokuzibhekisa kumkhethi kuyo href
okanye data-bs-target
kuphawu. Zininzi <button>
okanye <a>
zinokubonisa kwaye zifihle into ukuba nganye ibhekisa kuyo href
kunye data-bs-target
nophawu
<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>
Ukufikeleleka
Qinisekisa ukuba wongeza aria-expanded
kwinto yolawulo. Olu phawu lubonisa ngokucacileyo imeko yangoku yento eqengqelekayo ebotshelelwe kulawulo kwizifundi zesikrini kunye nobuchwephesha obuncedisayo obufanayo. Ukuba isiqalelo esiqengqelekayo sivalwe ngokungagqibekanga, uphawu loyelelwano lwento yolawulo kufuneka lube nexabiso aria-expanded="false"
. Ukuba usete into eqengqelekayo ukuba ivuleke ngokungagqibekanga usebenzisa show
iklasi, misela aria-expanded="true"
kulawulo endaweni yoko. Iplagi iya kuzitshintsha ngokuzenzekela olu phawu kulawulo olusekwe ekubeni ngaba into eqengqelekayo ivuliwe okanye ayivalwanga (ngeJavaScript, okanye kuba umsebenzisi eqalise enye into yolawulo nayo ibotshelelwe kwinto enye eqengqelekayo). Ukuba isiqalelo solawulo se HTML asiloqhosha (umzekelo, i <a>
okanye <div>
), uphawurole="button"
kufuneka yongezwe kwisiqalelo.
Ukuba isiqalelo sakho solawulo sijolise kwinto enye eqengqelekayo - oko kukuthi data-bs-target
uphawu lwalatha id
kumkhethi - kufuneka wongeze aria-controls
uphawu lwento yolawulo, equlathe id
into eqengqelekayo. Izifundi zesikrini zanamhlanje kunye neetekhnoloji ezincedisayo ezifanayo zisebenzisa olu phawu ukunika abasebenzisi iindlela ezimfutshane ukuya ngqo kwinto egobekayo ngokwayo.
Qaphela ukuba uphumezo lwangoku lweBootstrap aluquki udibaniso olukhethiweyo lwebhodi yezitshixo echazwe kwi- ARIA yoBugunyaziso bokuSebenza ipateni yeakhordion - kuya kufuneka uzibandakanye ngokwakho kunye neJavaScript yesiko.
Sass
Izinto eziguquguqukayo
$transition-collapse: height .35s ease;
$transition-collapse-width: width .35s ease;
Iiklasi
Ukugoqa kweeklasi zenguqu kunokufunyanwa scss/_transitions.scss
njengoko ezi kwabelwana ngazo kumacandelo amaninzi (ukuwa kunye ne-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);
}
}
Ukusetyenziswa
Iplagin yokuwa isebenzisa iiklasi ezimbalwa ukuphatha ukuphakamisa okunzima:
.collapse
ifihla umxholo.collapse.show
ibonisa umxholo.collapsing
yongezwa xa inguqu iqala, kwaye isuswe xa igqibile
Ezi klasi zinokufumaneka kwi _transitions.scss
.
Ngeempawu zedatha
Yongeza nje data-bs-toggle="collapse"
kunye no data-bs-target
-a kwisiqalelo ukwabela ngokuzenzekelayo ulawulo lwento enye okanye ezininzi eziqengqelekayo. Uphawu data-bs-target
loyelelwano lwamkela umkhethi we-CSS ukuze kufakwe ukutshitshiswa. Qinisekisa ukuba wongeza iklasi collapse
kwinto eqengqelekayo. Ukuba ungathanda ukuba ivuleke ngokungagqibekanga, yongeza udidi olongezelelweyo show
.
Ukongeza ulawulo lweqela olufana ne-accordion kwindawo egotywayo, yongeza uphawu loyelelwano lwedatha data-bs-parent="#selector"
. Jonga kwiphepha leaccordion ngolwazi oluthe kratya.
NgeJavaScript
Vula ngesandla nge:
const collapseElementList = document.querySelectorAll('.collapse')
const collapseList = [...collapseElementList].map(collapseEl => new bootstrap.Collapse(collapseEl))
Iinketho
Njengoko iinketho zinokugqithiswa ngeempawu zedatha okanye iJavaScript, unokongeza igama lokhetho ku data-bs-
, njengakwi data-bs-animation="{value}"
. Qinisekisa ukuba utshintshe uhlobo lwecala legama lokukhetha ukusuka kwi " camelCase " ukuya kwi " kebab-case " xa udlula iinketho ngeempawu zedatha. Umzekelo, sebenzisa data-bs-custom-class="beautifier"
endaweni ye- data-bs-customClass="beautifier"
.
Ukusukela kwi-Bootstrap 5.2.0, onke amacandelo axhasa umlinganiso wedatha ogciniweyo wovavanyodata-bs-config
onokubeka uqwalaselo lwecandelo olulula njengomtya we-JSON. Xa into ineempawu data-bs-config='{"delay":0, "title":123}'
kunye data-bs-title="456"
neempawu, ixabiso lokugqibela title
liya kuba 456
kunye neempawu zedatha eyahlukileyo ziya kuwenza ngaphezulu amaxabiso anikiweyo data-bs-config
. Ukongeza, iimpawu zedatha ezikhoyo ziyakwazi ukubeka ixabiso le-JSON njenge data-bs-delay='{"show":0,"hide":150}'
.
Igama | Uhlobo | Ukuhlala kukho | Inkcazo |
---|---|---|---|
parent |
umkhethi, isiqalelo se-DOM | null |
Ukuba umzali unikiwe, ke zonke izinto eziqengqelekayo phantsi komzali ochaziweyo ziya kuvalwa xa le nto iqengqelekayo iboniswa. (efana nokuziphatha kwe-accordion yendabuko - oku kuxhomekeke card kwiklasi). Uphawu kufuneka lubekwe kwindawo ekujoliswe kuyo egotywayo. |
toggle |
boolean | true |
Qwalasela into eqengqelekayo kubizo. |
Iindlela
Iindlela ze-Asynchronous kunye notshintsho
Zonke iindlela ze-API zi -asynchronous kwaye ziqala utshintsho . Babuyela kumnxeba ngokukhawuleza nje ukuba utshintsho luqalisiwe kodwa ngaphambi kokuba luphele . Ukongeza, ukufowunelwa kwendlela kwicandelo lenguquko kuya kuhoywa .
Yenza umxholo wakho ube yinto eqengqelekayo. Yamkela iinketho ozikhethelayo object
.
Unokwenza umzekelo wokudilika kunye nomakhi, umzekelo:
const bsCollapse = new bootstrap.Collapse('#myCollapse', {
toggle: false
})
Indlela | Inkcazo |
---|---|
dispose |
Itshabalalisa ukuwa kwento. (Isusa idatha egciniweyo kwisiqalelo seDOM) |
getInstance |
Indlela engatshintshiyo ekuvumela ukuba ufumane umzekelo wokudilika onxulunyaniswa nento yeDOM, ungayisebenzisa ngolu hlobo: bootstrap.Collapse.getInstance(element) . |
getOrCreateInstance |
Indlela engatshintshiyo ebuyisela umzekelo wokudilika onxulunyaniswa nento yeDOM okanye yenze entsha ukuba ayikhange iqalwe. Ungayisebenzisa ngolu hlobo bootstrap.Collapse.getOrCreateInstance(element) :. |
hide |
Ifihla into eqengqelekayo. Ibuyisela kumnxebi phambi kokuba into eqengqelekayo ifihliwe (umzekelo, phambi kokuba hidden.bs.collapse isiganeko senzeke). |
show |
Ibonisa into eqengqelekayo. Ibuyisela kumnxeba phambi kokuba into eqengqelekayo iboniswe (umzekelo, phambi kokuba shown.bs.collapse isiganeko senzeke). |
toggle |
Iguqulela into eqengqelekayo ukuze iboniswe okanye ifihliwe. Ibuyisela kumnxeba phambi kokuba into eqengqelekayo iboniswe okanye ifihliwe (okt phambi kokuba isiganeko shown.bs.collapse okanye hidden.bs.collapse isiganeko senzeke). |
Iziganeko
Iklasi yokuwa yeBootstrap iveza iminyhadala embalwa yokuhuka ekusebenzeni kokuwa.
Uhlobo lomsitho | Inkcazo |
---|---|
hide.bs.collapse |
Esi siganeko sigxothwa ngokukhawuleza xa hide indlela sele ibizwa. |
hidden.bs.collapse |
Esi siganeko sigxothwa xa into yokudilika ifihliwe kumsebenzisi (iya kulinda iinguqu zeCSS ukuba zigqibe). |
show.bs.collapse |
Esi siganeko sivutha ngokukhawuleza xa show kubizwa indlela yomzekelo. |
shown.bs.collapse |
Esi siganeko sigxothwa xa into yokudilika yenziwe yabonakala kumsebenzisi (iya kulinda iinguqu zeCSS ukuba zigqibe). |
const myCollapsible = document.getElementById('myCollapsible')
myCollapsible.addEventListener('hidden.bs.collapse', event => {
// do something...
})