SourceModal
Sebenzisa iplagi yemodal ye-Bootstrap yeJavaScript ukongeza iingxoxo kwindawo yakho yeebhokisi ezikhanyayo, izaziso zabasebenzisi, okanye umxholo olungiselelwe ngokupheleleyo.
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:
Khuphela
$ ( '#myModal' ). on ( 'shown.bs.modal' , function () {
$ ( '#myInput' ). trigger ( 'focus' )
})
Qhubeka ufunda iidemos kunye nezikhokelo zokusetyenziswa.
Apha ngezantsi kukho umzekelo wemodyuli engatshintshiyo (ithetha ukuba yona position
kwaye display
iye yabhalwa ngaphezulu). 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 sokugxotha esicacileyo.
Isicatshulwa somzimba we-Modal siya apha.
Khuphela
<div class= "modal" tabindex= "-1" role= "dialog" >
<div class= "modal-dialog" role= "document" >
<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>
Guqula idemo yendlela yokusebenza ngokunqakraza iqhosha elingezantsi. Iza kutyibilika phantsi kwaye iphele ukusuka phezulu ephepheni.
Woohoo, you're reading this text in a modal!
Qalisa imodal yedemo
Khuphela
<!-- 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" role= "dialog" aria-labelledby= "exampleModalLabel" aria-hidden= "true" >
<div class= "modal-dialog" role= "document" >
<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>
Xa iimodyuli zinde kakhulu kwimboniselo yomsebenzisi okanye isixhobo, ziyaskrola ngaphandle kwephepha ngokwalo. Zama idemo engezantsi ukuze ubone ukuba sithetha ukuthini.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.
Qalisa imodal yedemo
Khuphela
<!-- Button trigger modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModalLong" >
Launch demo modal
</button>
<!-- Modal -->
<div class= "modal fade" id= "exampleModalLong" tabindex= "-1" role= "dialog" aria-labelledby= "exampleModalLongTitle" aria-hidden= "true" >
<div class= "modal-dialog" role= "document" >
<div class= "modal-content" >
<div class= "modal-header" >
<h5 class= "modal-title" id= "exampleModalLongTitle" > 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>
Yongeza kumbindi .modal-dialog-centered
othe .modal-dialog
nkqo wemodali.
Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Qalisa imodal yedemo
Khuphela
<!-- Button trigger modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= "#exampleModalCenter" >
Launch demo modal
</button>
<!-- Modal -->
<div class= "modal fade" id= "exampleModalCenter" tabindex= "-1" role= "dialog" aria-labelledby= "exampleModalCenterTitle" aria-hidden= "true" >
<div class= "modal-dialog modal-dialog-centered" role= "document" >
<div class= "modal-content" >
<div class= "modal-header" >
<h5 class= "modal-title" id= "exampleModalCenterTitle" > 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>
Iingcebiso zezixhobo kunye neepopovers zinokufakwa kwiimodyuli njengoko zifuneka. Xa iimodyuli zivaliwe, naziphi na iincam zesixhobo kunye neepopovers ngaphakathi nazo ziyalahlwa ngokuzenzekelayo.
Popover in a modal
This button triggers a popover on click.
Tooltips in a modal
This link and that link have tooltips on hover.
Qalisa imodal yedemo
Khuphela
<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>
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.
.col-md-4
.col-md-4 .ml-auto
.col-md-3 .ml-auto
.col-md-2 .ml-auto
Level 1: .col-sm-9
Level 2: .col-8 .col-sm-6
Level 2: .col-4 .col-sm-6
Qalisa imodal yedemo
Khuphela
<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>
Ukutshintsha komxholo wemodyuli
Uneqela lamaqhosha athi onke avuse imodal efanayo enemixholo eyahlukileyo kancinane? Sebenzisa event.relatedTarget
kunye neempawu zeHTML (data-*
ngokunokwenzeka ngejQuery ) ukwahlula 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
.
Vula imodyuli ye-@mdo
Vula imodali ye-@fat
Vula imodyuli ye-@getbootstrap
Khuphela
<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" role= "dialog" aria-labelledby= "exampleModalLabel" aria-hidden= "true" >
<div class= "modal-dialog" role= "document" >
<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>
Khuphela
$ ( '#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 )
})
Kwiimodyuli ezibonakala ngokulula kunokuba ziphele ukubonwa, susa .fade
iklasi kuphawu lwakho lwemodal.
Khuphela
<div class= "modal" tabindex= "-1" role= "dialog" aria-labelledby= "..." aria-hidden= "true" >
...
</div>
Ukuba umphakamo wemodali uyatshintsha ngelixa ivuliwe, kuya kufuneka ufowunele $('#myModal').modal('handleUpdate')
ukulungisa indawo yemodali kwimeko yokuskrola.
Qinisekisa ukuba wongeza role="dialog"
kunye aria-labelledby="..."
, ubhekisa kwisihloko semodal, ku .modal
, kwaye role="document"
kuyo .modal-dialog
ngokwayo. Ukongeza, unganika inkcazo yencoko yababini yemodal nge aria-describedby
kwi .modal
.
Ifakela iividiyo zikaYouTube
Ukufakela iividiyo zikaYouTube kwiimodyuli kufuna iJavaScript eyongezelelweyo engekho kwiBootstrap ukumisa ngokuzenzekelayo ukudlala nokunye. Bona esi sithuba sokuPhuma kweSitaki siluncedo ukuze ufumane ulwazi oluthe kratya.
Iimodyuli zineesayizi ezimbini ozikhethelayo, ezifumaneka ngeeklasi zesilungisi eziza kubekwa kwi .modal-dialog
. Ezi sayizi zikhaba kwiindawo ezithile zokuqhawula ukunqanda iibar zokuskrola ezithe tye kwiindawo zokujonga ezimxinwa.
Imodeli enkulu
Imodeli encinci
Khuphela
<!-- Large modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= ".bd-example-modal-lg" > Large modal</button>
<div class= "modal fade bd-example-modal-lg" tabindex= "-1" role= "dialog" aria-labelledby= "myLargeModalLabel" aria-hidden= "true" >
<div class= "modal-dialog modal-lg" >
<div class= "modal-content" >
...
</div>
</div>
</div>
<!-- Small modal -->
<button type= "button" class= "btn btn-primary" data-toggle= "modal" data-target= ".bd-example-modal-sm" > Small modal</button>
<div class= "modal fade bd-example-modal-sm" tabindex= "-1" role= "dialog" aria-labelledby= "mySmallModalLabel" aria-hidden= "true" >
<div class= "modal-dialog modal-sm" >
<div class= "modal-content" >
...
</div>
</div>
</div>
I-plugin ye-modal iguqula umxholo wakho ofihliweyo kwimfuno, ngeempawu zedatha okanye iJavaScript. Yongeza kwakhona .modal-open
kwi- <body>
override default scrolling behavior kwaye yenza i- .modal-backdrop
a ukunika indawo yokucofa ukugxotha iimodyuli ezibonisiweyo xa ucofa ngaphandle kwemodali.
Vula imodali ngaphandle kokubhala iJavaScript. Cwangcisa data-toggle="modal"
isiqalelo somlawuli, njengeqhosha, kunye ne data-target="#foo"
okanye href="#foo"
ukujolisa kwindlela ethile yokuguqula.
Khuphela
<button type= "button" data-toggle= "modal" data-target= "#myModal" > Launch modal</button>
Fowunela i-modal ene-id myModal
ngomgca omnye weJavaScript:
Khuphela
$ ( '#myModal' ). modal ( options )
Iinketho zinokugqithiswa ngeempawu zedatha okanye iJavaScript. Kuphawu lwedatha, faka igama lokhetho ku data-
, njengakwi data-backdrop=""
.
Igama
Uhlobo
Ukuhlala kukho
Inkcazo
ngasemva
boolean okanye umtya'static'
yinyani
Ibandakanya i-modal-backdrop element. Kungenjalo, khankanya static
imeko yangasemva engayivaliyo imodal ngokucofa.
ikhibhodi
boolean
yinyani
Ivala imodal xa iqhosha lokubaleka licofa
ingqalelo
boolean
yinyani
Ibeka ugqaliselo kwimodali xa iqaliswa.
bonisa
boolean
yinyani
Ibonisa imodali xa iqaliswa.
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 .
Jonga uxwebhu lwethu lweJavaScript ngolwazi olungakumbi.
Yenza umxholo wakho ube yimodali. Yamkela iinketho ozikhethelayo object
.
Khuphela
$ ( '#myModal' ). modal ({
keyboard : false
})
Yenza imodal ngesandla. Ubuyela kumnxeba phambi kokuba imodali iboniswe okanye ifihliwe (okt phambi kokuba isiganeko shown.bs.modal
okanye hidden.bs.modal
isiganeko senzeke).
Khuphela
$ ( '#myModal' ). modal ( 'toggle' )
Ivula imodali ngesandla. Ubuyela kumnxeba phambi kokuba imodali iboniswe (okt phambi kokuba shown.bs.modal
isiganeko senzeke).
Khuphela
$ ( '#myModal' ). modal ( 'show' )
Ufihla imodali ngesandla. Ibuyisela kumnxebi phambi kokuba imodali ifihlwe (okt phambi kokuba hidden.bs.modal
isiganeko senzeke).
Khuphela
$ ( '#myModal' ). modal ( 'hide' )
lungisa ngokuzenzela indawo yemodali ukuba umphakamo wemodali uyatshintsha ngelixa ivuliwe (okt xa kuvela ibar yokuskrola).
Khuphela
$ ( '#myModal' ). modal ( 'handleUpdate' )
Uyonakalisa imodali yento.
Iklasi ye-modal ye-Bootstrap iveza iminyhadala embalwa yokuqhagamshelwa ekusebenzeni kwe-modal. Zonke iziganeko zemodal ziyagxothwa kwimodali ngokwayo (okt kwi <div class="modal">
).
Uhlobo loMnyhadala
Inkcazo
bonisa.bs.modal
Esi siganeko sivutha ngokukhawuleza xa show
kubizwa indlela yomzekelo. Ukuba kubangelwa ngonqakrazo, into ecofayo iyafumaneka relatedTarget
njengepropathi yesiganeko.
ebonisiweyo.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.
fihla.bs.modal
Esi siganeko sigxothwa ngokukhawuleza xa hide
indlela yomzekelo ibiziwe.
efihliweyo.bs.modal
Esi siganeko sigxothwa xa i-modal igqibile ukufihlwa kumsebenzisi (iya kulinda iinguqu zeCSS ukuba zigqibe).
Khuphela
$ ( '#myModal' ). on ( 'hidden.bs.modal' , function ( e ) {
// do something...
})