Source

Modal

Sèvi ak plugin modal JavaScript Bootstrap pou ajoute dyalòg sou sit ou a pou bwat limyè, notifikasyon itilizatè, oswa kontni konplètman koutim.

Ki jan li fonksyone

Anvan w kòmanse ak eleman modal Bootstrap la, asire w ke ou li sa ki annapre yo paske opsyon meni nou yo te chanje dènyèman.

  • Modèl yo bati ak HTML, CSS, ak JavaScript. Yo ap pozisyone sou tout lòt bagay nan dokiman an epi retire woulo liv la <body>pou kontni modal woule pito.
  • Klike sou "backdrop" modal la pral otomatikman fèmen modal la.
  • Bootstrap sipòte sèlman yon fenèt modal nan yon moman. Modal anbrike yo pa sipòte paske nou kwè yo se eksperyans itilizatè pòv yo.
  • Modèl itilize position: fixed, ki pafwa ka yon ti jan patikilye sou rann li yo. Chak fwa sa posib, mete HTML modal ou a nan yon pozisyon wo nivo pou evite entèferans potansyèl de lòt eleman. Ou pral gen anpil chans rankontre pwoblèm lè nidifikasyon yon .modalnan yon lòt eleman fiks.
  • Yon fwa ankò, akòz position: fixed, gen kèk opozisyon ak lè l sèvi avèk modal sou aparèy mobil. Gade dokiman sipò navigatè nou an pou plis detay.
  • Akòz fason HTML5 defini semantik li yo, atribi autofocusHTML la pa gen okenn efè nan modal Bootstrap. Pou reyalize menm efè a, sèvi ak kèk JavaScript koutim:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Kontinye lekti pou demonstrasyon ak direktiv itilizasyon.

Egzanp yo

Anba la a se yon egzanp modal estatikposition (sa vle di li yo ak displayyo te remplacé). Gen ladann header modal la, kò modal (obligatwa pou padding), ak modal footer (si ou vle). Nou mande pou w mete en-tête modal ak aksyon ranvwaye chak fwa sa posib, oswa bay yon lòt aksyon eksplisit ranvwaye.

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

Live Demo

Aktive yon demonstrasyon modal k ap travay lè w klike sou bouton ki anba a. Li pral glise desann ak fennen soti nan tèt paj la.

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

Defile kontni long

Lè modal yo vin twò long pou fenèt itilizatè a oswa aparèy, yo defile endepandan de paj la li menm. Eseye Demo ki anba a pou wè sa nou vle di.

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

Vètikalman santre

Add .modal-dialog-centeredto .modal-dialoga vètikal sant modal la.

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

Konsèy zouti ak popovers

Ti konsèy ak popovers yo ka mete nan modal jan sa nesesè. Lè modal yo fèmen, nenpòt konsèy sou zouti ak popover nan yo tou otomatikman ranvwaye.

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

Sèvi ak kadriyaj la

Sèvi ak sistèm kadriyaj Bootstrap la nan yon modal pa nidifikasyon .container-fluidnan .modal-body. Lè sa a, sèvi ak klas sistèm kadriyaj nòmal yo menm jan ou ta nenpòt lòt kote.

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

Kontni modal varye

Gen yon pakèt bouton ki tout deklanche menm modal la ak kontni yon ti kras diferan? Sèvi event.relatedTargetak ak HTML data-*atribi (petèt atravè jQuery ) yo varye sa ki nan modal la depann sou ki bouton yo te klike.

Anba la a se yon Demo ap viv ki te swiv pa egzanp HTML ak JavaScript. Pou plis enfòmasyon, li dokiman evènman modal yo pou plis detay sou 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)
})

Retire animasyon

Pou modal ki tou senpleman parèt olye ke fennen nan yo wè, retire .fadeklas la nan maketing modal ou a.

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

Wotè dinamik

Si wotè yon modal chanje pandan li louvri, ou ta dwe rele $('#myModal').modal('handleUpdate')pou reajiste pozisyon modal la nan ka yon defile parèt.

Aksesiblite

Asire w ou ajoute role="dialog"ak aria-labelledby="...", referans tit modal la, nan .modal, ak role="document"nan .modal-dialogtèt li. Anplis de sa, ou ka bay yon deskripsyon dyalòg modal ou a ak aria-describedbysou .modal.

