Modal
Sebenzisa iplagi yemodal ye-Bootstrap yeJavaScript ukongeza iingxoxo kwindawo yakho yeebhokisi ezikhanyayo, izaziso zabasebenzisi, okanye umxholo olungiselelwe ngokupheleleyo.
Ingaba isebenza kanjani
Ngaphambi kokuba uqalise ngecandelo le-modal ye-Bootstrap, qiniseka ukuba ufunda oku kulandelayo njengoko ukhetho lwethu lwemenyu lusanda kutshintsha.
- Iimodyuli zakhiwe ngeHTML, CSS, kunye neJavaScript. Zibekwe phezu kwayo yonke enye into kuxwebhu kwaye zisuse umqulu ukusuka
<body>
ukuze umxholo wemodal ujikeleze endaweni yoko. - Ukucofa i-modal "i-backdrop" iya kuvala ngokuzenzekelayo i-modal.
- I-Bootstrap ixhasa kuphela ifestile yemodal enye ngexesha. Iimodyuli ezifakwe kwiNgxoxo azixhaswa njengoko sikholelwa ukuba zingamava angalunganga kubasebenzisi.
- Iimodali zisetyenziswa
position: fixed
, enokuthi ngamanye amaxesha ibe yinto ethile malunga nonikezelo lwayo. Nanini na xa kunokwenzeka, beka i-modal yakho ye-HTML kwindawo yomgangatho ophezulu ukunqanda ukuphazamiseka okunokubakho kwezinye izinto. Uya kubaleka kwimiba xa ufulela.modal
ngaphakathi kwenye into emiselweyo. - Kwakhona, ngenxa ye
position: fixed
, kukho imiqolomba ngokusebenzisa iimodyuli kwizixhobo eziphathwayo. Jonga amaxwebhu ethu enkxaso kwisikhangeli ukuze ufumane iinkcukacha. - Ngenxa yendlela i-HTML5 eyichaza ngayo i-semantics yayo, uphawu lwe-
autofocus
HTML alunasiphumo kwiimodyuli zeBootstrap. Ukufezekisa umphumo ofanayo, sebenzisa iJavaScript yesiko:
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')
myModal.addEventListener('shown.bs.modal', function () {
myInput.focus()
})
prefers-reduced-motion
kumbuzo wemidiya. Jonga icandelo
leentshukumo ezincitshisiweyo kuxwebhu lwethu lofikelelo .
Qhubeka ufunda iidemos kunye nezikhokelo zokusetyenziswa.
Imizekelo
Amacandelo e-Modal
Apha ngezantsi ngumzekelo omileyo wemodyuli (othetha ukuba wona position
kwaye display
ukhutshiwe). Okubandakanyiweyo yintloko yemodyuli, umzimba wemodali (ufuneka padding
), kunye nefooter yemodali (ukhetho). Sicela ukuba uquke iiheader zemodal ezinezenzo zokugxotha nanini na kunokwenzeka, okanye unikeze esinye isenzo esicacileyo sokugxotha.
<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>
Idemo ephilayo
Guqula idemo yendlela yokusebenza ngokunqakraza iqhosha elingezantsi. Iza kutyibilika ezantsi kwaye iphele ukusuka phezulu ephepheni.
<!-- 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>
Umqolo omileyo
Xa i-backdrop isetelwe kwi-static, i-modal ayizukuvalwa xa ucofa ngaphandle kwayo. Cofa iqhosha elingezantsi ukuze uyizame.
<!-- 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>
Ukuskrola umxholo omde
Xa iimodyuli zinde kakhulu kwimboniselo yomsebenzisi okanye isixhobo, ziyaskrola ngaphandle kwephepha ngokwalo. Zama idemo engezantsi ukuze ubone ukuba sithetha ukuthini.
Unokwenza kwakhona imodali eskrolekayo evumela ukuskrola umzimba we-modal ngokongeza .modal-dialog-scrollable
ku .modal-dialog
.
<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
...
</div>
Ngokuthe nkqo embindini
Yongeza kumbindi .modal-dialog-centered
othe .modal-dialog
nkqo wemodali.
<!-- 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>
Iingcebiso zezixhobo kunye neepopovers
Iingcebiso zezixhobo kunye neepopovers zinokufakwa kwiimodyuli njengoko zifuneka. Xa iimodyuli zivaliwe, naziphi na iincam zesixhobo kunye neepopovers ngaphakathi nazo ziyalahlwa ngokuzenzekelayo.
<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>
Ukusebenzisa igridi
Sebenzisa inkqubo yegridi yeBootstrap ngaphakathi kwemodali ngokuzalela .container-fluid
ngaphakathi kwe .modal-body
. Emva koko, sebenzisa iiklasi zegridi eqhelekileyo njengoko ubuya kwenza naphi na kwenye indawo.
<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>
Ukutshintsha komxholo wemodyuli
Uneqela lamaqhosha athi onke avuse imodal efanayo enemixholo eyahlukileyo kancinane? Sebenzisa event.relatedTarget
kunye neempawu zeHTMLdata-bs-*
zokwahluka imixholo yemodal ngokuxhomekeke ekubeni leliphi iqhosha elicofiweyo.
Ngezantsi i-demo ephilayo ilandelwa ngumzekelo we-HTML kunye neJavaScript. Ngolwazi oluthe vetshe, funda amaxwebhu eziganeko zemodal ngeenkcukacha 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
})
Guqula phakathi kweemodeli
Guqula phakathi kweemodyuli ezininzi ngokubekwa kobuchule data-bs-target
kunye data-bs-toggle
neempawu. Umzekelo, ungatshintsha imodal yokusetha kwakhona igama lokugqitha ngaphakathi kophawu olusele luvuliwe kwimodal. Nceda uqaphele iimodyuli ezininzi azinakuvuleka ngexesha elinye-le ndlela itshintsha ngokulula phakathi kweemodyuli ezimbini ezahlukeneyo.
<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>
Guqula oopopayi
Uguqulo $modal-fade-transform
lumisela imo yenguqu .modal-dialog
yaphambi kwemodal fade-in oopopayi, $modal-show-transform
inguqu imisela inguqu .modal-dialog
ekupheleni kwemodal fade-in oopopayi.
Ukuba ufuna umzekelo u-zoom-in oopopayi, ungaseta $modal-fade-transform: scale(.8)
.
Susa oopopayi
Kwiimodyuli ezivele zivele endaweni yokucima ukubonwa, susa .fade
iklasi kuphawu lwakho lwemodal.
<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
...
</div>
Ukuphakama okunamandla
Ukuba umphakamo wemodali uyatshintsha ngelixa ivuliwe, kufuneka ufowunele myModal.handleUpdate()
ukulungisa indawo yemodali xa kuvela ibar yokuskrola.
Ukufikeleleka
Qinisekisa ukuba wongeza aria-labelledby="..."
, ubhekisa kwisihloko semodal, ku .modal
. Ukongeza, unganika inkcazo yencoko yababini yemodal nge aria-describedby
kwi .modal
. Qaphela ukuba awudingi ukongeza role="dialog"
kuba sele songeze ngeJavaScript.
Ifakela iividiyo zikaYouTube
Ukufakela iividiyo zikaYouTube kwiimodyuli kufuna iJavaScript eyongezelelweyo engekho kwiBootstrap ukumisa ngokuzenzekelayo ukudlala nokunye. Jonga esi sithuba seSitaki sokuPhuma siluncedo ngolwazi oluthe kratya.
Iisayizi ozikhethelayo
Iimodyuli zineesayizi ezintathu ozikhethelayo, ezifumaneka ngeeklasi zesilungisi eziza kubekwa kwi .modal-dialog
. Ezi sayizi zikhaba kwiindawo ezithile zokuqhawula ukunqanda iibar zokuskrola ezithe tye kwiindawo zokujonga ezimxinwa.
Ubungakanani | Iklasi | Modal max-width |
---|---|---|
Incinci | .modal-sm |
300px |
Ukuhlala kukho | Akukho nanye | 500px |
Enkulu | .modal-lg |
800px |
Inkulu kakhulu | .modal-xl |
1140px |
Imodyuli yethu engagqibekanga ngaphandle kodidi lwesilungisi yenza "phakathi" yesayizi yemodyuli.
<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>
IModali yesikrini esigcweleyo
Olunye uqashiso lukhetho lokuveza imodal egquma imboniselo yomsebenzisi, ekhoyo ngeeklasi zesilungisi ezibekwe kwi .modal-dialog
.
Iklasi | Ubukho |
---|---|
.modal-fullscreen |
Ngalo lonke ixesha |
.modal-fullscreen-sm-down |
Ngezantsi576px |
.modal-fullscreen-md-down |
Ngezantsi768px |
.modal-fullscreen-lg-down |
Ngezantsi992px |
.modal-fullscreen-xl-down |
Ngezantsi1200px |
.modal-fullscreen-xxl-down |
Ngezantsi1400px |
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
...
</div>
Sass
Izinto eziguquguqukayo
$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);
Umjikelo
Iimodyuli eziphendulayo zesikrini esigcweleyo ziveliswa $breakpoints
ngemephu kunye nelophu kwi 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);
}
}
}
}
Ukusetyenziswa
I-plugin ye-modal iguqula umxholo wakho ofihliweyo kwimfuno, ngeempawu zedatha okanye iJavaScript. Ikwabeka ngaphezulu kokuziphatha kokuskrola okungagqibekanga kwaye yenze .modal-backdrop
ukubonelela ngokucofa indawo yokugxotha iimodyuli ezibonisiweyo xa ucofa ngaphandle kwemodali.
Ngeempawu zedatha
Vula imodali ngaphandle kokubhala iJavaScript. Cwangcisa data-bs-toggle="modal"
isiqalelo somlawuli, njengeqhosha, kunye ne data-bs-target="#foo"
okanye href="#foo"
ukujolisa kwindlela ethile yokuguqula.
<button type="button" data-bs-toggle="modal" data-bs-target="#myModal">Launch modal</button>
NgeJavaScript
Yenza imodali ngomgca omnye weJavaScript:
var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)
Iinketho
Iinketho zinokugqithiswa ngeempawu zedatha okanye iJavaScript. Kuphawu lwedatha, faka igama lokhetho ku data-bs-
, njengakwi data-bs-backdrop=""
.
Igama | Uhlobo | Ukuhlala kukho | Inkcazo |
---|---|---|---|
backdrop |
boolean okanye umtya'static' |
true |
Ibandakanya i-modal-backdrop element. Kungenjalo, khankanya static imeko yangasemva engayivaliyo imodal ngokucofa. |
keyboard |
boolean | true |
Ivala imodal xa iqhosha lokubaleka licofa |
focus |
boolean | true |
Ibeka ugqaliselo kwimodali xa iqaliswa. |
Iindlela
Iindlela zeAsynchronous kunye notshintsho
Zonke iindlela ze-API zi -asynchronous kwaye ziqala utshintsho . Babuyela kumnxeba ngokukhawuleza nje ukuba inguqu iqalisiwe kodwa ingekapheli . Ukongeza, ukufowunelwa kwendlela kwicandelo lenguquko kuya kuhoywa .
Iinketho zokupasa
Yenza umxholo wakho ube yimodali. Yamkela iinketho ozikhethelayo object
.
var myModal = new bootstrap.Modal(document.getElementById('myModal'), {
keyboard: false
})
guqula
Yenza imodal ngesandla. Ubuyela kumnxeba phambi kokuba imodali iboniswe okanye ifihlwe (okt phambi kokuba isiganeko shown.bs.modal
okanye hidden.bs.modal
isiganeko senzeke).
myModal.toggle()
bonisa
Ivula imodali ngesandla. Ubuyela kumnxeba phambi kokuba imodali iboniswe (okt phambi kokuba shown.bs.modal
isiganeko senzeke).
myModal.show()
Kwakhona, ungadlula i-DOM element njengengxabano enokufunyanwa kwiziganeko ze-modal ( relatedTarget
njengepropati).
var modalToggle = document.getElementById('toggleMyModal') // relatedTarget
myModal.show(modalToggle)
fihla
Ufihla imodali ngesandla. Ibuyisela kumnxebi phambi kokuba imodali ifihlwe (okt phambi kokuba hidden.bs.modal
isiganeko senzeke).
myModal.hide()
handleUpdate
lungisa ngokuzenzela indawo yemodali ukuba umphakamo wemodali uyatshintsha ngelixa ivuliwe (okt xa kuvela ibar yokuskrola).
myModal.handleUpdate()
lahla
Uyonakalisa imodali yento. (Isusa idatha egciniweyo kwisiqalelo seDOM)
myModal.dispose()
getInstance
Indlela engatshintshiyo ekuvumela ukuba ufumane umzekelo wemodal odityaniswa nento yeDOM
var myModalEl = document.getElementById('myModal')
var modal = bootstrap.Modal.getInstance(myModalEl) // Returns a Bootstrap modal instance
getOrCreateInstance
Indlela engatshintshiyo ekuvumela ukuba ufumane umzekelo wemodal onxulunyaniswa nento yeDOM, okanye wenze entsha ukuba ayiqalwanga.
var myModalEl = document.querySelector('#myModal')
var modal = bootstrap.Modal.getOrCreateInstance(myModalEl) // Returns a Bootstrap modal instance
Iziganeko
Iklasi ye-modal ye-Bootstrap iveza iminyhadala embalwa yokuqhagamshelwa ekusebenzeni kwe-modal. Zonke iziganeko zemodal ziyagxothwa kwimodali ngokwayo (okt kwi <div class="modal">
).
Uhlobo lomsitho | Inkcazo |
---|---|
show.bs.modal |
Esi siganeko sivutha ngokukhawuleza xa show kubizwa indlela yomzekelo. Ukuba kubangelwa ngonqakrazo, into ecofayo iyafumaneka relatedTarget njengepropathi yesiganeko. |
shown.bs.modal |
Esi siganeko sigxothwa xa i-modal yenziwe yabonakala kumsebenzisi (iya kulinda i-CSS transitions ukugqiba). Ukuba kubangelwa ngonqakrazo, into ecofayo iyafumaneka relatedTarget njengepropathi yesiganeko. |
hide.bs.modal |
Esi siganeko sigxothwa ngokukhawuleza xa hide indlela yomzekelo ibiziwe. |
hidden.bs.modal |
Esi siganeko sigxothwa xa i-modal igqibile ukufihlwa kumsebenzisi (iya kulinda iinguqu zeCSS ukuba zigqibe). |
hidePrevented.bs.modal |
Esi siganeko siyagxothwa xa imodal ibonisiwe, umva wayo static kunye nonqakrazo ngaphandle kwemodal okanye ucinezelo lweqhosha lokuphuncuka lwenziwa ngokhetho lwebhodi yezitshixo okanye data-bs-keyboard imiselwe ku false . |
var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', function (event) {
// do something...
})