Skip to main content Skip ad navigationem soUicitudo

Utere Bootstrap's JavaScript modalis plugin ad addere dialogs ad locum tuum pro lightboxes, notificationes usuario, vel omnino contenta consuetudine.

Quomodo facitur

Priusquam incepit cum Bootstrap modulatione modali, scito sequentia legere, sicut nostrae tabulae optiones nuper mutatae sunt.

  • Modales aedificantur cum HTML, CSS, et JavaScript. Super omnia alia in scriptione positi sunt et librum removent <body>ut potius cartis modalibus contentorum.
  • Clicking in modalis "backdrop" automatice claudet modalem.
  • Bootstrap tantum sustinet unam fenestram modalem ad tempus. Modales nidificati non sustinentur ut credamus eas experientias usoris pauperes esse.
  • Modalis usus position: fixed, qui aliquantulum de suo reddendo esse interdum potest. Quotiens fieri potest, pone modalem HTML in summo gradu, ne potentiale impedimentum ab aliis elementis fiat. Verisimile erit exitus incurrere cum .modalintra aliud fixum elementum nidificans.
  • Iterum, propter position: fixedaliquas cautiones cum modalibus in mobilibus machinis utendo. Vide navigatrum subsidium soUicitudo pro details.
  • Ob quam HTML5 sua semantica definit, attributio autofocusHTML effectum in modalibus Bootstrap non habet. Ad eundem effectum assequendum, utere aliqua consuetudine JavaScript:
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')

myModal.addEventListener('shown.bs.modal', function () {
  myInput.focus()
})
Effectus animationis huius componentis dependet ex prefers-reduced-motioninterrogatione instrumentorum. Videre motum sectionem accessibilitatis nostrae documentationis reductam .

Demos legere et usus guidelines serva.

Exempla

Infra exemplum modalis staticposition (est eius significatio et displayvexata). Inclusa sunt caput modalis, corpus modale (requiritur ad padding) et footer modalis (libitum). Rogamus ut includas capitis modales actiones dimissionis quoties fieri potest, vel aliam explicitam dimissionem praebere.

<div class="modal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></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-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Vivamus demo

Toggle operantes demo modalem pyga infra strepitando. A summo paginae labatur et deficiet.

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

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Static backdrop

Cum gutta static adponitur, modalis extra illud strepitando non claudet. Infra ipsum preme experior.

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

<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Understood</button>
      </div>
    </div>
  </div>
</div>

Scrolling diu contentus

Cum modales nimis longiores fiunt ad prospectum seu machinam usoris, ipsi paginae ipsius independentes sunt. Conare demo inferius videre quid velis.

.modal-dialog-scrollablePotes etiam facere librum modalem modalem qui librum corporis modalis admittit addendo .modal-dialog.

<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
  ...
</div>

Vertically sitas

Adde .modal-dialog-centeredad .modal-dialogverticaliter centrum modalem.

<!-- Vertically centered modal -->
<div class="modal-dialog modal-dialog-centered">
  ...
</div>

<!-- Vertically centered scrollable modal -->
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
  ...
</div>

Instrumentorum et popovers

Instrumenta et popoveri intra modales prout opus est poni possunt. Cum modalia clausa sint, instrumenta quaevis instrumenta et popoveri intus ipso facto dimittuntur.

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

Usus malesuada euismod

Utilise Bootstrap ratio gridis in modali .container-fluidintra nidificando .modal-body. Tum utere normali eget systematis classes sicut alibi vis.

<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 ms-auto">.col-md-4 .ms-auto</div>
    </div>
    <div class="row">
      <div class="col-md-3 ms-auto">.col-md-3 .ms-auto</div>
      <div class="col-md-2 ms-auto">.col-md-2 .ms-auto</div>
    </div>
    <div class="row">
      <div class="col-md-6 ms-auto">.col-md-6 .ms-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>

Varius contentus modalis

Habere fasciculum globuli omnes felis eundem modalem cum contentis leviter diversis? Usus event.relatedTargetet HTML data-bs-*attributa variare contenta modalis pendentibus quibus globulus strepebat.

Infra est demo viva exemplum HTML et JavaScript. Pro magis informationes, lege modales eventus docs pro details de relatedTarget.

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@getbootstrap">Open modal for @getbootstrap</button>

<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">New message</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <form>
          <div class="mb-3">
            <label for="recipient-name" class="col-form-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="mb-3">
            <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-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>
var exampleModal = document.getElementById('exampleModal')
exampleModal.addEventListener('show.bs.modal', function (event) {
  // Button that triggered the modal
  var button = event.relatedTarget
  // Extract info from data-bs-* attributes
  var recipient = button.getAttribute('data-bs-whatever')
  // If necessary, you could initiate an AJAX request here
  // and then do the updating in a callback.
  //
  // Update the modal's content.
  var modalTitle = exampleModal.querySelector('.modal-title')
  var modalBodyInput = exampleModal.querySelector('.modal-body input')

  modalTitle.textContent = 'New message to ' + recipient
  modalBodyInput.value = recipient
})

