Ruka hadi kwa yaliyomo kuu Ruka hadi kwenye urambazaji wa hati

Tumia programu-jalizi ya modali ya JavaScript ya Bootstrap ili kuongeza vidadisi kwenye tovuti yako kwa visanduku vyepesi, arifa za watumiaji, au maudhui maalum kabisa.

Inavyofanya kazi

Kabla ya kuanza na kijenzi cha modali cha Bootstrap, hakikisha kuwa umesoma yafuatayo kwani chaguzi zetu za menyu zimebadilika hivi majuzi.

  • Moduli hujengwa kwa HTML, CSS na JavaScript. Zimewekwa juu ya kila kitu kingine kwenye hati na huondoa kusongesha kutoka <body>ili maudhui ya modal yasogeze badala yake.
  • Kubofya kwenye "mandhari" ya modal kutafunga modali kiotomatiki.
  • Bootstrap inasaidia tu dirisha moja la modal kwa wakati mmoja. Miundo iliyopachikwa haitumiki kwa kuwa tunaamini kuwa matumizi mabaya ya watumiaji.
  • Modals use position: fixed, ambayo wakati mwingine inaweza kuwa maalum kidogo juu ya utoaji wake. Inapowezekana, weka HTML yako ya modali katika nafasi ya juu ili kuepuka kuingiliwa kwa vipengele vingine. Huenda utakumbana na matatizo wakati wa kuweka kiota .modalndani ya kipengele kingine kisichobadilika.
  • Kwa mara nyingine tena, kutokana na position: fixed, kuna baadhi ya tahadhari za kutumia moduli kwenye vifaa vya rununu. Tazama hati zetu za usaidizi wa kivinjari kwa maelezo.
  • Kutokana na jinsi HTML5 inavyofafanua semantiki zake, sifa ya autofocusHTML haina athari katika miundo ya Bootstrap. Ili kufikia athari sawa, tumia JavaScript maalum:
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')

myModal.addEventListener('shown.bs.modal', function () {
  myInput.focus()
})
Athari ya uhuishaji ya kipengele hiki inategemea prefers-reduced-motionhoja ya midia. Tazama sehemu ya mwendo iliyopunguzwa ya hati zetu za ufikivu .

Endelea kusoma kwa maonyesho na miongozo ya matumizi.

Mifano

Hapo chini kuna mfano tuli wa modal (ikimaanisha kuwa yake positionna displayimebatilishwa). Imejumuishwa ni kichwa cha modali, mwili wa modali (inahitajika kwa padding), na kijachini cha modali (si lazima). Tunaomba ujumuishe vichwa vya modal na vitendo vya kuondoa kila inapowezekana, au utoe hatua nyingine ya wazi ya kukataa.

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

Onyesho la moja kwa moja

Geuza onyesho la modali ya kufanya kazi kwa kubofya kitufe kilicho hapa chini. Itateleza chini na kufifia kutoka juu ya ukurasa.

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

Mandhari tuli

Mandhari ya nyuma yanapowekwa kuwa tuli, modali haitafungwa wakati wa kubofya nje yake. Bofya kitufe kilicho hapa chini ili kuijaribu.

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

Inasogeza maudhui marefu

Miundo inapokuwa ndefu sana kwa kituo cha kutazama au kifaa cha mtumiaji, husogeza bila kutegemea ukurasa wenyewe. Jaribu onyesho hapa chini ili kuona tunamaanisha nini.

Unaweza pia kuunda modi inayoweza kusongeshwa ambayo inaruhusu kusogeza mwili wa modali kwa kuongeza .modal-dialog-scrollablekwa .modal-dialog.

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

Imewekwa katikati

Ongeza .modal-dialog-centeredkwa .modal-dialogkatikati wima katikati ya modali.

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

Vidokezo vya zana na popover

Vidokezo vya zana na popover vinaweza kuwekwa ndani ya moduli kama inahitajika. Miundo inapofungwa, vidokezo vyovyote vya zana na popover ndani pia huondolewa kiotomatiki.

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

Kutumia gridi ya taifa

Tumia mfumo wa gridi ya Bootstrap ndani ya modali kwa kuweka kiota .container-fluidndani ya .modal-body. Kisha, tumia madarasa ya mfumo wa gridi ya kawaida kama ungefanya popote pengine.

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

Kutofautisha maudhui ya modal

