Source

Modal

Siv Bootstrap's JavaScript modal plugin ntxiv dialogs rau koj qhov chaw rau lightboxes, neeg siv cov ntawv ceeb toom, lossis cov ntsiab lus kev cai tag nrho.

Nws ua haujlwm li cas

Ua ntej pib nrog Bootstrap's modal tivthaiv, nco ntsoov nyeem cov hauv qab no raws li peb cov kev xaiv zaub mov tsis ntev los no tau hloov.

  • Modals yog tsim nrog HTML, CSS, thiab JavaScript. Lawv nyob nraum positioned tshaj txhua yam nyob rau hauv cov ntaub ntawv thiab tshem tawm scroll los ntawm lub <body>thiaj li hais tias modal cov ntsiab lus scrolls hloov.
  • Nyem rau ntawm lub modal "backdrop" yuav cia li kaw lub modal.
  • Bootstrap tsuas txhawb ib lub qhov rais modal ib zaug. Nested modals tsis txaus siab vim peb ntseeg tias lawv yog cov neeg siv tsis zoo.
  • Modals siv position: fixed, uas qee zaum tuaj yeem yog qhov tshwj xeeb ntawm nws qhov kev ua haujlwm. Thaum twg ua tau, tso koj cov HTML modal rau hauv qhov chaw sab saum toj kom tsis txhob muaj kev cuam tshuam los ntawm lwm cov ntsiab lus. Koj yuav muaj peev xwm khiav mus rau hauv cov teeb meem thaum nesting ib .modalnyob rau hauv lwm yam khoom ruaj.
  • Ib zaug ntxiv, vim position: fixed, muaj qee qhov caveats nrog kev siv modals ntawm mobile pab kiag li lawm. Saib peb cov ntaub ntawv txhawb nqa browser kom paub meej.
  • Vim li cas HTML5 txhais nws cov semantics, HTML tus cwj pwm tsisautofocus muaj txiaj ntsig hauv Bootstrap modals. Txhawm rau ua tiav cov txiaj ntsig zoo ib yam, siv qee qhov kev cai JavaScript:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Cov nyhuv animation ntawm cov khoom siv no yog nyob ntawm cov prefers-reduced-motionlus nug xov xwm. Saib qhov txo qis ntawm peb cov ntaub ntawv nkag mus tau .

Khaws nyeem rau demos thiab cov lus qhia siv.

Piv txwv

Hauv qab no yog ib qho piv txwv zoo li qub (lub ntsiab lus nws positionthiab displaytau dhau los). Xws li yog lub taub hau modal, modal lub cev (yuav tsum tau rau padding), thiab modal footer (yeem). Peb thov kom koj suav nrog cov ntsiab lus ntawm kev tso tawm thaum twg los tau, lossis muab lwm qhov kev tso tawm kom pom tseeb.

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

Nyob demo

Toggle ib tug ua hauj lwm modal demo los ntawm nias lub pob hauv qab no. Nws yuav swb thiab ploj mus los ntawm sab saum toj ntawm nplooj ntawv.

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

Static backdrop

Thaum backdrop yog teem rau zoo li qub, lub modal yuav tsis kaw thaum nias sab nraum nws. Nyem lub pob hauv qab no sim nws.

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

Scrolling cov ntsiab lus ntev

Thaum modals ntev dhau los rau tus neeg siv qhov chaw saib lossis ntaus ntawv, lawv scroll ywj siab ntawm nplooj ntawv nws tus kheej. Sim cov demo hauv qab no kom pom tias peb txhais li cas.

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

Koj tuaj yeem tsim tau ib qho scrollable modal uas tso cai rau scroll lub modal lub cev los ntawm kev ntxiv .modal-dialog-scrollablerau .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>

Vertical centered

Ntxiv .modal-dialog-centeredmus .modal-dialograu vertically center lub 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>

Cov lus qhia thiab popovers

Cov lus qhia thiab popovers tuaj yeem muab tso rau hauv modals raws li xav tau. Thaum cov modals raug kaw, txhua yam lus qhia thiab cov popovers nyob rau hauv kuj raug tso tseg.

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

Siv daim phiaj