Toggle inter modales

Toggle inter plures modales cum subtilioribus quibusdam data-bs-targetet data-bs-toggleattributis. Exempli gratia, tesseram rete modalem ab intra iam apertum signum modalis toggle posses. Quaeso nota plura modalia simul aperta esse non possunt — haec methodus simpliciter toggles inter duos modales separatos.

Aperi primum modalis
<div class="modal fade" id="exampleModalToggle" aria-hidden="true" aria-labelledby="exampleModalToggleLabel" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalToggleLabel">Modal 1</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        Show a second modal and hide this one with the button below.
      </div>
      <div class="modal-footer">
        <button class="btn btn-primary" data-bs-target="#exampleModalToggle2" data-bs-toggle="modal" data-bs-dismiss="modal">Open second modal</button>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" id="exampleModalToggle2" aria-hidden="true" aria-labelledby="exampleModalToggleLabel2" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalToggleLabel2">Modal 2</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        Hide this modal and show the first with the button below.
      </div>
      <div class="modal-footer">
        <button class="btn btn-primary" data-bs-target="#exampleModalToggle" data-bs-toggle="modal" data-bs-dismiss="modal">Back to first</button>
      </div>
    </div>
  </div>
</div>
<a class="btn btn-primary" data-bs-toggle="modal" href="#exampleModalToggle" role="button">Open first modal</a>

Mutare animationem

Variabilis $modal-fade-transformstatum transformationem determinat .modal-dialogante modu defluxionis in animatione, $modal-show-transformvariabilis transformationem determinat in .modal-dialogfine modu flexae in animationis.

Si exempli gratia vis zoom-in animante, potes ponere $modal-fade-transform: scale(.8).

Aufer animationem

Modales enim, qui simpliciter apparent potius quam in conspectum decident, .fadegenus e charactere modali removent.

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

Dynamic iuga

Si altitudo modalis mutationum cum aperta est, vocare debes myModal.handleUpdate()ad componendum locum modalis in casu scrollbar quod apparet.

Accessibility

Adde scito aria-labelledby="...", referens titulum modalem , ad .modal. Accedit, quod descriptionem dialogi modalis tui cum aria-describedbyon .modal. Nota te non opus addere role="dialog"cum iam per JavaScript.

Embedding YouTube videos

Embedding YouTube videos in modalibus additional JavaScript non requirit in Bootstrap ut automatice playback prohibere et magis. Vide hoc utile Stack superfluum post pro magis notitia.

Moles libitum

Modales tres magnitudinum ad libitum habent, in promptu per modifier classes ponendas .modal-dialog. Hae magnitudines calcitrare in quibusdam confractionibus ad vitandarum schedulae horizontales in prospectibus angustioribus.

Magnitudo Classis Modal max-latitudo
Parvus .modal-sm 300px
Default Nullus 500px
Magna .modal-lg 800px
Extra large .modal-xl 1140px

Nostrum defaltum modalis sine determinatione classis constituit magnitudinem modalem "medii".

<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>

Modal Fullscreen

Alius dominandi modus est option pop sursum modalis quae conspectum usoris tegit, praesto per classes determinatas quae in a ponuntur .modal-dialog.

Classis Availability
.modal-fullscreen Semper
.modal-fullscreen-sm-down Infra576px
.modal-fullscreen-md-down Infra768px
.modal-fullscreen-lg-down Infra992px
.modal-fullscreen-xl-down Infra1200px
.modal-fullscreen-xxl-down Infra1400px
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
  ...
</div>

Sass

Variabilium

$modal-inner-padding:               $spacer;

$modal-footer-margin-between:       .5rem;

$modal-dialog-margin:               .5rem;
$modal-dialog-margin-y-sm-up:       1.75rem;

$modal-title-line-height:           $line-height-base;

$modal-content-color:               null;
$modal-content-bg:                  $white;
$modal-content-border-color:        rgba($black, .2);
$modal-content-border-width:        $border-width;
$modal-content-border-radius:       $border-radius-lg;
$modal-content-inner-border-radius: subtract($modal-content-border-radius, $modal-content-border-width);
$modal-content-box-shadow-xs:       $box-shadow-sm;
$modal-content-box-shadow-sm-up:    $box-shadow;

$modal-backdrop-bg:                 $black;
$modal-backdrop-opacity:            .5;
$modal-header-border-color:         $border-color;
$modal-footer-border-color:         $modal-header-border-color;
$modal-header-border-width:         $modal-content-border-width;
$modal-footer-border-width:         $modal-header-border-width;
$modal-header-padding-y:            $modal-inner-padding;
$modal-header-padding-x:            $modal-inner-padding;
$modal-header-padding:              $modal-header-padding-y $modal-header-padding-x; // Keep this for backwards compatibility

$modal-sm:                          300px;
$modal-md:                          500px;
$modal-lg:                          800px;
$modal-xl:                          1140px;

$modal-fade-transform:              translate(0, -50px);
$modal-show-transform:              none;
$modal-transition:                  transform .3s ease-out;
$modal-scale-transform:             scale(1.02);

