Source

Modal we de sho aw fɔ du am

Yuz Bootstrap in JavaSkript modal plɔgin fɔ ad dayalɔg dɛn to yu sayt fɔ laytbɔks, yuz notis, ɔ kɔmplit kɔstɔm kɔntinyu.

Aw i de wok

Bifo yu bigin wit Bootstrap in modal kɔmpɔnɛnt, mek shɔ se yu rid wetin de dɔŋ ya bikɔs wi mɛnyu opshɔn dɛn dɔn chenj i nɔ tu te yet.

  • Dɛn bil di mɔdal dɛn wit HTML, CSS, ɛn JavaSkript. Dɛn de posishun oba ɔl ɔda tin dɛn na di dɔkyumɛnt ɛn pul skrol frɔm di <body>so dat modal kɔntinyu skrol insted.
  • We yu klik di modal “bakdrop” i go lɔk di modal ɔtomɛtik wan.
  • Bootstrap de sɔpɔt wan modal winda nɔmɔ wan tɛm. Nested modals nɔ de sɔpɔt as wi biliv se dɛn na po yuz ɛkspiriɛns.
  • Modals use position: fixed, we kin sɔm tɛm dɛn kin bi smɔl patikyula bɔt in rɛnda. Ɛnitɛm we i pɔsibul, put yu modal HTML na wan tɔp-lɛvel pozishɔn fɔ avɔyd pɔtnɛshɛl intafɛreshɔn frɔm ɔda ɛlimɛnt dɛn. Yu go mɔs rɔn insay prɔblɛm dɛn we yu de nest wan .modalinsay ɔda fiks ɛlimɛnt.
  • Wans bak, bikɔs ɔf position: fixed, sɔm kɔvɛt dɛn de wit yuz modal dɛn na mobayl divays dɛn. Si wi brawza sɔpɔt dɔkyumɛnt dɛn fɔ di ditel dɛn.
  • Bikɔs ɔf aw HTML5 de difayn in sɛmantiks, di autofocusHTML atribyut nɔ gɛt ɛni ifɛkt na Bootstrap modals. Fɔ mek yu ebul fɔ du di sem tin, yuz sɔm kɔstɔm JavaSkript:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Di animashɔn ifɛkt fɔ dis kɔmpɔnɛnt de dipen pan di prefers-reduced-motionmidia kwɛstyɔn. Si di ridyus muvmɛnt sɛkshɔn na wi aksesibiliti dɔkyumentri .

Kɔntinyu fɔ rid fɔ di demo ɛn aw fɔ yuz gaydlayn dɛn.

Ɛgzampul dɛn

Dis dɔŋ ya na wan statik modal ɛgzampul (we min in positionɛn displaydɛn dɔn ɔvarayd). Insay de, di modal hεda, di modal bכdi (we nid fכ padding), εn di modal fut (opshכnal). Wi de aks yu fɔ put modal hεda wit dismis akshɔn ɛnitɛm we i pɔsibul, ɔ gi ɔda klia dismis akshɔn.

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

Layf dɛmo

Toggle wan wok modal demo bay we yu klik di bɔtin we de dɔŋ. I go slayv dɔŋ ɛn fade insay frɔm di ɔp pat na di pej.

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

Statik bakgrɔn

We dɛn sɛt bakdrop to statik, di modal nɔ go lɔk we yu klik ausayd am. Klik di bɔtin we de dɔŋ fɔ tray am.

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdrop">
  Launch static backdrop modal
</button>

<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="staticBackdropLabel">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">Understood</button>
      </div>
    </div>
  </div>
</div>

Skrol lɔng kɔntinyu

We di modal dɛn kin tu lɔng fɔ di pɔsin in viupɔt ɔ divays, dɛn kin skrol indipɛndɛnt frɔm di pej sɛf. Tray di demo we de dɔŋ fɔ si wetin wi min.

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

Yu kin mek bak wan modal we yu kin skrol we de alaw fɔ skrol di modal bɔdi bay we yu ad .modal-dialog-scrollableto .modal-dialog.

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalScrollable">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalScrollable" tabindex="-1" role="dialog" aria-labelledby="exampleModalScrollableTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalScrollableTitle">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>

Vertikal wan we de na di sɛnt

Add .modal-dialog-centeredto .modal-dialogfɔ vertikal sɛnt di modal.

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

Tultips ɛn popovers

