SourceI-Modal
Sebenzisa i-plugin ye-modal ye-Bootstrap ye-JavaScript ukwengeza izingxoxo kusayithi lakho ukuze uthole amabhokisi okukhanya, izaziso zabasebenzisi, noma okuqukethwe ngokwezifiso ngokuphelele.
Ngaphambi kokuthi uqale nge-modal ye-Bootstrap, qiniseka ukuthi ufunda okulandelayo njengoba izinketho zethu zemenyu zisanda kushintsha.
Amamodeli akhiwe nge-HTML, CSS, neJavaScript. Abekwe phezu kwayo yonke enye into kudokhumenti futhi asuse ukuskrola kokuthi <body>
okuqukethwe kwe-modal kuskrole esikhundleni salokho.
Ukuchofoza "okungemuva" kwe-modal kuzovala ngokuzenzakalelayo i-modal.
I-Bootstrap isekela kuphela iwindi lemodali eyodwa ngesikhathi. Amamodeli afakwe esidlekeni awasekelwa njengoba sikholelwa ukuthi awamazi kahle abasebenzisi.
Amamodali asebenzisa position: fixed
, okungenzeka ukuthi ngezinye izikhathi athize mayelana nokunikezwa kwawo. Noma nini lapho kunokwenzeka, beka i-modal yakho ye-HTML endaweni yezinga eliphezulu ukuze ugweme ukuphazamiseka okungaba khona okuvela kwezinye izici. Cishe uzohlangabezana nezinkinga lapho wenza isidleke .modal
phakathi kwenye into elungisiwe.
Nakulokhu, ngenxa position: fixed
ye-, kuneminye imixwayiso ngokusebenzisa ama-modal kumadivayisi eselula. Bona amadokhumenti asekela isiphequluli sethu ukuze uthole imininingwane.
Ngenxa yokuthi i-HTML5 iyichaza kanjani i-semantics yayo, isibaluli se- autofocus
HTML asinawo umthelela kumamodeli we-Bootstrap. Ukuze uthole umphumela ofanayo, sebenzisa i-JavaScript yangokwezifiso:
Kopisha
$ ( '#myModal' ). on ( 'shown.bs.modal' , function () {
$ ( '#myInput' ). trigger ( 'focus' )
})
Qhubeka ufunda amademo nemihlahlandlela yokusebenzisa.
Ngezansi isibonelo se-modal esimile position
(okusho ukuthi saso futhi display
sikhishiwe). Kufakwe unhlokweni we-modal, umzimba we-modal (uyadingeka padding
), kanye nonyaweni we-modal (ongakukhetha). Sicela ukuthi ufake izihloko ze-modal ezinezenzo zokucashisa noma nini lapho kunokwenzeka, noma unikeze esinye isenzo sokuchitha esisobala.
Umbhalo womzimba we-Modal ungena lapha.
Kopisha
<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 yemodi yokusebenza ngokuchofoza inkinobho engezansi. Izoshelela phansi bese ifiphala kusukela phezulu kwekhasi.
Woohoo, you're reading this text in a modal!
Yethula imodeli yedemo
Kopisha
<!-- 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>
Ukuskrola okuqukethwe okude
Uma amamodeli eba made kakhulu endaweni yokubuka yomsebenzisi noma idivayisi, bayaskrola ngaphandle kwekhasi ngokwalo. Zama idemo engezansi ukuze ubone ukuthi siqonde 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.
Yethula imodeli yedemo
Kopisha
<!-- 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>
Engeza .modal-dialog-centered
ukuze .modal-dialog
umise phakathi nendawo imodali ngokuqondile.
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.
Yethula imodeli yedemo
Kopisha
<!-- 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>
Izeluleko zamathuluzi kanye ne- popover zingafakwa ngaphakathi kwamamodeli njengoba kudingeka. Uma amamodeli avaliwe, noma yimaphi amathiphu nama-popover ngaphakathi nawo achithwa ngokuzenzakalelayo.
Popover in a modal
This button triggers a popover on click.
Tooltips in a modal
This link and that link have tooltips on hover.
Yethula imodeli yedemo
Kopisha
<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 isistimu yegridi ye-Bootstrap ngaphakathi kwe-modal ngokuzalela .container-fluid
ngaphakathi kwe- .modal-body
. Bese, sebenzisa amakilasi esistimu yegridi evamile njengoba ubungenza 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
Yethula imodeli yedemo
Kopisha
<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>
Ukushintshashintsha kokuqukethwe kwe-modal
Unenqwaba yezinkinobho zonke ezicupha i-modal efanayo ngokuqukethwe okuhluke kancane? Sebenzisa event.relatedTarget
kanye nezibaluli ze- HTMLdata-*
(mhlawumbe nge-jQuery ) ukuze uguqule okuqukethwe kwe-modal kuye ngokuthi iyiphi inkinobho echofozwe.
Ngezansi kunedemo ebukhoma elandelwa isibonelo se-HTML ne-JavaScript. Ukuze uthole ulwazi olwengeziwe, funda amadokhumenti emicimbi ye-modal ukuze uthole imininingwane kokuthi relatedTarget
.
Vula i-modal ye-@mdo
Vula i-modal ye-@fat
Vula i-modal ye-@getbootstrap
Kopisha
<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>
Kopisha
$ ( '#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 )
})
Kumamodeli avele avele kunokuba afiphele ukuze abukwe, susa .fade
ikilasi kumakhaphu akho e-modal.
Kopisha
<div class= "modal" tabindex= "-1" role= "dialog" aria-labelledby= "..." aria-hidden= "true" >
...
</div>
Uma ukuphakama kwe-modal kushintsha ngenkathi ivuliwe, kufanele ushayele $('#myModal').modal('handleUpdate')
ukuze ulungise indawo ye-modal uma kwenzeka kuvela ibha yokuskrola.
Qiniseka ukuthi wengeza role="dialog"
futhi aria-labelledby="..."
, ubhekisela kusihloko se-modal, ku- , .modal
nakusona uqobo. Ukwengeza, unganikeza incazelo yengxoxo yakho ye-modal nge- on .role="document"
.modal-dialog
aria-describedby
.modal
Ishumeka amavidiyo e-YouTube
Ukushumeka amavidiyo e-YouTube kumamodeli kudinga i-JavaScript eyengeziwe engekho ku-Bootstrap ukuze kumiswe ukudlalwa ngokuzenzakalelayo nokunye. Bona lokhu okuthunyelwe okuchichima kwesitaki okuwusizo ukuze uthole ulwazi olwengeziwe.
Amamodeli anosayizi ababili abazikhethela, atholakala ngamakilasi okulungisa azobekwa ku- .modal-dialog
. Laba bosayizi bangena ezindaweni ezithile zokuphumula ukuze bagweme amabha okuskrola avundlile ezimbobeni zokubuka ezinciphile.
I-modal enkulu
Imodeli encane
Kopisha
<!-- 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 okuqukethwe kwakho okufihliwe ngokufunwa, ngezibaluli zedatha noma i-JavaScript. Iphinde yengeze .modal-open
ekubhaleni phezu <body>
kokuziphatha kokuskrola okuzenzakalelayo futhi ikhiqize .modal-backdrop
ukuhlinzeka ngendawo yokuchofoza yokucashisa amamodeli abonisiwe lapho uchofozwa ngaphandle kwe-modal.
Yenza kusebenze i-modal ngaphandle kokubhala i-JavaScript. Misa data-toggle="modal"
into yesilawuli, njengenkinobho, kanye ne-a data-target="#foo"
noma href="#foo"
ukuze uqondise i-modal ethile ukuze uyiguqule.
Kopisha
<button type= "button" data-toggle= "modal" data-target= "#myModal" > Launch modal</button>
Shayela i-modal ene-id myModal
enomugqa owodwa we-JavaScript:
Kopisha
$ ( '#myModal' ). modal ( options )
Izinketho zingadluliswa ngezibaluli zedatha noma i-JavaScript. Ukuze uthole izibaluli zedatha, faka igama lenketho kokuthi data-
, njengaku data-backdrop=""
.
Igama
Uhlobo
Okuzenzakalelayo
Incazelo
okwasemuva
i-boolean noma intambo'static'
iqiniso
Kufaka phakathi i-elementi ye-modal-backdrop. Noma, cacisa static
okwasemuva okungavali i-modal lapho uchofozwa.
ikhibhodi
boolean
iqiniso
Ivala i-modal lapho ukhiye we-escape ucindezelwa
gxila
boolean
iqiniso
Ibeka ukugxila ku-modal lapho iqaliswa.
umbukiso
boolean
iqiniso
Ibonisa i-modal lapho iqaliswa.
Yenza okuqukethwe kwakho kusebenze njengemodeli. Yamukela izinketho ozikhethela object
.
Kopisha
$ ( '#myModal' ). modal ({
keyboard : false
})
Iguqula ngokwenza i-modal. Ibuyela kofonayo ngaphambi kokuba imodali ikhonjiswe noma ifihlwe (okungukuthi ngaphambi kokuba kwenzeke shown.bs.modal
noma hidden.bs.modal
isigameko).
Kopisha
$ ( '#myModal' ). modal ( 'toggle' )
Ivula mathupha i-modal. Ibuyela kofonayo ngaphambi kokuthi imodali ikhonjiswe (okungukuthi ngaphambi kokuba shown.bs.modal
umcimbi wenzeke).
Kopisha
$ ( '#myModal' ). modal ( 'show' )
Ifihla i-modal ngokwenza. Ibuyela kofonayo ngaphambi kokuthi i-modal ifihlekile (okungukuthi ngaphambi kokuthi hidden.bs.modal
kwenzeke umcimbi).
Kopisha
$ ( '#myModal' ). modal ( 'hide' )
lungisa mathupha indawo ye-modal uma ukuphakama kwe-modal kushintsha ngenkathi ivuliwe (okungukuthi uma kwenzeka kuvela ibha yokuskrola).
Kopisha
$ ( '#myModal' ). modal ( 'handleUpdate' )
Icekela phansi i-modal yento ethile.
Ikilasi le-modal ye-Bootstrap lidalula imicimbi embalwa ukuze ixhume ekusebenzeni kwe-modal. Yonke imicimbi ye-modal ixoshwa ku-modal ngokwayo (okungukuthi ku- <div class="modal">
).
Uhlobo Lomcimbi
Incazelo
show.bs.modal
Lo mcimbi uvutha ngokushesha lapho show
indlela yesibonelo ibizwa. Uma kubangelwa ukuchofoza, into echofoziwe iyatholakala njengempahla relatedTarget
yomcimbi.
okubonisiwe.bs.modali
Lo mcimbi uyaxoshwa uma i-modal yenziwe yabonakala kumsebenzisi (izolinda ukuthi ukuguqulwa kwe-CSS kuqedwe). Uma kubangelwa ukuchofoza, into echofoziwe iyatholakala njengempahla relatedTarget
yomcimbi.
fihla.bs.modal
Lo mcimbi uxoshwa ngokushesha lapho hide
indlela yesibonelo ibizwa.
kufihliwe.bs.modal
Lo mcimbi uyaxoshwa lapho i-modal isiqedile ukufihlwa kumsebenzisi (izolinda ukuthi ukuguqulwa kwe-CSS kuqedwe).
Kopisha
$ ( '#myModal' ). on ( 'hidden.bs.modal' , function ( e ) {
// do something...
})