Modal
Whakamahia te monohiko JavaScript a Bootstrap ki te taapiri i nga korero ki to papaanga mo nga pouaka rama, nga whakamohiotanga a nga kaiwhakamahi, me nga ihirangi tino ritenga ranei.
Pehea te mahi
I mua i te tiimata me te waahanga tikanga a Bootstrap, kia mohio koe ki te panui i nga mea e whai ake nei na te mea kua huri nga whiringa tahua.
- Ka hangaia nga tauira ki te HTML, CSS, me te JavaScript. Ka tuuhia ki runga ake i nga mea katoa o te tuhinga ka tango i te panuku mai i te pukapuka kia panuku te
<body>
ihirangi aratau. - Ma te panui i te "papamuri" ka kati aunoa i te ahua.
- Ka tautokohia e Bootstrap tetahi matapihi aratau i ia wa. Kaore i te tautokohia nga tikanga kohanga na te mea e whakapono ana matou he ngoikore nga wheako kaiwhakamahi.
- Ka whakamahia
position: fixed
e nga tauira, he ahua motuhake mo tana whakaputanga. I nga wa katoa ka taea, tuuhia to HTML modal ki te taumata teitei kia kore ai e pokanoa mai i etahi atu huānga. Ka raru pea koe i te wa e kohanga ana.modal
i tetahi atu huānga kua whakaritea. - Ano ano, na te mea
position: fixed
, kei reira etahi whakatupato me te whakamahi i nga tikanga i runga i nga taputapu pūkoro. Tirohia a maatau tuhinga tautoko tirotiro mo nga taipitopito. - Na te ahua o te HTML5 e whakaatu ana i ona tohu, kaore he painga o te
autofocus
huanga HTML ki nga tikanga Bootstrap. Hei whakatutuki i taua paanga, whakamahia etahi ritenga JavaScript:
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')
myModal.addEventListener('shown.bs.modal', function () {
myInput.focus()
})
prefers-reduced-motion
uiui pāpāho. Tirohia te
waahanga motini whakaheke o a maatau tuhinga whakaurunga .
Kia mau ki te panui mo nga whakaaturanga me nga aratohu whakamahi.
Tauira
Nga waahanga motika
Kei raro nei he tauira aratau patekoposition
( ko tona tikanga display
kua whakakorehia). Kei roto ko te pane aratau, te tinana aratau (e hiahiatia ana mo padding
), me te hiku aratau (he kowhiri). Ka tono matou kia whakaurua e koe nga pane aratau me nga mahi whakakore i nga wa katoa ka taea, me whakarato ranei i tetahi atu mahi whakakore.
<div class="modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Demo ora
Takahurihia he whakaaturanga aratau mahi ma te paatene i te paatene kei raro nei. Ka paheke ki raro ka memeha mai i te tihi o te wharangi.
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Papamuri pateko
Ina tautuhia te papamuri ki te pateko, ka kore te aratau e kati ina paato ana ki waho. Patohia te paatene i raro nei hei whakamatau.
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Launch static backdrop modal
</button>
<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
Te panuku i nga ihirangi roa
Ka roa rawa nga tikanga mo te tauranga tirohanga a te kaiwhakamahi, te taputapu ranei, ka panuku motuhake mai i te wharangi ake. Whakamātauria te demo i raro nei kia kite he aha te tikanga.
Ka taea hoki e koe te hanga i tetahi aratau panuku ka taea te panuku i te tinana aratau ma te taapiri atu .modal-dialog-scrollable
ki te .modal-dialog
.
<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
...
</div>
Pokapū poutū
Tāpirihia .modal-dialog-centered
ki .modal-dialog
te poutū poutū te aratau.
<!-- Vertically centered modal -->
<div class="modal-dialog modal-dialog-centered">
...
</div>
<!-- Vertically centered scrollable modal -->
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
...
</div>
Nga aki utauta me nga popovers
Ka taea te whakanoho i nga aki taputapu me nga pouvers ki roto i nga tikanga e hiahiatia ana. Ina katia nga tikanga, ka whakakorea aunoatia nga aki taputapu me nga popovers o roto.
<div class="modal-body">
<h5>Popover in a modal</h5>
<p>This <a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-bs-content="Popover body content is set in this attribute.">button</a> triggers a popover on click.</p>
<hr>
<h5>Tooltips in a modal</h5>
<p><a href="#" class="tooltip-test" title="Tooltip">This link</a> and <a href="#" class="tooltip-test" title="Tooltip">that link</a> have tooltips on hover.</p>
</div>
Te whakamahi i te matiti
Whakamahia te punaha matiti Bootstrap i roto i te aratau ma te kohanga .container-fluid
i roto i te .modal-body
. Na, whakamahia nga karaehe punaha matiti noa penei i a koe ki hea atu.
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4 ms-auto">.col-md-4 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-3 ms-auto">.col-md-3 .ms-auto</div>
<div class="col-md-2 ms-auto">.col-md-2 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-6 ms-auto">.col-md-6 .ms-auto</div>
</div>
<div class="row">
<div class="col-sm-9">
Level 1: .col-sm-9
<div class="row">
<div class="col-8 col-sm-6">
Level 2: .col-8 .col-sm-6
</div>
<div class="col-4 col-sm-6">
Level 2: .col-4 .col-sm-6
</div>
</div>
</div>
</div>
</div>
</div>
Te rereke i nga ihirangi aratau
He paihere o nga patene e whakaoho ana i te ahua kotahi me nga ihirangi paku rereke? Whakamahia event.relatedTarget
me nga huanga HTMLdata-bs-*
ki te rereke i nga ihirangi o te aratau i runga i te paatene i paatohia.
Kei raro ko te demo ora e whai ana i te tauira HTML me JavaScript. Mo etahi atu korero, panuihia nga tuhinga takahanga modal mo nga taipitopito mo relatedTarget
.
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@getbootstrap">Open modal for @getbootstrap</button>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New message</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form>
<div class="mb-3">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="mb-3">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
var exampleModal = document.getElementById('exampleModal')
exampleModal.addEventListener('show.bs.modal', function (event) {
// Button that triggered the modal
var button = event.relatedTarget
// Extract info from data-bs-* attributes
var recipient = button.getAttribute('data-bs-whatever')
// If necessary, you could initiate an AJAX request here
// and then do the updating in a callback.
//
// Update the modal's content.
var modalTitle = exampleModal.querySelector('.modal-title')
var modalBodyInput = exampleModal.querySelector('.modal-body input')
modalTitle.textContent = 'New message to ' + recipient
modalBodyInput.value = recipient
})
Takahuri i waenganui i nga tikanga
Takahurihia i waenga i nga tikanga maha me etahi waahi mohio o te data-bs-target
me nga data-bs-toggle
huanga. Hei tauira, ka taea e koe te huri i te aratau tautuhi kupuhipa mai i roto i tetahi tohu kua tuwhera i roto i te tikanga. Kia mahara kaore e taea te whakatuwhera i nga tikanga maha i te wa kotahi — ka takahuri noa tenei tikanga i waenga i nga tikanga e rua.
<div class="modal fade" id="exampleModalToggle" aria-hidden="true" aria-labelledby="exampleModalToggleLabel" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalToggleLabel">Modal 1</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Show a second modal and hide this one with the button below.
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-bs-target="#exampleModalToggle2" data-bs-toggle="modal" data-bs-dismiss="modal">Open second modal</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="exampleModalToggle2" aria-hidden="true" aria-labelledby="exampleModalToggleLabel2" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalToggleLabel2">Modal 2</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Hide this modal and show the first with the button below.
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-bs-target="#exampleModalToggle" data-bs-toggle="modal" data-bs-dismiss="modal">Back to first</button>
</div>
</div>
</div>
</div>
<a class="btn btn-primary" data-bs-toggle="modal" href="#exampleModalToggle" role="button">Open first modal</a>
Huri hākoritanga
Ka $modal-fade-transform
whakatauhia e te taurangi te ahua o te huringa i .modal-dialog
mua i te whakakorikori ahua memeha-i roto, ko te $modal-show-transform
taurangi ka whakatau i te huringa o .modal-dialog
te mutunga o te hākoritanga memeha-i roto i te aratau.
Mena kei te pirangi koe hei tauira he hākoritanga topa-mai, ka taea e koe te whakarite $modal-fade-transform: scale(.8)
.
Tango hākoritanga
Mo nga tauira ka puta noa, kaua e memeha ki te tiro, tangohia te .fade
karaehe mai i to tohu tohu.
<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
...
</div>
Te teitei hihiko
Mena ka huri te teitei o te aratau i te wa e tuwhera ana, me waea atu koe myModal.handleUpdate()
ki te whakatika i te tuunga o te aratau mena ka puta he pae panuku.
Te urunga
Kia mahara ki te taapiri aria-labelledby="..."
, tohutoro i te taitara aratau, ki .modal
. I tua atu, ka taea e koe te whakaatu i to korero korero mo aria-describedby
runga i te .modal
. Kia mahara kaore koe e hiahia ki te taapiri role="dialog"
i te mea kua taapirihia e matou ma te JavaScript.
Te whakauru ataata YouTube
Ko te whakauru i nga riipene YouTube ki roto i nga tikanga ka hiahiatia he JavaScript taapiri kaore i Bootstrap hei aukati aunoa i te purei me etahi atu. Tirohia tenei panui Stack Overflow mo etahi atu korero.
Rahi whiriwhiri
E toru nga rahi o nga tauira, e waatea ana ma nga karaehe whakarereke hei whakanoho ki runga i te .modal-dialog
. Ka whana enei rahi ki etahi waahi pakaru hei karo i nga pae panuku whakapae i runga i nga tauranga whaiti.
Rahi | Karaehe | Modal max-whanui |
---|---|---|
Iti | .modal-sm |
300px |
Taunoa | Karekau | 500px |
Nui | .modal-lg |
800px |
Nui atu | .modal-xl |
1140px |
Ko ta maatau aratau taunoa kaore he karaehe whakarereke ko te momo momo rahi "waenganui".
<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>
Modal Mata Katoa
Ko tetahi atu taapiri ko te kowhiringa ki te whakaputa i tetahi aratau e kapi ana i te tauranga tirohanga a te kaiwhakamahi, e waatea ana ma nga karaehe whakarereke ka tuu ki runga i te .modal-dialog
.
Karaehe | Te waatea |
---|---|
.modal-fullscreen |
I nga wa katoa |
.modal-fullscreen-sm-down |
Kei raro576px |
.modal-fullscreen-md-down |
Kei raro768px |
.modal-fullscreen-lg-down |
Kei raro992px |
.modal-fullscreen-xl-down |
Kei raro1200px |
.modal-fullscreen-xxl-down |
Kei raro1400px |
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
...
</div>
Sass
Taurangi
$modal-inner-padding: $spacer;
$modal-footer-margin-between: .5rem;
$modal-dialog-margin: .5rem;
$modal-dialog-margin-y-sm-up: 1.75rem;
$modal-title-line-height: $line-height-base;
$modal-content-color: null;
$modal-content-bg: $white;
$modal-content-border-color: rgba($black, .2);
$modal-content-border-width: $border-width;
$modal-content-border-radius: $border-radius-lg;
$modal-content-inner-border-radius: subtract($modal-content-border-radius, $modal-content-border-width);
$modal-content-box-shadow-xs: $box-shadow-sm;
$modal-content-box-shadow-sm-up: $box-shadow;
$modal-backdrop-bg: $black;
$modal-backdrop-opacity: .5;
$modal-header-border-color: $border-color;
$modal-footer-border-color: $modal-header-border-color;
$modal-header-border-width: $modal-content-border-width;
$modal-footer-border-width: $modal-header-border-width;
$modal-header-padding-y: $modal-inner-padding;
$modal-header-padding-x: $modal-inner-padding;
$modal-header-padding: $modal-header-padding-y $modal-header-padding-x; // Keep this for backwards compatibility
$modal-sm: 300px;
$modal-md: 500px;
$modal-lg: 800px;
$modal-xl: 1140px;
$modal-fade-transform: translate(0, -50px);
$modal-show-transform: none;
$modal-transition: transform .3s ease-out;
$modal-scale-transform: scale(1.02);
Porowhita
Ko nga tauira mata-katoa whakautu ka hangaia ma te $breakpoints
mapi me te kopae i roto scss/_modal.scss
.
@each $breakpoint in map-keys($grid-breakpoints) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
$postfix: if($infix != "", $infix + "-down", "");
@include media-breakpoint-down($breakpoint) {
.modal-fullscreen#{$postfix} {
width: 100vw;
max-width: none;
height: 100%;
margin: 0;
.modal-content {
height: 100%;
border: 0;
@include border-radius(0);
}
.modal-header {
@include border-radius(0);
}
.modal-body {
overflow-y: auto;
}
.modal-footer {
@include border-radius(0);
}
}
}
}
Whakamahinga
Ko te mono mono ka huri i to ihirangi huna i runga i te tono, ma nga huanga raraunga, JavaScript ranei. Ka takahia hoki te whanonga panuku taunoa me te whakaputa .modal-backdrop
i te waahi paatoo mo te whakakore i nga tikanga kua whakaatuhia ina paato i waho o te aratau.
Ma nga huanga raraunga
Whakahohehia he tikanga me te kore e tuhi JavaScript. Whakatakotoria data-bs-toggle="modal"
ki runga i te huānga mana whakahaere, penei i te paatene, me te, ki te whai data-bs-target="#foo"
ranei href="#foo"
i tetahi aratau motuhake hei takahuri.
<button type="button" data-bs-toggle="modal" data-bs-target="#myModal">Launch modal</button>
Ma te JavaScript
Waihangahia he tikanga me te rarangi kotahi o JavaScript:
var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)
Kōwhiringa
Ko nga whiringa ka taea te tuku ma nga huanga raraunga, JavaScript ranei. Mo nga huanga raraunga, apitihia te ingoa whiringa ki data-bs-
, penei i te data-bs-backdrop=""
.
Ingoa | Momo | Taunoa | Whakaahuatanga |
---|---|---|---|
backdrop |
boolean te aho ranei'static' |
true |
Kei roto he huānga tauira-papamuri. Arā, whakawhāitihia static he papamuri e kore e kati i te aratau i runga i te paatene. |
keyboard |
boolean | true |
Ka kati i te aratau ina pehia te ki mawhiti |
focus |
boolean | true |
Ka tuu te aro ki te aratau ina arawhiti. |
Nga tikanga
Nga tikanga tukutahi me nga whakawhitinga
Ko nga tikanga API katoa he tukutahi ka timata te whakawhiti . Ka hoki mai ano ki te kaikaranga ina timata te whakawhiti engari i mua i te mutunga . I tua atu, ka warewarehia te karanga tikanga mo tetahi waahanga whakawhiti .
Nga whiringa whakawhiti
Whakahohehia to ihirangi hei aratau. Ka whakaae ki nga whiringa whiriwhiri object
.
var myModal = new bootstrap.Modal(document.getElementById('myModal'), {
keyboard: false
})
takahuri
Ka takahuri-a-ringa i te tikanga. Ka hoki ki te kaiwaea i mua i te whakaaturanga, i te hunanga ranei o te aratau (arā, i mua i te puta o shown.bs.modal
te hidden.bs.modal
takahanga).
myModal.toggle()
whakaatu
Ka whakatuwhera a-ringa i te tikanga. Ka hoki ki te kaiwaea i mua i te whakaaturanga o te tikanga (ara i mua i te shown.bs.modal
puta o te takahanga).
myModal.show()
Ano, ka taea e koe te tuku i tetahi huānga DOM hei tohenga ka taea te whiwhi i nga huihuinga aratau (hei relatedTarget
taonga).
var modalToggle = document.getElementById('toggleMyModal') // relatedTarget
myModal.show(modalToggle)
huna
Ka huna a-ringa i tetahi tikanga. Ka hoki ki te kaiwaea i mua i te hunanga o te tikanga (ara i mua i te hidden.bs.modal
puta o te takahanga).
myModal.hide()
handleUpdate
Whakaritea-a-ringa te tuunga o te aratau mena ka huri te teitei o te aratau i te wa e tuwhera ana (arā, mena ka puta he pae panuku).
myModal.handleUpdate()
tuku
Ka whakakore i te ahua o te huānga. (Tangohia ai nga raraunga rongoa i te huānga DOM)
myModal.dispose()
getInstance
Tikanga pateko e taea ai e koe te tiki i te tauira aratau e hono ana ki tetahi huānga DOM
var myModalEl = document.getElementById('myModal')
var modal = bootstrap.Modal.getInstance(myModalEl) // Returns a Bootstrap modal instance
getOrCreateInstance
Tikanga pateko e taea ai e koe te tiki i te tauira aratau e hono ana ki tetahi huānga DOM, ki te hanga i tetahi mea hou mena kaore i arawhitia
var myModalEl = document.querySelector('#myModal')
var modal = bootstrap.Modal.getOrCreateInstance(myModalEl) // Returns a Bootstrap modal instance
Nga huihuinga
Ko te karaehe aratau a Bootstrap e whakaatu ana i etahi huihuinga mo te hono atu ki nga mahi tikanga. Ko nga huihuinga aratau katoa ka puhia ki te aratau ake (arā, i te <div class="modal">
).
Momo takahanga | Whakaahuatanga |
---|---|
show.bs.modal |
Ka pupuhi tenei takahanga ina ka show karangahia te aratuka tauira. Mena na te paato i pa, ka waatea te huānga kua paatohia hei relatedTarget taonga mo te huihuinga. |
shown.bs.modal |
Ka pupuhihia tenei takahanga i te wa i kitea ai te ahua ki te kaiwhakamahi (ka tatari kia oti nga whakawhitinga CSS). Mena na te paato i pa, ka waatea te huānga kua paatohia hei relatedTarget taonga mo te huihuinga. |
hide.bs.modal |
Ka puhia tonutia tenei takahanga ina hide kua karangahia te aratuka tauira. |
hidden.bs.modal |
Ka pupuhihia tenei takahanga ina mutu te hunanga o te tikanga mai i te kaiwhakamahi (ka tatari kia oti nga whakawhitinga CSS). |
hidePrevented.bs.modal |
Ka pupuhihia tenei takahanga ina whakaatuhia te aratau, ko tona papamuri, static ka paato i waho o te aratau, ka mahia ranei te perehi mawhiti me te kowhiringa papapātuhi, data-bs-keyboard ka tautuhia ranei ki false . |
var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', function (event) {
// do something...
})