Source

Modal a wɔde di dwuma

Fa Bootstrap JavaScript modal plugin no di dwuma fa nkɔmmɔbɔ ahorow ka wo sait no ho ma kanea nnaka, ɔdefo amanneɛbɔ, anaa nsɛm a wɔahyɛ da ayɛ koraa.

Sɛnea ɛyɛ adwuma

Ansa na wobɛhyɛ aseɛ de Bootstrap no modal component no, hwɛ sɛ wobɛkenkan deɛ ɛdidi soɔ yi ɛfiri sɛ yɛn menu options no asesa nnansa yi ara.

  • Wɔde HTML, CSS, ne JavaScript na ɛkyekyere modals. Wɔde wɔn asi biribiara a aka wɔ krataa no mu so na woyi scroll no fi mu <body>sɛnea ɛbɛyɛ a modal content scrolls mmom.
  • Sɛ wobɔ modal “backdrop” no so a, ɛbɛto modal no mu ankasa.
  • Bootstrap boa modal window baako pɛ wɔ bere koro mu. Nested modals no nnyɛ mmoa sɛnea yegye di sɛ ɛyɛ osuahu a enye a wɔde di dwuma no.
  • Modals de position: fixed, a ɛtɔ mmere bi a ebetumi ayɛ pɔtee kakra wɔ ne nkyerɛase ho. Bere biara a ɛbɛyɛ yiye no, fa wo modal HTML no to gyinabea a ɛkorɔn so na woakwati nneɛma afoforo a ebetumi atwitware mu. Ɛda adi sɛ wobɛtu mmirika akɔ nsɛm mu bere a woreyɛ nesting a .modalwɔ fixed element foforo mu no.
  • Saa bere yi nso, esiane position: fixed, kɔkɔbɔ ahorow bi wɔ hɔ a ɛfa modals a wɔde di dwuma wɔ mobile mfiri ahorow so ho. Hwɛ yɛn browser support docs no ma nsɛm no mu nsɛm.
  • Esiane sɛnea HTML5 kyerɛkyerɛ ne nkyerɛase mu nti, HTML autofocussu no nni nkɛntɛnso biara wɔ Bootstrap modals mu. Sɛ wopɛ sɛ wunya nkɛntɛnso koro no ara a, fa JavaScript bi a wɔahyɛ da ayɛ di dwuma:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Kɔ so kenkan ma demos ne akwankyerɛ a wɔde di dwuma.

Nhwɛso ahorow

Ase hɔ no yɛ static modal nhwɛso (a ɛkyerɛ sɛ ne positionna displaywɔabɔ so). Nea ɛka ho ne modal header, modal body (a ɛho hia ma padding), ne modal footer (a wopɛ). Yɛsrɛ sɛ fa modal headers a ɛwɔ dismiss actions ka ho bere biara a ɛbɛyɛ yiye, anaasɛ fa dismiss action foforo a ɛda adi pefee ma.

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

Live demo a ɛwɔ hɔ

Toggle modal demo a ɛreyɛ adwuma denam button a ɛwɔ ase ha no a wobɛma so. Ɛbɛtwetwe akɔ fam na ɛbɛyera afi kratafa no atifi.

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

Nsɛm a ɛware a wobɛtwetwe

Sɛ modals no tenten dodo ma ɔdefo no viewport anaa device a, ɛtwetwe a ɛnyɛ krataafa no ankasa. Sɔ demo a ɛwɔ ase ha no hwɛ na hwɛ nea yɛpɛ sɛ yɛka.

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

Ɛwɔ mfinimfini gyina hɔ

Fa ka .modal-dialog-centeredho na .modal-dialogfa vertically mfinimfini modal no.

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

Nnwinnade ho nsɛm ne popovers

Wobetumi de nnwinnade ho nsɛm ne popovers ahyɛ modals mu sɛnea ɛho hia. Sɛ wɔto modals mu a, adwinnade ho nsɛm biara ne popovers a ɛwɔ mu no nso wɔpow no ara kwa.

<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ɛ wode grid no di dwuma a

Fa Bootstrap grid nhyehyɛe no di dwuma wɔ modal bi mu denam nesting .container-fluid.modal-body. Afei, fa grid system classes a ɛyɛ daa no di dwuma sɛnea wobɛyɛ wɔ baabi foforo biara no.

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

Modal nsɛm a egu ahorow

So wowɔ button ahorow bi a ne nyinaa kanyan modal koro no ara a ɛsono emu nsɛm kakra? Fa event.relatedTargetne HTML data-*su ahorow (ebia ɛnam jQuery so ) di dwuma de sesa modal no mu nsɛm a egyina bɔtn a wɔakliki so.

Ase hɔ no yɛ live demo a nhwɛso HTML ne JavaScript di akyi. Sɛ wopɛ nsɛm pii a, kenkan modal events docs no ma nsɛm pii fa 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)
})

Yi animation no fi hɔ

Sɛ wopɛ modals a ɛda adi kɛkɛ sen sɛ ɛbɛyera akɔ mu ahwɛ a, yi .fadeadesua no fi wo modal markup no mu.

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

Nneɛma a ɛkorɔn a ɛyɛ nnam

Sɛ modal bi sorokɔ sesa bere a wɔabue a, ɛsɛ sɛ wofrɛ $('#myModal').modal('handleUpdate')no san siesie modal no gyinabea sɛ ebia scrollbar bi bɛpue a.

Nneɛma a wotumi nya

Hwɛ hu sɛ wode role="dialog"ne aria-labelledby="...", bɛka modal asɛmti no ho, akɔ .modal, ne role="document"ankasa .modal-dialogso. Bio nso, wobɛtumi de wo modal dialog no ho nkyerɛkyerɛmu ama aria-describedbywɔ on .modal.