Una rundo la vitufe ambavyo vyote huanzisha modali sawa na yaliyomo tofauti kidogo? Tumia event.relatedTargetna sifa za HTMLdata-bs-* ili kubadilisha maudhui ya modali kulingana na kitufe ambacho kilibofya.

Ifuatayo ni onyesho la moja kwa moja linalofuatwa na mfano wa HTML na JavaScript. Kwa habari zaidi, soma hati za matukio ya modal kwa maelezo kuhusu 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
})

Geuza kati ya moduli

Geuza kati ya moduli nyingi kwa uwekaji wa busara wa data-bs-targetna data-bs-togglesifa. Kwa mfano, unaweza kugeuza modali ya kuweka upya nenosiri kutoka ndani ya ishara ambayo tayari imefunguliwa katika modali. Tafadhali kumbuka miundo mingi haiwezi kufunguliwa kwa wakati mmoja -njia hii hugeuza tu kati ya moduli mbili tofauti.

Fungua moduli ya kwanza
<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>

Badilisha uhuishaji

Tofauti $modal-fade-transformhuamua hali ya mageuzi ya .modal-dialogkabla ya uhuishaji wa modali kufifia, $modal-show-transformkigezo huamua ubadilishaji wa .modal-dialogmwisho wa uhuishaji wa modal fade-in.

Ikiwa unataka kwa mfano uhuishaji wa kukuza, unaweza kuweka $modal-fade-transform: scale(.8).

Ondoa uhuishaji

Kwa moduli ambazo zinaonekana tu badala ya kufifia ili kutazamwa, ondoa .fadedarasa kutoka kwa lebo yako ya modal.

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

Urefu wenye nguvu

Ikiwa urefu wa modali hubadilika wakati umefunguliwa, unapaswa kupiga simu myModal.handleUpdate()ili kurekebisha nafasi ya modali ikiwa upau wa kusogeza utatokea.

Ufikivu

Hakikisha umeongeza aria-labelledby="...", ukirejelea kichwa cha modali, kwa .modal. Zaidi ya hayo, unaweza kutoa maelezo ya mazungumzo yako ya modal na aria-describedbyon .modal. Kumbuka kuwa huhitaji kuongeza role="dialog"kwa kuwa tayari tunaiongeza kupitia JavaScript.

Inapachika video za YouTube

Kupachika video za YouTube katika miundo kunahitaji JavaScript ya ziada si kwenye Bootstrap ili kukomesha uchezaji kiotomatiki na zaidi. Tazama chapisho hili muhimu la Stack Overflow kwa maelezo zaidi.

Ukubwa wa hiari

Moduli zina saizi tatu za hiari, zinapatikana kupitia madarasa ya kurekebisha kuwekwa kwenye .modal-dialog. Saizi hizi huingia katika sehemu fulani za kukatika ili kuepuka pau za kusogeza zilizo mlalo kwenye lango finyu za kutazama.

Ukubwa Darasa Modal max-upana
Ndogo .modal-sm 300px
Chaguomsingi Hakuna 500px
Kubwa .modal-lg 800px
Ziada kubwa .modal-xl 1140px

Modali yetu chaguo-msingi bila darasa la kirekebishaji inajumuisha modali ya ukubwa wa "wastani".

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

Modali ya Skrini nzima

Ubatilishaji mwingine ni chaguo la kuunda moduli ambayo inashughulikia tovuti ya kutazama ya mtumiaji, inayopatikana kupitia madarasa ya kurekebisha ambayo yamewekwa kwenye .modal-dialog.

Darasa Upatikanaji
.modal-fullscreen Kila mara
.modal-fullscreen-sm-down Chini576px
.modal-fullscreen-md-down Chini768px
.modal-fullscreen-lg-down Chini992px
.modal-fullscreen-xl-down Chini1200px
.modal-fullscreen-xxl-down Chini1400px
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
  ...
</div>

Sass

Vigezo

$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);

Kitanzi

Miundo ya kujibu ya skrini nzima hutolewa kupitia $breakpointsramani na kitanzi katika 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);
      }
    }
  }
}

Matumizi

Programu-jalizi ya modal hugeuza maudhui yako yaliyofichwa inapohitajika, kupitia sifa za data au JavaScript. Pia hubatilisha tabia chaguo-msingi ya kusogeza na kuzalisha a .modal-backdropkutoa eneo la kubofya kwa ajili ya kuondoa miundo iliyoonyeshwa unapobofya nje ya modali.

Kupitia sifa za data