Tultips ɛn popovers kin put insay modals as nid de. We dɛn lɔk modal dɛn, ɛni tultip ɛn popovers insay dɛnsɛf kin ɔtomɛtik dismis.

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

We yu de yuz di grid

Yutilayz di Bootstrap grid sistem insay wan modal bay we yu de nest .container-fluidinsay di .modal-body. Dɔn, yuz di nɔmal grid sistɛm klas dɛn lɛk aw yu go du ɛni ɔda say.

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

Difrɛn difrɛn modal kɔntinyu

Yu gɛt wan bɔnch bɔtin dɛn we ɔl de trig di sem modal wit smɔl difrɛn kɔntinyu? Yuz event.relatedTargetɛn HTML data-*atribyut dɛn (i kin bi bay jQuery ) fɔ chenj di tin dɛn we de insay di modal dipen pan us bɔtin dɛn klik.

Dis dɔŋ ya na layv dɛmo we de fala wit ɛgzampul HTML ɛn JavaSkript. Fɔ no mɔ, rid di modal events docs fɔ di ditel dɛn bɔt 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)
})

Chenj animashɔn

Di $modal-fade-transformvεriεbul de ditarmin di transfכm stet כf .modal-dialogbifo di modal fεd-in animashכn, di $modal-show-transformvεriεbul de ditarmin di transfכm כf .modal-dialogna di εnd כf di modal fεd-in animashכn.

If yu want fɔ ɛgzampul wan zoom-in animashɔn, yu kin sɛt $modal-fade-transform: scale(.8).

Rimov di animashɔn

Fɔ modal dɛn we jɔs de apia pas fɔ fade in fɔ si, pul di .fadeklas frɔm yu modal mak.

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

Dinamik ayt dɛn

If di ayt fɔ wan modal chenj we i opin, yu fɔ kɔl $('#myModal').modal('handleUpdate')fɔ ajɔst di modal in pozishɔn bak if wan skrolba apia.

Di we aw pɔsin kin ebul fɔ go de

Mek shɔ se yu ad role="dialog"ɛn aria-labelledby="...", we de rɛfrɛns di modal taytul, to .modal, ɛn role="document"to di .modal-dialogsɛf. Apat frɔm dat, yu kin gi diskripshɔn fɔ yu modal dayalɔg wit aria-describedbyon .modal.

Fɔ put di fim dɛn na Yutub fɔ put insay

Fɔ ɛmbas Yutub fim dɛn na modal dɛn nid fɔ gɛt ɔda JavaSkript we nɔ de na Bootstrap fɔ mek i stɔp fɔ ple ɔtomɛtik wan ɛn ɔda tin dɛn. Si dis ɛp Stack Overflow post fɔ mɔ infɔmeshɔn.

Saiz dɛn we yu kin pik

Modal dɛn gɛt tri opshɔnal saiz dɛn, we de bay modifya klas dɛn fɔ put pan wan .modal-dialog. Dɛn saiz dɛn ya kin kik insay sɔm brek pɔynt dɛn fɔ avɔyd ɔrizɔntal skrol bar dɛn na smɔl smɔl viupɔt dɛn.

Sayz Klas Modal maks-width fɔ di wan dɛn
Smɔl .modal-sm 300px
Balans Nɔn 500px
Big .modal-lg 800px
Ekstra big wan .modal-xl 1140px

Wi difɔlt modal we nɔ gɛt modifya klas de kɔnstityut di “midul” saiz modal.

<!-- Extra large modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-xl">Extra large modal</button>

<div class="modal fade bd-example-modal-xl" tabindex="-1" role="dialog" aria-labelledby="myExtraLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-xl" role="document">
    <div class="modal-content">
      ...
    </div>
  </div>
</div>

<!-- 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" role="document">
    <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" role="document">
    <div class="modal-content">
      ...
    </div>
  </div>
</div>

Aw fɔ yuz am

Di modal plɔgin de chenj yu kɔntinyu we yu ayd we yu aks fɔ am, bay we yu yuz data atribyut ɔ JavaSkript. I de ad bak .modal-opento di <body>fɔ ɔvalayz difɔlt skrolin bihayvya ɛn jenarayz wan .modal-backdropfɔ gi wan klik eria fɔ dismis di modal dɛn we dɛn sho we yu klik ausayd di modal.

Via data atribyut dɛn