YouTube video ahorow a wɔde hyɛ mu

YouTube video ahorow a wode bɛhyɛ modals mu no hwehwɛ JavaScript foforo a enni Bootstrap mu na ama woagyae playback no ankasa ne nea ɛkeka ho. Hwɛ Stack Overflow post a ɛboa yi ma nsɛm pii.

Nneɛma akɛse a wubetumi apaw

Modals wɔ akɛseɛ mmienu a wobɛpaw, a ɛwɔ hɔ denam modifier classes a wɔde bɛto a .modal-dialog. Saa akɛseɛ yi kick in wɔ breakpoints bi mu sɛdeɛ ɛbɛyɛ a wobɛkwati horizontal scrollbars wɔ viewports teateaa so.

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

Sɛnea wɔde di dwuma

Modal plugin no dannan wo nsɛm a ahintaw no bere a wɔhwehwɛ no, ɛnam data su anaa JavaScript so. Ɛsan nso de ka .modal-opento <body>override default scrolling behavior no ho na ɛma a .modal-backdropto ma click area ma dismissing shown modals bere a clicking outside the modal.

Via data su ahorow so

Fa modal bi yɛ adwuma a wonkyerɛw JavaScript. Fa data-toggle="modal"si controller element bi so, te sɛ button, ne a data-target="#foo"anaasɛ href="#foo"sɛ wode besi modal pɔtee bi so sɛ wobɛdannan.

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

Ɛdenam JavaScript so

Frɛ modal a ɛwɔ id myModala ɛwɔ JavaScript line biako:

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

Nneɛma a wubetumi apaw

Wobetumi afa data attributes anaa JavaScript so de nneɛma a wobɛpaw no akɔ. Sɛ wopɛ data su ahorow a, fa option din no ka ho data-, sɛnea ɛwɔ data-backdrop="".

Din Korɔ Mfiaseɛ Nkyerɛmu
akyi mfonini boolean anaasɛ ahama no'static' nokorɛ Nea ɛka ho ne modal-backdrop element bi. Sɛnea ɛbɛyɛ a, kyerɛ staticma akyigyina a ɛnto modal no mu wɔ klik so.
keyboard so nsɛm boolean ho asɛm nokorɛ Ɛto modal no mu bere a wɔabɔ escape key no
baabi a ani si boolean ho asɛm nokorɛ Fa adwene si modal no so bere a wɔahyɛ ase no.
da no adi boolean ho asɛm nokorɛ Kyerɛ modal no bere a wɔahyɛ ase no.

Akwan a wɔfa so yɛ

Asynchronous akwan ne nsakrae ahorow

API akwan nyinaa yɛ asynchronous na ɛhyɛ nsakrae ase . Wɔsan kɔ nea ɔfrɛɛ no ​​no nkyɛn bere a wɔafi nsakrae no ase ara pɛ nanso ansa na aba awiei . Bio nso, wobebu ani agu ɔkwan a wɔfa so frɛ ade bi a ɛresakra so no so .

Hwɛ yɛn JavaScript nkrataa no na woanya nsɛm pii.

.modal(options)

Ɛma wo nsɛm no yɛ adwuma sɛ modal. Gye akwan a wubetumi apaw bi tom object.

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

.modal('toggle')

Nsaano toggles a modal. San kɔ nea ɔfrɛ no no nkyɛn ansa na wɔakyerɛ modal no ankasa anaa wɔde asie (kyerɛ sɛ ansa na shown.bs.modalanaa hidden.bs.modaladeyɛ no aba).

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

.modal('show')

Nsaano bue modal bi. San kɔ nea ɔfrɛ no no nkyɛn ansa na wɔada modal no adi ankasa (kyerɛ sɛ ansa na shown.bs.modalasɛm no asi).

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

.modal('hide')

Nsaano de sie modal bi. San kɔ nea ɔfrɛ no no nkyɛn ansa na wɔde modal no asie ankasa (kyerɛ sɛ ansa na hidden.bs.modalasɛm no asi).

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

.modal('handleUpdate')

Fa nsa siesie modal no gyinabea sɛ modal bi sorokɔ sesa bere a wɔabue (kyerɛ sɛ sɛ ɛba sɛ scrollbar bi pue a).

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

.modal('dispose')

Ɔsɛe element bi modal.

Nsɛm a esisi

Bootstrap no modal adesuakuw no da nsɛm kakraa bi a esisii adi ma hooking kɔ modal dwumadi mu. Wɔtow modal nsɛm a esisi nyinaa wɔ modal no ankasa so (kyerɛ sɛ wɔ <div class="modal">).

Nsɛm a Ɛsisii no Su Nkyerɛmu
kyerɛ.bs.modal Saa adeyɛ yi tow ntɛm ara bere a showwɔfrɛ instance kwan no. Sɛ kliki na ɛde ba a, element a wɔakliki no wɔ hɔ sɛ relatedTargetadeyɛ no agyapade.
wɔakyerɛ.bs.modal Wɔtow saa adeyɛ yi bere a wɔayɛ modal no ahu nea ɔde di dwuma no (bɛtwɛn ma CSS nsakrae ahorow no awie). Sɛ kliki na ɛde ba a, element a wɔakliki no wɔ hɔ sɛ relatedTargetadeyɛ no agyapade.
hide.bs.modal no ho nsɛm Wɔtow saa adeyɛ yi ntɛm ara bere a hidewɔafrɛ instance kwan no.
ahintaw.bs.modal Wɔtow saa adeyɛ yi bere a wɔawie modal no a wɔde asie ama ɔdefo no (bɛtwɛn ma CSS nsakrae ahorow no awie).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})