Source

Modali

Gwiritsani ntchito pulogalamu yowonjezera ya JavaScript ya Bootstrap kuti muwonjezere zokambirana patsamba lanu la mabokosi opepuka, zidziwitso za ogwiritsa ntchito, kapena zomwe mumakonda.

Momwe zimagwirira ntchito

Musanayambe ndi gawo la Bootstrap, onetsetsani kuti mwawerenga zotsatirazi popeza zosankha zathu zasintha posachedwa.

  • Ma modals amapangidwa ndi HTML, CSS, ndi JavaScript. Iwo ali pamwamba pa china chirichonse mu chikalatacho ndi kuchotsa mpukutu <body>kuti modal zolembedwa mipukutu m'malo.
  • Kusindikiza pa modal "backdrop" kudzatseka modal.
  • Bootstrap imathandizira zenera limodzi lokha nthawi imodzi. Makhalidwe a Nested sagwiritsidwa ntchito chifukwa timakhulupirira kuti sagwiritsa ntchito bwino.
  • Ma Modals amagwiritsa ntchito position: fixed, zomwe nthawi zina zimatha kukhala zodziwika bwino pakumasulira kwake. Ngati n'kotheka, ikani HTML yanu pamalo apamwamba kuti musasokonezedwe ndi zinthu zina. Mutha kukumana ndi zovuta mukamanga zisa .modalmkati mwa chinthu china chokhazikika.
  • Apanso, chifukwa cha position: fixed, pali machenjezo ogwiritsira ntchito ma modals pazida zam'manja. Onani zolemba zathu zothandizira msakatuli kuti mumve zambiri.
  • Chifukwa cha momwe HTML5 imafotokozera semantics yake, mawonekedwe a autofocusHTML alibe mphamvu mu ma modals a Bootstrap. Kuti mukwaniritse zomwezo, gwiritsani ntchito JavaScript yokhazikika:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Pitilizani kuwerenga ma demo ndi malangizo ogwiritsira ntchito.

Zitsanzo

Pansipa pali static modal chitsanzo (kutanthauza zake positionndipo displayzasinthidwa). Kuphatikizidwa ndi mutu wa modal, thupi la modal (lofunikira padding) ndi modal footer (posankha). Tikukupemphani kuti muphatikizepo mitu yamutu yokhala ndi zochita zochotsa ngati kuli kotheka, kapena muperekepo kanthu kena kochotsa.

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

Chiwonetsero chamoyo

Sinthani mawonekedwe ogwirira ntchito podina batani pansipa. Idzatsika ndikuzimiririka kuchokera pamwamba pa tsamba.

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

Kusuntha zinthu zazitali

Ma mods akakhala ataliatali kwambiri kuti ayang'ane ndi wogwiritsa ntchito kapena chipangizo, amasuntha mosadalira tsambalo. Yesani chiwonetsero pansipa kuti muwone zomwe tikutanthauza.

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

Chokhazikika pakati

Onjezani .modal-dialog-centeredku .modal-dialogmolunjika pakati pa modali.

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

Tooltips ndi popovers

Zida ndi popovers zitha kuyikidwa m'ma modal ngati pakufunika . Ma modals akatsekedwa, zida zilizonse ndi ma popovers mkati mwake amachotsedwanso.

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

Kugwiritsa ntchito grid

Gwiritsani ntchito gridi ya Bootstrap mkati mwa modal pomanga zisa .container-fluidmkati mwa .modal-body. Kenako, gwiritsani ntchito makalasi amtundu wa grid monga momwe mungachitire kwina kulikonse.

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

Mitundu yosiyanasiyana ya modal

Muli ndi mabatani angapo omwe onse amayambitsa modali yofanana ndi zomwe zili zosiyana pang'ono? Gwiritsani ntchito event.relatedTargetndi mawonekedwe a HTMLdata-* (mwina kudzera pa jQuery ) kuti musinthe zomwe zili mu modal kutengera batani lomwe ladina.

Pansipa pali chiwonetsero chamoyo chotsatiridwa ndi chitsanzo cha HTML ndi JavaScript. Kuti mumve zambiri, werengani zolemba zamodal zochitika kuti mumve zambiri 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)
})

Chotsani makanema ojambula

Kwa ma mods omwe amangowoneka m'malo mozimiririka kuti awoneke, chotsani .fadekalasiyo pazolemba zanu.

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

Matali amphamvu

Ngati kutalika kwa modal kumasintha pamene ili lotseguka, muyenera kuyimba $('#myModal').modal('handleUpdate')kuti mukonzenso malo a modal ngati scrollbar ikuwoneka.

Kufikika

Onetsetsani kuti muwonjezere role="dialog"ndi aria-labelledby="...", kutanthauza mutu wa modal, ku .modal, ndi role="document"pawokha .modal-dialog. Kuphatikiza apo, mutha kufotokoza za dialog yanu ya modal ndi aria-describedbypa .modal.

