Source

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 .modalngaphakathi 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- autofocusHTML alunasiphumo kwiimodyuli zeBootstrap. Ukufezekisa umphumo ofanayo, sebenzisa iJavaScript yesiko:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Qhubeka ufunda iidemos kunye nezikhokelo zokusetyenziswa.

Imizekelo

Apha ngezantsi kukho umzekelo wemodyuli engatshintshiyo (ithetha ukuba yona positionkwaye displayiye 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.

<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">&times;</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-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Idemo ephilayo

Guqula idemo yendlela yokusebenza ngokunqakraza iqhosha elingezantsi. Iza kutyibilika phantsi kwaye iphele ukusuka phezulu ephepheni.

<!-- 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">&times;</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 umxholo omde

Xa iimodyuli zinde kakhulu kwimboniselo yomsebenzisi okanye isixhobo, ziyaskrola ngaphandle kwephepha ngokwalo. Zama idemo engezantsi ukuze ubone ukuba sithetha ukuthini.

<!-- 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">&times;</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>

Ngokuthe nkqo embindini

Yongeza kumbindi .modal-dialog-centeredothe .modal-dialognkqo wemodali.

<!-- 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="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</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

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-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-fluidngaphakathi 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 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.relatedTargetkunye 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.

<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">&times;</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)
})

Susa oopopayi

Kwiimodyuli ezibonakala ngokulula kunokuba ziphele ukubonwa, susa .fadeiklasi kuphawu lwakho lwemodal.

<div class="modal" tabindex="-1" role="dialog" aria-labelledby="..." aria-hidden="true">
  ...
</div>

Ukuphakama okunamandla

Ukuba umphakamo wemodali uyatshintsha ngelixa ivuliwe, kuya kufuneka ufowunele $('#myModal').modal('handleUpdate')ukulungisa indawo yemodali kwimeko yokuskrola.

Ukufikeleleka

Qinisekisa ukuba wongeza role="dialog"kunye aria-labelledby="...", ubhekisa kwisihloko semodal, ku .modal, kwaye role="document"kuyo .modal-dialogngokwayo. Ukongeza, unganika inkcazo yencoko yababini yemodal nge aria-describedbykwi .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.

Iisayizi ozikhethelayo

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.

<!-- 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>

Ukusetyenziswa

I-plugin ye-modal iguqula umxholo wakho ofihliweyo kwimfuno, ngeempawu zedatha okanye iJavaScript. Yongeza kwakhona .modal-openkwi- <body>override default scrolling behavior kwaye yenza i- .modal-backdropa ukunika indawo yokucofa ukugxotha iimodyuli ezibonisiweyo xa ucofa ngaphandle kwemodali.

Ngeempawu zedatha

Vula imodali ngaphandle kokubhala iJavaScript. Cwangcisa data-toggle="modal"isiqalelo somlawuli, njengeqhosha, kunye ne data-target="#foo"okanye href="#foo"ukujolisa kwindlela ethile yokuguqula.

<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>

NgeJavaScript

Fowunela i-modal ene-id myModalngomgca omnye weJavaScript:

$('#myModal').modal(options)

Iinketho

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 staticimeko 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

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.

.modal(options)

Yenza umxholo wakho ube yimodali. Yamkela iinketho ozikhethelayo object.

$('#myModal').modal({
  keyboard: false
})

.modal('toggle')

Yenza imodal ngesandla. Ubuyela kumnxeba phambi kokuba imodali iboniswe okanye ifihliwe (okt phambi kokuba isiganeko shown.bs.modalokanye hidden.bs.modalisiganeko senzeke).

$('#myModal').modal('toggle')

.modal('show')

Ivula imodali ngesandla. Ubuyela kumnxeba phambi kokuba imodali iboniswe (okt phambi kokuba shown.bs.modalisiganeko senzeke).

$('#myModal').modal('show')

.modal('hide')

Ufihla imodali ngesandla. Ibuyisela kumnxebi phambi kokuba imodali ifihlwe (okt phambi kokuba hidden.bs.modalisiganeko senzeke).

$('#myModal').modal('hide')

.modal('handleUpdate')

lungisa ngokuzenzela indawo yemodali ukuba umphakamo wemodali uyatshintsha ngelixa ivuliwe (okt xa kuvela ibar yokuskrola).

$('#myModal').modal('handleUpdate')

.modal('dispose')

Uyonakalisa imodali yento.

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 loMnyhadala Inkcazo
bonisa.bs.modal Esi siganeko sivutha ngokukhawuleza xa showkubizwa indlela yomzekelo. Ukuba kubangelwa ngonqakrazo, into ecofayo iyafumaneka relatedTargetnjengepropathi 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 relatedTargetnjengepropathi yesiganeko.
fihla.bs.modal Esi siganeko sigxothwa ngokukhawuleza xa hideindlela yomzekelo ibiziwe.
efihliweyo.bs.modal Esi siganeko sigxothwa xa i-modal igqibile ukufihlwa kumsebenzisi (iya kulinda iinguqu zeCSS ukuba zigqibe).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})