Anrejistre videyo YouTube yo

Anrejistre videyo YouTube nan modal mande pou JavaScript adisyonèl ki pa nan Bootstrap otomatikman sispann lekti ak plis ankò. Gade pòs itil Stack Overflow sa a pou plis enfòmasyon.

Gwosè si ou vle

Modal yo gen de gwosè opsyonèl, ki disponib atravè klas modifye yo dwe mete sou yon .modal-dialog. Gwosè sa yo choute nan sèten pwen rupture pou fè pou evite ba defile orizontal sou vi ki pi etwat.

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

Itilizasyon

Plugin modal la chanje kontni kache ou sou demann, atravè atribi done oswa JavaScript. Li ajoute tou .modal-openpou <body>pase sou konpòtman defile default ak jenere yon .modal-backdroppou bay yon zòn klike sou pou ranvwaye modal yo montre lè klike deyò modal la.

Via atribi done yo

Aktive yon modal san w pa ekri JavaScript. Mete data-toggle="modal"sou yon eleman kontwolè, tankou yon bouton, ansanm ak yon data-target="#foo"oswa href="#foo"pou vize yon modal espesifik pou activer.

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

Via JavaScript

Rele yon modal ak id myModalak yon sèl liy JavaScript:

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

Opsyon

Opsyon yo ka pase atravè atribi done oswa JavaScript. Pou atribi done, ajoute non opsyon a nan data-, tankou nan data-backdrop="".

Non Kalite Default Deskripsyon
seri boolean oswa fisèl la'static' vre Gen ladann yon eleman modal-backdrop. Altènativman, presize staticpou yon seri ki pa fèmen modal la sou klike sou.
klavye booleyen vre Fèmen modal la lè yo peze kle chape
konsantre booleyen vre Mete konsantre sou modal la lè inisyalize.
montre booleyen vre Montre modal la lè inisyalize.

Metòd

Metòd asynchrone ak tranzisyon

Tout metòd API yo asenkron epi yo kòmanse yon tranzisyon . Yo retounen kote moun k ap rele a le pli vit ke tranzisyon an kòmanse men anvan li fini . Anplis de sa, yo pral inyore yon apèl metòd sou yon eleman tranzisyon .

Gade dokiman JavaScript nou an pou plis enfòmasyon.

.modal(options)

Aktive kontni ou kòm yon modal. Aksepte yon opsyon opsyonèl object.

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

.modal('toggle')

Manyèlman aktive yon modal. Retounen bay moun kap rele a anvan modal la aktyèlman te montre oswa kache (sa vle di anvan evènman shown.bs.modalan hidden.bs.modalrive).

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

.modal('show')

Manyèlman ouvè yon modal. Retounen bay moun k ap rele a anvan yo montre modal la (sa vle di anvan shown.bs.modalevènman an rive).

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

.modal('hide')

Manyèlman kache yon modal. Retounen bay moun kap rele a anvan modal la te kache (sa vle di anvan hidden.bs.modalevènman an rive).

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

.modal('handleUpdate')

Manyèlman reajiste pozisyon modal la si wotè yon modal chanje pandan li ouvri (sa vle di nan ka yon defile parèt).

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

.modal('dispose')

Detwi modal yon eleman.

Evènman

Klas modal Bootstrap a ekspoze kèk evènman pou branche nan fonksyonalite modal. Tout evènman modal yo tire sou modal la li menm (sa vle di nan la <div class="modal">).

Kalite Evènman Deskripsyon
montre.bs.modal Evènman sa a dife imedyatman lè yo showrele metòd egzanp lan. Si sa te koze pa yon klik, eleman klike la disponib kòm relatedTargetpwopriyete evènman an.
montre.bs.modal Evènman sa a revoke lè modal la vin vizib pou itilizatè a (ap tann tranzisyon CSS yo fini). Si sa te koze pa yon klik, eleman klike la disponib kòm relatedTargetpwopriyete evènman an.
kache.bs.modal Evènman sa a te tire imedyatman lè yo hidete rele metòd egzanp lan.
hidden.bs.modal Evènman sa a revoke lè modal la fini kache pou itilizatè a (ap tann pou tranzisyon CSS fini).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})