Loop

Responsivum modales fullscreen generantur per $breakpointsmap et loop in scss/_modal.scss.

@each $breakpoint in map-keys($grid-breakpoints) {
  $infix: breakpoint-infix($breakpoint, $grid-breakpoints);
  $postfix: if($infix != "", $infix + "-down", "");

  @include media-breakpoint-down($breakpoint) {
    .modal-fullscreen#{$postfix} {
      width: 100vw;
      max-width: none;
      height: 100%;
      margin: 0;

      .modal-content {
        height: 100%;
        border: 0;
        @include border-radius(0);
      }

      .modal-header {
        @include border-radius(0);
      }

      .modal-body {
        overflow-y: auto;
      }

      .modal-footer {
        @include border-radius(0);
      }
    }
  }
}

Consuetudinem

Plugin modalis toggles tuas absconditas in demanda, per data attributa vel JavaScript. Etiam mores defaltam scrolling vincit et generat .modal-backdroput cliccam aream praebeat ad dimittendum modales ostensos cum extra modales clicking.

Via data attributa

Modalis eu sine scripto JavaScript. Pone data-bs-toggle="modal"in elementum moderatoris, ut puga pyga, una cum a data-bs-target="#foo"vel href="#foo"ut scopum modalem specificum toggle.

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

Via JavaScript

Facere modalis cum una JavaScript:

var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)

Optiones

Optiones transigi possunt per data attributa vel JavaScript. Data enim attributa, adiungunt optionis nomen data-bs-, ut in data-bs-backdrop="".

Nomen Type Default Descriptio
backdrop Boolean vel filum'static' true Includes elementum modal-backdrop. Vel, specificare staticpro exo- ram quod non claudit modalem in strepita.
keyboard Boolean true Modalis claudit cum clavis effugium premitur
focus Boolean true Focum ponit in modali cum initialized.

Methodi

Modi asynchronous et transitus

Omnes API modi asynchroni sunt et transitus committitur . Redeunt ad RECENS mox ut transitus incipiat sed antequam finiatur . Methodus praeterea in transitus transeuntis ignorabitur .

See our JavaScript documentation for more information .

Transeuntes optiones

Exagitat tuam contentus modalem. Acceptat optiones libitum object.

var myModal = new bootstrap.Modal(document.getElementById('myModal'), {
  keyboard: false
})

toggle

Manually toggles modalis. Redit ad RECENS antequam modalis actu ostensum est vel absconditum (id est ante shown.bs.modalvel hidden.bs.modaleventum).

myModal.toggle()

ostende

Manually modal opens. RECENS REcurrit antequam modalis actu ostensum est (id antequam shown.bs.modalevenit).

myModal.show()

Item, elementum dom transire potes ut argumentum quod recipi potest in eventibus modalibus (sicut relatedTargetproprietas).

var modalToggle = document.getElementById('toggleMyModal') // relatedTarget
myModal.show(modalToggle)

celare

Manually celat modalis. Redit ad RECENS antequam modalis actu absconditus est (id antequam hidden.bs.modalevenit).

myModal.hide()

handleUpdate

Manually statum modalis legitimum, si altitudo modalis mutationum dum aperta est (ie in casu scrollbar apparet).

myModal.handleUpdate()

dispono

Modalis elementum destruit. (Recondita removet data in dom elementum)

myModal.dispose()

getInstance

Statica methodus quae permittit ut instantia modalis consociata cum elemento DOM

var myModalEl = document.getElementById('myModal')
var modal = bootstrap.Modal.getInstance(myModalEl) // Returns a Bootstrap modal instance

getOrCreateInstance

Statica methodus quae te permittit ut instantia modalis cum elemento DOM coniungitur, vel novum crea in casu non initialisatum.

var myModalEl = document.querySelector('#myModal')
var modal = bootstrap.Modal.getOrCreateInstance(myModalEl) // Returns a Bootstrap modal instance

Events

Classis modalis Bootstrap paucas eventus exponit pro hamo in functionem modalem. Omnes eventus modales accenduntur ad ipsum modalem (id est ad <div class="modal">).

Genus res Descriptio
show.bs.modal Huius rei accendit statim cum showinstantia methodus appellatur. Si causatur per strepita, elementum clicked praesto est ut relatedTargetproprietas eventus.
shown.bs.modal Eventus hic accensus est cum modalis utenti visibilia facta est (exspectabit CSS transitus ad perficiendum). Si causatur per strepita, elementum clicked praesto est ut relatedTargetproprietas eventus.
hide.bs.modal Hic eventus statim accensus est, cum hidemethodus instantia vocata est.
hidden.bs.modal Eventus hic accensus est cum modalis occultationem ab usore confecit (exspectabit CSS transitus ad perficiendum).
hidePrevented.bs.modal Hic eventus accenditur cum modalis ostenditur, eius backdros est staticet strepita extra modalem vel effugium preli clavis cum optione claviaturae vel data-bs-keyboardad posito perficitur false.
var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', function (event) {
  // do something...
})