Modal
Sèvi ak plugin modal JavaScript Bootstrap pou ajoute dyalòg sou sit ou a pou bwat limyè, notifikasyon itilizatè, oswa kontni konplètman koutim.
Ki jan li fonksyone
Anvan w kòmanse ak eleman modal Bootstrap la, asire w ke ou li sa ki annapre yo paske opsyon meni nou yo te chanje dènyèman.
- Modèl yo bati ak HTML, CSS, ak JavaScript. Yo ap pozisyone sou tout lòt bagay nan dokiman an epi retire woulo liv la
<body>
pou kontni modal woule pito. - Klike sou "backdrop" modal la pral otomatikman fèmen modal la.
- Bootstrap sipòte sèlman yon fenèt modal nan yon moman. Modal anbrike yo pa sipòte paske nou kwè yo se eksperyans itilizatè pòv yo.
- Modèl itilize
position: fixed
, ki pafwa ka yon ti jan patikilye sou rann li yo. Chak fwa sa posib, mete HTML modal ou a nan yon pozisyon wo nivo pou evite entèferans potansyèl de lòt eleman. Ou pral gen anpil chans rankontre pwoblèm lè nidifikasyon yon.modal
nan yon lòt eleman fiks. - Yon fwa ankò, akòz
position: fixed
, gen kèk opozisyon ak lè l sèvi avèk modal sou aparèy mobil. Gade dokiman sipò navigatè nou an pou plis detay. - Akòz fason HTML5 defini semantik li yo, atribi
autofocus
HTML la pa gen okenn efè nan modal Bootstrap. Pou reyalize menm efè a, sèvi ak kèk JavaScript koutim:
$('#myModal').on('shown.bs.modal', function () {
$('#myInput').trigger('focus')
})
prefers-reduced-motion
rechèch medya yo. Gade
seksyon mouvman redui nan dokiman aksè nou an .
Kontinye lekti pou demonstrasyon ak direktiv itilizasyon.
Egzanp yo
Konpozan modal yo
Anba la a se yon egzanp modal estatikposition
(sa vle di li yo ak display
yo te remplacé). Gen ladann header modal la, kò modal (obligatwa pou padding
), ak modal footer (si ou vle). Nou mande pou w mete en-tête modal ak aksyon ranvwaye chak fwa sa posib, oswa bay yon lòt aksyon eksplisit ranvwaye.
<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="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</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-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Live Demo
Aktive yon demonstrasyon modal k ap travay lè w klike sou bouton ki anba a. Li pral glise desann ak fennen soti nan tèt paj la.
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-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="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Fondasyon estatik
Lè seri a mete nan estatik, modal la pa pral fèmen lè klike deyò li. Klike sou bouton ki anba a pou eseye li.
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdrop">
Launch static backdrop modal
</button>
<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-backdrop="static" data-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="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
Defile kontni long
Lè modal yo vin twò long pou fenèt itilizatè a oswa aparèy, yo defile endepandan de paj la li menm. Eseye Demo ki anba a pou wè sa nou vle di.
Ou kapab tou kreye yon modal defilable ki pèmèt defile kò modal la lè w ajoute .modal-dialog-scrollable
nan .modal-dialog
.
<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
...
</div>
Vètikalman santre
Add .modal-dialog-centered
to .modal-dialog
a vètikal sant modal la.
<!-- 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>
Konsèy zouti ak popovers
Ti konsèy ak popovers yo ka mete nan modal jan sa nesesè. Lè modal yo fèmen, nenpòt konsèy sou zouti ak popover nan yo tou otomatikman ranvwaye.
<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-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>
Sèvi ak kadriyaj la
Sèvi ak sistèm kadriyaj Bootstrap la nan yon modal pa nidifikasyon .container-fluid
nan .modal-body
. Lè sa a, sèvi ak klas sistèm kadriyaj nòmal yo menm jan ou ta nenpòt lòt kote.
<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 ml-auto">.col-md-4 .ml-auto</div>
</div>
<div class="row">
<div class="col-md-3 ml-auto">.col-md-3 .ml-auto</div>
<div class="col-md-2 ml-auto">.col-md-2 .ml-auto</div>
</div>
<div class="row">
<div class="col-md-6 ml-auto">.col-md-6 .ml-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>
Kontni modal varye
Gen yon pakèt bouton ki tout deklanche menm modal la ak kontni yon ti kras diferan? Sèvi event.relatedTarget
ak ak HTML data-*
atribi (petèt atravè jQuery ) yo varye sa ki nan modal la depann sou ki bouton yo te klike.
Anba la a se yon Demo ap viv ki te swiv pa egzanp HTML ak JavaScript. Pou plis enfòmasyon, li dokiman evènman modal yo pou plis detay sou relatedTarget
.
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-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="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<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-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})
Chanje animasyon
Varyab $modal-fade-transform
la detèmine eta transfòmasyon nan .modal-dialog
anvan animasyon an fade-in modal, $modal-show-transform
varyab la detèmine transfòmasyon nan .modal-dialog
nan fen animasyon an fennen modal.
Si ou vle pou egzanp yon animasyon zoom-in, ou ka mete $modal-fade-transform: scale(.8)
.
Retire animasyon
Pou modal ki tou senpleman parèt olye ke fennen nan yo wè, retire .fade
klas la nan maketing modal ou a.
<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
...
</div>
Wotè dinamik
Si wotè yon modal chanje pandan li louvri, ou ta dwe rele $('#myModal').modal('handleUpdate')
pou reajiste pozisyon modal la nan ka yon defile parèt.
Aksesiblite
Asire w ou ajoute aria-labelledby="..."
, referans tit modal la, nan .modal
. Anplis de sa, ou ka bay yon deskripsyon dyalòg modal ou a ak aria-describedby
sou .modal
. Remake byen ke ou pa bezwen ajoute role="dialog"
paske nou deja ajoute li atravè JavaScript.
Anrejistre videyo YouTube yo
Anrejistre videyo YouTube nan modal mande pou JavaScript adisyonèl ki pa nan Bootstrap otomatikman sispann lekti ak plis ankò. Gade pòs itil Stack Overflow sa a pou plis enfòmasyon.
Gwosè si ou vle
Modal yo gen twa gwosè opsyonèl, ki disponib atravè klas modifye yo dwe mete sou yon .modal-dialog
. Gwosè sa yo choute nan sèten pwen rupture pou fè pou evite ba defile orizontal sou vi ki pi etwat.
Gwosè | Klas | Modal max-lajè |
---|---|---|
Ti | .modal-sm |
300px |
Default | Okenn | 500px |
Gwo | .modal-lg |
800px |
Siplemantè gwo | .modal-xl |
1140px |
Modal default nou an san klas modifye konstitye modal gwosè "mwayen".
<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>
Itilizasyon
Plugin modal la chanje kontni kache ou sou demann, atravè atribi done oswa JavaScript. Li ajoute tou .modal-open
pou <body>
pase sou konpòtman defile default ak jenere yon .modal-backdrop
pou bay yon zòn klike sou pou ranvwaye modal yo montre lè klike deyò modal la.
Via atribi done yo
Aktive yon modal san w pa ekri JavaScript. Mete data-toggle="modal"
sou yon eleman kontwolè, tankou yon bouton, ansanm ak yon data-target="#foo"
oswa href="#foo"
pou vize yon modal espesifik pou activer.
<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>
Via JavaScript
Rele yon modal ak id myModal
ak yon sèl liy JavaScript:
$('#myModal').modal(options)
Opsyon
Opsyon yo ka pase atravè atribi done oswa JavaScript. Pou atribi done, ajoute non opsyon a nan data-
, tankou nan data-backdrop=""
.
Non | Kalite | Default | Deskripsyon |
---|---|---|---|
seri | boolean oswa fisèl la'static' |
vre | Gen ladann yon eleman modal-backdrop. Altènativman, presize static pou yon seri ki pa fèmen modal la sou klike sou. |
klavye | booleyen | vre | Fèmen modal la lè yo peze kle chape |
konsantre | booleyen | vre | Mete konsantre sou modal la lè inisyalize. |
montre | booleyen | vre | Montre modal la lè inisyalize. |
Metòd
Metòd asynchrone ak tranzisyon
Tout metòd API yo asenkron epi yo kòmanse yon tranzisyon . Yo retounen kote moun k ap rele a le pli vit ke tranzisyon an kòmanse men anvan li fini . Anplis de sa, yo pral inyore yon apèl metòd sou yon eleman tranzisyon .
.modal(options)
Aktive kontni ou kòm yon modal. Aksepte yon opsyon opsyonèl object
.
$('#myModal').modal({
keyboard: false
})
.modal('toggle')
Manyèlman aktive yon modal. Retounen bay moun kap rele a anvan modal la aktyèlman te montre oswa kache (sa vle di anvan evènman shown.bs.modal
an hidden.bs.modal
rive).
$('#myModal').modal('toggle')
.modal('show')
Manyèlman ouvè yon modal. Retounen bay moun k ap rele a anvan yo montre modal la (sa vle di anvan shown.bs.modal
evènman an rive).
$('#myModal').modal('show')
.modal('hide')
Manyèlman kache yon modal. Retounen bay moun kap rele a anvan modal la te kache (sa vle di anvan hidden.bs.modal
evènman an rive).
$('#myModal').modal('hide')
.modal('handleUpdate')
Manyèlman reajiste pozisyon modal la si wotè yon modal chanje pandan li ouvri (sa vle di nan ka yon defile parèt).
$('#myModal').modal('handleUpdate')
.modal('dispose')
Detwi modal yon eleman.
Evènman
Klas modal Bootstrap a ekspoze kèk evènman pou branche nan fonksyonalite modal. Tout evènman modal yo tire sou modal la li menm (sa vle di nan la <div class="modal">
).
Kalite Evènman | Description |
---|---|
show.bs.modal | This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event. |
shown.bs.modal | This event is fired when the modal has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the relatedTarget property of the event. |
hide.bs.modal | This event is fired immediately when the hide instance method has been called. |
hidden.bs.modal | This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete). |
hidePrevented.bs.modal | Evènman sa a revoke lè yo montre modal la, seri li yo static ak yon klik deyò modal la oswa yon peze kle chape fèt ak opsyon klavye a oswa data-keyboard mete sou false . |
$('#myModal').on('hidden.bs.modal', function (event) {
// do something...
})