Siv lub Bootstrap kab sib chaws nyob rau hauv ib tug modal los ntawm nesting .container-fluidnyob rau hauv lub .modal-body. Tom qab ntawd, siv cov chav kawm kab ke ib txwm ua raws li koj xav tau nyob txhua qhov chaw.

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

Varying modal cov ntsiab lus

Muaj ib pawg ntawm cov nyees khawm uas txhua tus ua rau tib lub modal nrog cov ntsiab lus sib txawv me ntsis? Siv event.relatedTargetthiab HTML data-*cwj pwm (tejzaum nws ntawm jQuery ) kom sib txawv ntawm cov ntsiab lus ntawm lub modal nyob ntawm seb lub pob twg raug nias.

Hauv qab no yog qhov ua yeeb yaj kiab nyob ua raws li piv txwv HTML thiab JavaScript. Yog xav paub ntxiv, nyeem cov txheej xwm modal docs kom paub meej txog 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)
})

Hloov cov animation

Qhov $modal-fade-transformsib txawv txiav txim siab hloov lub xeev ntawm .modal-dialogua ntej modal ploj-hauv animation, qhov $modal-show-transformsib txawv txiav txim siab hloov pauv ntawm .modal-dialogqhov kawg ntawm modal fade-in animation.

Yog tias koj xav tau piv txwv zoom-hauv animation, koj tuaj yeem teeb tsa $modal-fade-transform: scale(.8).

Tshem cov animation

Rau cov qauv uas tsuas yog tshwm sim ntau dua li ploj mus saib, tshem tawm cov .fadechav kawm los ntawm koj cov ntawv cim modal.

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

Dynamic qhov siab

Yog hais tias qhov siab ntawm ib qho kev hloov pauv thaum nws qhib, koj yuav tsum hu $('#myModal').modal('handleUpdate')mus kho lub modal txoj hauj lwm nyob rau hauv cov ntaub ntawv ib tug scrollbar tshwm.

Kev siv tau

Nco ntsoov ntxiv role="dialog"thiab aria-labelledby="...", referencing lub npe modal, rau .modal, thiab role="document"rau .modal-dialognws tus kheej. Tsis tas li ntawd, koj tuaj yeem muab cov lus piav qhia ntawm koj lub modal dialog nrog aria-describedbyrau ntawm .modal.

Embedding YouTube yeeb yaj duab

Embedding YouTube yeeb yaj duab nyob rau hauv modals yuav tsum tau ntxiv JavaScript tsis nyob rau hauv Bootstrap kom cia li nres playback thiab ntau dua. Saib qhov pab tau Stack Overflow ncej kom paub ntau ntxiv.

Xaiv qhov ntau thiab tsawg

Modals muaj peb qhov ntau thiab tsawg, muaj nyob ntawm cov chav kawm hloov kho kom muab tso rau ntawm ib qho .modal-dialog. Cov qhov loj me no ncaws tawm ntawm qee qhov chaw nres tsheb kom tsis txhob muaj kab rov tav scrollbars ntawm qhov chaw nqaim.

Loj Chav kawm Modal max-dav
Me me .modal-sm 300px
Default Tsis muaj 500px
Loj .modal-lg 800px
Ntxiv loj .modal-xl 1140px

Peb default modal yam tsis muaj kev hloov kho chav kawm yog qhov "nruab nrab" 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>

Kev siv

Lub modal plugin toggles koj zais cov ntsiab lus ntawm kev thov, ntawm cov ntaub ntawv cwj pwm lossis JavaScript. Nws kuj tseem ntxiv .modal-openrau <body>kom dhau qhov kev coj tus cwj pwm scrolling thiab tsim .modal-backdropkom muaj ib qho chaw nyem rau kev tshem tawm cov qauv qhia thaum nias sab nraum lub modal.

Los ntawm cov ntaub ntawv attributes

Qhib lub modal yam tsis tau sau JavaScript. Teem data-toggle="modal"rau ntawm tus tswj lub caij, zoo li lub pob, nrog rau ib data-target="#foo"lossis href="#foo"rau lub hom phiaj rau ib qho kev hloov pauv.

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

Ntawm JavaScript