Aktiv wan modal we nɔ rayt JavaSkript. Sɛt data-toggle="modal"pan wan kɔntrol ɛlimɛnt, lɛk wan bɔtin, wit wan data-target="#foo"ɔ href="#foo"fɔ tɔch wan patikyula modal fɔ tɔgl.

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

Yu kin yuz JavaSkript fɔ yuz am

Kɔl wan modal wit id myModalwit wan layn na JavaSkript:

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

Di tin dɛn we yu kin pik fɔ du

Yu kin pas opshɔn dɛn bay we yu yuz data atribyut ɔ JavaSkript. Fɔ data atribyut dɛn, ad di opshɔn nem to data-, lɛk insay data-backdrop="".

Nem Kayn Balans Tɔk bɔt
bakdrop fɔ de boolean ɔ di string'static' tru Inklud wan modal-bakdrop ɛlimɛnt. Ɔda we de fɔ du dat, spɛsifa staticfɔ wan bakdrop we nɔ de lɔk di modal we yu klik ɔ we yu prɛs ɛspɛk ki.
kibɔd fɔ di kibɔd boolean we dɛn kɔl tru Klos di modal we dɛn prɛs di ɛspɛk ki
tink bɔt boolean we dɛn kɔl tru Put di fos pan di modal we dɛn initialize.
sho boolean we dɛn kɔl tru Sho di modal we dɛn initialize.

Di we aw dɛn de du am

Asynchronous mεtכd dεm εn transishכn dεm

Ɔl di API mɛtɔd dɛn na asynchronous ɛn dɛn kin stat wan transishɔn . Dɛn kin go bak to di pɔsin we kɔl am jɔs lɛk aw dɛn bigin fɔ chenj bɔt bifo i dɔn . Apat frɔm dat, dɛn go ignore wan mɛtɔd kɔl pan wan transishɔn kɔmpɔnɛnt .

Si wi JavaSkript dɔkyumentri fɔ no mɔ .

.modal(options)

Aktiv yu kɔntinyu as modal. Aksept wan opshɔnal opshɔn dɛn object.

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

.modal('toggle')

Manually de toggle wan modal. Ritɔn to di pɔsin we kɔl bifo di modal dɔn rili sho ɔ ayd (dat na bifo di shown.bs.modalɔ hidden.bs.modalivin apin).

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

.modal('show')

Manually de opin wan modal. Ritɔn to di pɔsin we kɔl bifo di modal dɔn rili sho (dat na bifo di shown.bs.modalivin apin).

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

.modal('hide')

Manually de ayd wan modal. Ritɔn to di pɔsin we kɔl bifo di modal dɔn rili ayd (dat na bifo di hidden.bs.modalivin apin).

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

.modal('handleUpdate')

Riadjɔst di modal in pozishɔn wit yu an if di ayt fɔ wan modal chenj we i opin (dat na if wan skrolba apia).

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

.modal('dispose')

Destroy wan element in modal.

Di tin dɛn we kin apin

Bootstrap in modal klas de ɛksplɔz sɔm ivin dɛn fɔ huk insay modal funkshɔnaliti. Ɔl di modal ivin dɛn de faya na di modal sɛf (dat na na di <div class="modal">).

Di kayn tin we apin Tɔk bɔt
sho.bs.modal fɔ sho Dis ivent de faya wantɛm wantɛm we dɛn kɔl di showinstans mɛtɔd. If na klik, di ɛlimɛnt we dɛn klik de as di relatedTargetprɔpati fɔ di ivin.
we dɛn sho.bs.modal Dis ivent de faya we di modal dɔn mek di pɔsin we de yuz am si (go wet fɔ mek di CSS transishɔn dɛn dɔn). If na klik, di ɛlimɛnt we dɛn klik de as di relatedTargetprɔpati fɔ di ivin.
ayd.bs.modal fɔ di wan dɛn we de Dis ivent de faya wantɛm wantɛm we hidedɛn dɔn kɔl di instans mɛtɔd.
we dɛn ayd.bs.modal Dis ivent de faya we di modal dɔn dɔn fɔ ayd frɔm di yuza (go wet fɔ CSS transishɔn dɛn fɔ dɔn).
hidePrevented.bs.modal we dɛn dɔn ayd Dis ivent de faya we di modal sho, in bakdrop de staticɛn wan klik ausayd di modal ɔ wan ɛspɛk ki prɛs de du.
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})