Kuyika makanema a YouTube

Kuyika makanema a YouTube mu ma modals kumafuna JavaScript yowonjezera osati mu Bootstrap kuti asiye kusewera ndi zina zambiri. Onani positi yothandiza ya Stack Overflow kuti mumve zambiri.

Zosankha zazikulu

Ma Modals ali ndi makulidwe awiri osankha, omwe amapezeka kudzera pamagulu osintha kuti ayikidwe pa .modal-dialog. Kukula uku kumayambira pazipata zina kuti mupewe zopingasa zopingasa pamawonekedwe ocheperako.

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

Kugwiritsa ntchito

Pulogalamu yowonjezera ya modal imasintha zinthu zanu zobisika mukafuna, kudzera pa data kapena JavaScript. Imawonjezeranso .modal-openkusokoneza machitidwe <body>opukutira osasintha ndikupanga a .modal-backdropkuti apereke malo odina kuti muchotse ma modal owonetsedwa mukadina kunja kwa modal.

Kudzera muzochita za data

Yambitsani modali osalemba JavaScript. Khazikitsani data-toggle="modal"chinthu chowongolera, ngati batani, limodzi ndi data-target="#foo"kapena href="#foo"kutsata njira inayake kuti musinthe.

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

Kudzera pa JavaScript

Imbani modal yokhala ndi id myModalyokhala ndi mzere umodzi wa JavaScript:

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

Zosankha

Zosankha zitha kuperekedwa kudzera pa data kapena JavaScript. Pamawonekedwe a data, yonjezerani dzina lachisankho ku data-, monga mu data-backdrop="".

Dzina Mtundu Zosasintha Kufotokozera
kumbuyo boolean kapena chingwe'static' zoona Mulinso modal-backdrop element. Kapenanso, tchulani staticzakumbuyo zomwe sizitseka moduli mukadina.
kiyibodi boolean zoona Amatseka modali pamene kiyi yothawa ikanikizidwa
kuganizira boolean zoona Imayika chidwi pa modal ikakhazikitsidwa.
chiwonetsero boolean zoona Imawonetsa modali ikayambika.

Njira

Asynchronous njira ndi kusintha

Njira zonse za API ndizosasinthika ndipo zimayamba kusintha . Amabwerera kwa woyimbirayo atangoyamba kusintha koma asanathe . Kuonjezera apo, kuyitana kwa njira pa gawo losintha kudzanyalanyazidwa .

Onani zolemba zathu za JavaScript kuti mudziwe zambiri.

.modal(options)

Imatsegula zomwe zili ngati modal. Imavomereza zomwe mungachite object.

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

.modal('toggle')

Imatembenuza pamanja modali. Imabwereranso kwa woyimba foniyo isanasonyezedwe kapena kubisidwa (ie zisanachitike shown.bs.modalkapena hidden.bs.modalchochitikacho).

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

.modal('show')

Pamanja amatsegula modali. Imabwereranso kwa woyimba foniyo isanasonyezedwe (ie zisanachitike shown.bs.modal).

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

.modal('hide')

Amabisa pamanja modali. Imabwereranso kwa woyimbira foniyo isanabisike (ie zisanachitike hidden.bs.modal).

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

.modal('handleUpdate')

Sinthani pamanja malo a modal ngati kutalika kwa modali kukusintha pamene ili lotseguka (mwachitsanzo, ngati mpukutu utawonekera).

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

.modal('dispose')

Imawononga modal ya chinthu.

Zochitika

Kalasi ya modal ya Bootstrap imawulula zochitika zingapo kuti zigwirizane ndi machitidwe a modal. Zochitika zonse za modal zimachotsedwa pa modal yokha (ie pa <div class="modal">).

Mtundu wa Chochitika Kufotokozera
show.bs.modal Chochitika ichi chimayaka nthawi yomweyo shownjira yachitsanzo itayitanidwa. Ngati chifukwa cha kudina, chinthu chomwe chadina chimapezeka ngati relatedTargetkatundu wa chochitikacho.
zowonetsedwa.bs.modal Chochitikachi chimachotsedwa pamene modal yawonetsedwa kwa wogwiritsa ntchito (idzadikirira kuti kusintha kwa CSS kumalize). Ngati chifukwa cha kudina, chinthu chodina chimapezeka ngati relatedTargetkatundu wa chochitikacho.
hide.bs.modal Chochitika ichi chimachotsedwa nthawi yomweyo pamene hidenjira yachitsanzo yayitanidwa.
zobisika.bs.modal Chochitikachi chimachotsedwa pamene modal yatha kubisidwa kwa wogwiritsa ntchito (idikira kuti kusintha kwa CSS kumalize).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})