Source

I-Modal

Sebenzisa i-plugin ye-modal ye-Bootstrap ye-JavaScript ukwengeza izingxoxo kusayithi lakho ukuze uthole amabhokisi okukhanya, izaziso zabasebenzisi, noma okuqukethwe ngokwezifiso ngokuphelele.

Isebenza kanjani

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 .modalphakathi kwenye into elungisiwe.
  • Nakulokhu, ngenxa position: fixedye-, 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- autofocusHTML asinawo umthelela kumamodeli we-Bootstrap. Ukuze uthole umphumela ofanayo, sebenzisa i-JavaScript yangokwezifiso:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Qhubeka ufunda amademo nemihlahlandlela yokusebenzisa.

Izibonelo

Ngezansi isibonelo se-modal esimileposition (okusho ukuthi saso futhi displaysikhishiwe). 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.

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

Guqula idemo yemodi yokusebenza ngokuchofoza inkinobho engezansi. Izoshelela phansi bese ifiphala kusukela phezulu kwekhasi.

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

Uma amamodeli eba made kakhulu endaweni yokubuka yomsebenzisi noma idivayisi, bayaskrola ngaphandle kwekhasi ngokwalo. Zama idemo engezansi ukuze ubone ukuthi siqonde 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>

Kuphakathi nendawo

Engeza .modal-dialog-centeredukuze .modal-dialogumise phakathi nendawo imodali ngokuqondile.

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

Amathuluzi kanye popover

Izeluleko zamathuluzi kanye ne- popover zingafakwa ngaphakathi kwamamodeli njengoba kudingeka. Uma amamodeli avaliwe, noma yimaphi amathiphu nama-popover ngaphakathi nawo achithwa ngokuzenzakalelayo.

<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 isistimu yegridi ye-Bootstrap ngaphakathi kwe-modal ngokuzalela .container-fluidngaphakathi kwe- .modal-body. Bese, sebenzisa amakilasi esistimu yegridi evamile njengoba ubungenza 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>

Ukushintshashintsha kokuqukethwe kwe-modal

Unenqwaba yezinkinobho zonke ezicupha i-modal efanayo ngokuqukethwe okuhluke kancane? Sebenzisa event.relatedTargetkanye 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.

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

Kumamodeli avele avele kunokuba afiphele ukuze abukwe, susa .fadeikilasi kumakhaphu akho e-modal.

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

Ukuphakama okunamandla

Uma ukuphakama kwe-modal kushintsha ngenkathi ivuliwe, kufanele ushayele $('#myModal').modal('handleUpdate')ukuze ulungise indawo ye-modal uma kwenzeka kuvela ibha yokuskrola.

Ukufinyeleleka

Qiniseka ukuthi wengeza role="dialog"futhi aria-labelledby="...", ubhekisela kusihloko se-modal, ku- , .modalnakusona uqobo. Ukwengeza, unganikeza incazelo yengxoxo yakho ye-modal nge- on .role="document".modal-dialogaria-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.

Osayizi abazikhethela

Amamodeli anosayizi ababili abazikhethela, atholakala ngamakilasi okulungisa azobekwa ku- .modal-dialog. Lawa masayizi angena ezindaweni ezithile zokuphumula ukuze agweme amabha okuskrola avundlile ezimbobeni zokubuka ezincane.

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

Ukusetshenziswa

I-plugin ye-modal iguqula okuqukethwe kwakho okufihliwe ngokufunwa, ngokusebenzisa izibaluli zedatha noma i-JavaScript. Iphinde yengeze .modal-openekubhaleni phezu <body>kokuziphatha kokuskrola okuzenzakalelayo futhi ikhiqize .modal-backdropukuhlinzeka ngendawo yokuchofoza yokucashisa amamodeli abonisiwe lapho uchofozwa ngaphandle kwe-modal.

Ngezibaluli zedatha

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.

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

Nge-JavaScript

Shayela i-modal ene-id myModalenomugqa owodwa we-JavaScript:

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

Izinketho

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 Ifaka i-elementi ye-modal-backdrop. Noma, cacisa staticokwasemuva 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.

Izindlela

Izindlela ze-Asynchronous kanye noshintsho

Zonke izindlela ze-API azivumelanisi futhi ziqala inguquko . Abuyele kofonayo uma sekuqalwa uguquko kodwa lungakapheli . Ngaphezu kwalokho, ikholi yendlela engxenyeni yoshintsho izozitshwa .

Bona imibhalo yethu ye-JavaScript ukuze uthole ulwazi olwengeziwe.

.modal(options)

Yenza okuqukethwe kwakho kusebenze njengemodeli. Yamukela izinketho ozikhethela object.

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

.modal('toggle')

Iguqula ngokwenza i-modal. Ibuyela kofonayo ngaphambi kokuba imodali ikhonjiswe noma ifihlwe (okungukuthi ngaphambi kokuba kwenzeke shown.bs.modalnoma hidden.bs.modalisigameko).

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

.modal('show')

Ivula mathupha i-modal. Ibuyela kofonayo ngaphambi kokuthi imodali ikhonjiswe (okungukuthi ngaphambi kokuba shown.bs.modalumcimbi wenzeke).

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

.modal('hide')

Ifihla i-modal ngokwenza. Ibuyela kofonayo ngaphambi kokuthi i-modal ifihlekile (okungukuthi ngaphambi kokuba hidden.bs.modalkwenzeke umcimbi).

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

.modal('handleUpdate')

lungisa mathupha indawo ye-modal uma ukuphakama kwe-modal kushintsha ngenkathi ivuliwe (okungukuthi uma kwenzeka kuvela ibha yokuskrola).

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

.modal('dispose')

Icekela phansi i-modal yento ethile.

Imicimbi

Isigaba se-modal se-Bootstrap sidalula 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 showindlela yesibonelo ibizwa. Uma kubangelwa ukuchofoza, into echofoziwe iyatholakala njengempahla relatedTargetyomcimbi.
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 relatedTargetyomcimbi.
fihla.bs.modal Lo mcimbi uxoshwa ngokushesha lapho hideindlela yesibonelo ibizwa.
kufihliwe.bs.modal Lo mcimbi uyaxoshwa lapho i-modal isiqedile ukufihlwa kumsebenzisi (izolinda ukuthi ukuguqulwa kwe-CSS kuqedwe).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})