Hu rau modal nrog id myModalnrog ib kab ntawm JavaScript:

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

Kev xaiv

Cov kev xaiv tuaj yeem dhau los ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript. Rau cov ntaub ntawv tus cwj pwm, ntxiv rau qhov kev xaiv npe rau data-, xws li hauv data-backdrop="".

Lub npe Hom Default Kev piav qhia
keeb kwm yav dhau boolean los yog txoj hlua'static' muaj tseeb Xws li ib qho modal-backdrop element. Xwb, qhia meej staticrau ib qho keeb kwm yav dhau uas tsis kaw lub modal ntawm nias lossis ntawm kev khiav tawm xovxwm.
keyboard boolean muaj tseeb Kaw lub modal thaum khiav qhov tseem ceeb yog nias
tsom boolean muaj tseeb Muab qhov tsom mus rau lub modal thaum pib.
ua yeeb yam boolean muaj tseeb Qhia cov modal thaum pib.

Cov txheej txheem

Asynchronous txoj kev thiab kev hloov

Txhua txoj kev API yog asynchronous thiab pib hloov pauv . Lawv rov qab mus rau tus neeg hu xov tooj sai li sai tau thaum qhov kev hloov pauv pib tab sis ua ntej nws xaus . Tsis tas li ntawd, ib txoj kev hu rau ib qho kev hloov pauv yuav raug tsis quav ntsej .

Saib peb cov ntaub ntawv JavaScript kom paub ntau ntxiv .

.modal(options)

Activates koj cov ntsiab lus raws li ib tug modal. Txais ib qho kev xaiv xaiv object.

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

.modal('toggle')

Manually toggles ib modal. Rov qab mus rau tus neeg hu ua ntej lub modal tau tshwm sim los yog zais (piv txwv li ua ntej qhov shown.bs.modalkev hidden.bs.modaltshwm sim tshwm sim).

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

.modal('show')

Manually qhib lub modal. Rov qab mus rau tus neeg hu ua ntej lub modal tau tshwm sim tiag tiag (piv txwv li ua ntej qhov shown.bs.modalkev tshwm sim tshwm sim).

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

.modal('hide')

Manually hides ib modal. Rov qab mus rau tus neeg hu ua ntej lub modal tau muab zais tiag tiag (piv txwv li ua ntej qhov hidden.bs.modalxwm txheej tshwm sim).

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

.modal('handleUpdate')

Manually readjust lub modal txoj hauj lwm yog hais tias qhov siab ntawm ib tug modal hloov thaum nws qhib (piv txwv li nyob rau hauv cov ntaub ntawv ib tug scrollbar tshwm).

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

.modal('dispose')

Ua kom puas lub ntsiab ntawm modal.

Cov xwm txheej

Bootstrap's modal class exposes ob peb txheej xwm rau hooking rau hauv modal functionality. Tag nrho cov txheej xwm modal raug rho tawm haujlwm ntawm lub modal nws tus kheej (piv txwv li ntawm <div class="modal">).

Hom xwm txheej Kev piav qhia
show.bs.modal Qhov xwm txheej no tua hluav taws tam sim ntawd thaum showhu ua piv txwv. Yog hais tias tshwm sim los ntawm ib tug nias, lub clicked caij yog muaj raws li cov relatedTargetcuab yeej ntawm qhov kev tshwm sim.
ua.bs.modal Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub modal tau pom rau tus neeg siv (yuav tos rau CSS kev hloov pauv kom tiav). Yog hais tias tshwm sim los ntawm ib tug nias, lub clicked caij yog muaj raws li cov relatedTargetcuab yeej ntawm qhov kev tshwm sim.
nkaum.bs.modal Qhov xwm txheej no raug rho tawm haujlwm tam sim ntawd thaum hidehu ua piv txwv txoj kev.
zais.bs.modal Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub modal tiav lawm muab zais los ntawm tus neeg siv (yuav tos rau CSS kev hloov pauv kom tiav).
hidePrevented.bs.modal Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub modal raug pom, nws cov backdrop yog staticthiab ib tug nias sab nraum lub modal los yog ib tug khiav ceev nias yog ua.
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})