Washa modali bila kuandika JavaScript. Weka data-bs-toggle="modal"kwenye kipengele cha kidhibiti, kama kitufe, pamoja na data-bs-target="#foo"au href="#foo"kulenga modi mahususi ya kugeuza.

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

Kupitia JavaScript

Unda modali na safu moja ya JavaScript:

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

Chaguo

Chaguzi zinaweza kupitishwa kupitia sifa za data au JavaScript. Kwa sifa za data, ongeza jina la chaguo kwa data-bs-, kama katika data-bs-backdrop="".

Jina Aina Chaguomsingi Maelezo
backdrop boolean au kamba'static' true Inajumuisha kipengele cha mandharinyuma ya modal. Vinginevyo, bainisha statickwa mandhari ambayo haifungi muundo wa kubofya.
keyboard boolean true Hufunga modali wakati kitufe cha Escape kinapobozwa
focus boolean true Huweka mkazo kwenye modali inapoanzishwa.

Mbinu

Njia za Asynchronous na mabadiliko

Njia zote za API ni za asynchronous na zinaanzisha mpito . Wanarudi kwa mpigaji mara tu mpito unapoanza lakini kabla haujaisha . Kwa kuongeza, simu ya mbinu kwenye sehemu ya mpito itapuuzwa .

Tazama hati zetu za JavaScript kwa maelezo zaidi .

Kupitisha chaguzi

Huwasha maudhui yako kama modali. Inakubali chaguo za hiari object.

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

kugeuza

Hugeuza modali mwenyewe. Hurejesha kwa mpigaji simu kabla modali haijaonyeshwa au kufichwa (yaani kabla ya tukio shown.bs.modalau hidden.bs.modaltukio).

myModal.toggle()

onyesha

Hufungua moduli mwenyewe. Hurejesha kwa mpigaji simu kabla modali haijaonyeshwa (yaani kabla ya shown.bs.modaltukio kutokea).

myModal.show()

Pia, unaweza kupitisha kipengele cha DOM kama hoja ambayo inaweza kupokelewa katika matukio ya modal (kama relatedTargetmali).

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

kujificha

Huficha modali mwenyewe. Hurejesha kwa mpigaji simu kabla modali haijafichwa (yaani kabla ya hidden.bs.modaltukio kutokea).

myModal.hide()

kushughulikiaSasisho

Rekebisha mwenyewe nafasi ya modali ikiwa urefu wa modali hubadilika wakati iko wazi (yaani ikiwa upau wa kusogeza utatokea).

myModal.handleUpdate()

tupa

Huharibu modi ya kipengele. (Huondoa data iliyohifadhiwa kwenye kipengele cha DOM)

myModal.dispose()

getInstance

Njia tuli ambayo hukuruhusu kupata mfano wa modal unaohusishwa na kipengee cha DOM

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

getOrCreateInstance

Njia tuli ambayo hukuruhusu kupata mfano wa modal unaohusishwa na kipengee cha DOM, au kuunda mpya ikiwa haikuanzishwa.

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

Matukio

Darasa la modali la Bootstrap linafichua matukio machache ya kuunganisha kwenye utendakazi wa modal. Matukio yote ya modal yanarushwa kwa modali yenyewe (yaani kwenye <div class="modal">).

Aina ya tukio Maelezo
show.bs.modal Tukio hili huwaka mara moja wakati shownjia ya mfano inaitwa. Ikisababishwa na kubofya, kipengele kilichobofya kinapatikana kama sifa relatedTargetya tukio.
shown.bs.modal Tukio hili linafutwa wakati modali imefanywa kuonekana kwa mtumiaji (itasubiri mabadiliko ya CSS kukamilika). Ikisababishwa na kubofya, kipengele kilichobofya kinapatikana kama sifa relatedTargetya tukio.
hide.bs.modal Tukio hili linafutwa mara moja wakati hidenjia ya mfano imeitwa.
hidden.bs.modal Tukio hili litafutwa wakati modali imekamilika kufichwa kutoka kwa mtumiaji (itasubiri mabadiliko ya CSS kukamilika).
hidePrevented.bs.modal Tukio hili hutupwa wakati modali inaonyeshwa, mandhari yake ni staticna kubofya nje ya modali au ubonyezo wa kitufe cha kutoroka hufanywa kwa chaguo la kibodi au data-bs-keyboardkuweka false.
var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', function (event) {
  // do something...
})