Collapse
Shandura kuoneka kwezviri mukati meprojekiti yako nemakirasi mashoma uye yedu JavaScript plugins.
Zvinoshanda sei
Iyo yekuputsika JavaScript plugin inoshandiswa kuratidza nekuviga zvirimo. Mabhatani kana zvibatiso zvinoshandiswa sezvinokonzeresa zvakamisikidzwa kune chaiwo zvinhu zvaunoshandura. Kupeta chinhu kuno raramisa height
kubva pakukosha kwayo kuenda ku 0
. Tichifunga kuti CSS inobata sei mifananidzo, haugone kushandisa padding
pane .collapse
chinhu. Pane kudaro, shandisa kirasi sechinhu chakazvimirira chekuputira chinhu.
prefers-reduced-motion
nemubvunzo wenhau. Ona chikamu
chakaderedzwa chezvinyorwa zvedu zvekusvikika .
Muenzaniso
Dzvanya mabhatani ari pazasi kuratidza uye kuviga chimwe chinhu kuburikidza nekuchinja kwekirasi:
.collapse
anovanza zviri mukati.collapsing
inoshandiswa panguva yekuchinja.collapse.show
inoratidza zvirimo
Kazhinji, tinokurudzira kushandisa bhatani rine data-bs-target
hunhu. Kunyange zvisingakurudzirwe kubva pakuona semantic, unogona zvakare kushandisa chinongedzo chine href
hunhu (uye a role="button"
). Muzviitiko zvose izvi, data-bs-toggle="collapse"
zvinodiwa.
<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>
Horizontal
Iyo yekudonha plugin zvakare inotsigira yakachinjika kudonha. Wedzera iyo .collapse-horizontal
modifier kirasi kuti uchinje iyo width
pachinzvimbo height
uye isa width
pane yezvino mwana chinhu. Inzwa wakasununguka kunyora yako wega tsika Sass, shandisa inline masitaera, kana kushandisa yedu yehupamhi zvishandiso .
min-height
seti yekudzivirira yakawandisa repaint mumagwaro edu, izvi hazvidiwe zvakajeka.
Chete width
pane chinhu chemwana chinodiwa.
<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>
Zvinangwa zvakawanda
A <button>
kana <a>
inogona kuratidza uye kuvanza zvinhu zvakawanda nekuzvirevera neanosarudza mune ayo href
kana data-bs-target
hunhu. Yakawanda <button>
kana <a>
inogona kuratidza uye kuvanza chinhu kana mumwe nemumwe akachinongedzera nechavo href
kana data-bs-target
hunhu
<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>
Accessibility
Iva nechokwadi chekuwedzera aria-expanded
kune chekutonga chinhu. Uhwu hunhu hunoburitsa zviripachena mamiriro azvino echinhu chinokorokoka chakasungirirwa pakutonga kuvaverengi vezvikirini uye mamwe matekinoroji ekubatsira. Kana iyo chinhu chinoputika chakavharwa nekusagadzika, hunhu huri pane chekutonga hunofanirwa kuve nehukoshi hwe aria-expanded="false"
. Kana iwe wakaseta chinhu chinoputika kuti chivhurwe nekusarudzika uchishandisa show
kirasi, isa aria-expanded="true"
pane control pane. Iyo plugin inozochinja otomatiki hunhu uhu pahutongi zvichibva pakuti kana chinhu chinokorokoka chavhurwa kana kuvharwa (kuburikidza neJavaScript, kana nekuti mushandisi akonzeresa chimwe chinhu chekudzora chakasungirirwa kune chimwe chinhu chinoputika). Kana iyo control element's HTML element isiri bhatani (semuenzaniso, an <a>
kana <div>
), hunhurole="button"
inofanira kuwedzerwa kune element.
Kana chinhu chako chekutonga chiri kunanga chinhu chimwe chinoputsika - kureva kuti data-bs-target
hunhu huri kunongedza kune imwe id
sarudzo - iwe unofanirwa kuwedzera aria-controls
hunhu kuchinhu chekutonga, chine id
chechinhu chinodonha. Vaverengi vemazuva ano skrini uye matekinoroji ekubatsira akafanana anoshandisa hunhu uhwu kupa vashandisi mamwe mapfupi ekufamba akananga kuchinhu chinoputika pachacho.
Ziva kuti kuita kwazvino kweBootstrap hakuvharidzire mashandisirwo ekiibhodhi akasiyana-siyana anotsanangurwa muARIA Authoring Practices Guide accordion pateni - unozofanirwa kusanganisa iwe pachako neJavaScript.
Sass
Variables
$transition-collapse: height .35s ease;
$transition-collapse-width: width .35s ease;
Makirasi
Kupunza makirasi eshanduko anogona kuwanikwa mukati scss/_transitions.scss
sezvo aya akagovaniswa muzvikamu zvakawanda (kudonha uye 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);
}
}
Usage
Iyo yekudonha plugin inoshandisa mashoma makirasi kubata inorema kusimudza:
.collapse
inovanza zviri mukati.collapse.show
inoratidza zviri mukati.collapsing
inowedzerwa kana shanduko yatanga, uye inobviswa kana yapera
Aya makirasi anogona kuwanikwa mu _transitions.scss
.
Via data hunhu
Ingo wedzera data-bs-toggle="collapse"
uye a data-bs-target
kune chinhu kuti ugozvigadzike kutonga kwechimwe kana kupfuura chinodonha zvinhu. Hunhu data-bs-target
hunogamuchira CSS selector yekushandisa iyo kuputsika. Iva nechokwadi chekuwedzera kirasi collapse
kune chinhu chinoputika. Kana uchida kuti risavhurika, wedzera imwe kirasi show
.
Kuwedzera accordion-senge boka manejimendi munzvimbo inoputsika, wedzera iyo data hunhu data-bs-parent="#selector"
. Tarisa kune accordion peji kuti uwane rumwe ruzivo.
Via JavaScript
Vhura nemaoko ne:
const collapseElementList = document.querySelectorAll('.collapse')
const collapseList = [...collapseElementList].map(collapseEl => new bootstrap.Collapse(collapseEl))
Options
Sezvo sarudzo dzichigona kupfuudzwa kuburikidza nedata hunhu kana JavaScript, unogona kuisa sarudzo zita ku data-bs-
, semu data-bs-animation="{value}"
. Ita shuwa yekushandura mhando yekesi yezita resarudzo kubva ku " camelCase " kuenda ku " kebab-case " kana uchipfuura sarudzo kuburikidza ne data hunhu. Somuenzaniso, shandisa data-bs-custom-class="beautifier"
pane data-bs-customClass="beautifier"
.
As of Bootstrap 5.2.0, all components support an experimental reserved data attribute data-bs-config
that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}'
and data-bs-title="456"
attributes, the final title
value will be 456
and the separate data attributes will override values given on data-bs-config
. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'
.
Name | Type | Default | Description |
---|---|---|---|
parent |
selector, DOM element | null |
If parent is provided, then all collapsible elements under the specified parent will be closed when this collapsible item is shown. (similar to traditional accordion behavior - this is dependent on the card class). The attribute has to be set on the target collapsible area. |
toggle |
boolean | true |
Toggles the collapsible element on invocation. |
Methods
Asynchronous methods and transitions
All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.
Activates your content as a collapsible element. Accepts an optional options object
.
You can create a collapse instance with the constructor, for example:
const bsCollapse = new bootstrap.Collapse('#myCollapse', {
toggle: false
})
Method | Description |
---|---|
dispose |
Destroys an element’s collapse. (Removes stored data on the DOM element) |
getInstance |
Static method which allows you to get the collapse instance associated to a DOM element, you can use it like this: bootstrap.Collapse.getInstance(element) . |
getOrCreateInstance |
Static method which returns a collapse instance associated to a DOM element or create a new one in case it wasn’t initialized. You can use it like this: bootstrap.Collapse.getOrCreateInstance(element) . |
hide |
Hides a collapsible element. Returns to the caller before the collapsible element has actually been hidden (e.g., before the hidden.bs.collapse event occurs). |
show |
Shows a collapsible element. Returns to the caller before the collapsible element has actually been shown (e.g., before the shown.bs.collapse event occurs). |
toggle |
Toggles a collapsible element to shown or hidden. Returns to the caller before the collapsible element has actually been shown or hidden (i.e. before the shown.bs.collapse or hidden.bs.collapse event occurs). |
Events
Bootstrap’s collapse class exposes a few events for hooking into collapse functionality.
Event type | Description |
---|---|
hide.bs.collapse |
This event is fired immediately when the hide method has been called. |
hidden.bs.collapse |
This event is fired when a collapse element has been hidden from the user (will wait for CSS transitions to complete). |
show.bs.collapse |
This event fires immediately when the show instance method is called. |
shown.bs.collapse |
This event is fired when a collapse element has been made visible to the user (will wait for CSS transitions to complete). |
const myCollapsible = document.getElementById('myCollapsible')
myCollapsible.addEventListener('hidden.bs.collapse', event => {
// do something...
})