Source

Modal

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:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('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

Chini ni 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" 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>

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

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

Inasogeza maudhui marefu

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

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

Unaweza pia kuunda moduli inayoweza kusongeshwa ambayo inaruhusu kusogeza mwili wa modali kwa kuongeza .modal-dialog-scrollablekwa .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>

Imewekwa katikati

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

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

Vidokezo vya zana na popover

Vidokezo vya zana na popover vinaweza kuwekwa ndani ya moduli kama inahitajika. Wakati moduli zimefungwa, 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-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 mahali pengine popote.

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

Kutofautisha maudhui ya modal

Una rundo la vitufe ambavyo vyote huanzisha modali sawa na yaliyomo tofauti kidogo? Tumia event.relatedTargetna sifa za HTMLdata-* (labda kupitia jQuery ) ili kubadilisha yaliyomo kwenye modali kulingana na kitufe kilichobofya.

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

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 zinazoonekana tu badala ya kufifia ili kutazamwa, ondoa .fadedarasa kutoka kwa alama yako ya modal.

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

Urefu wenye nguvu

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

Ufikivu

Hakikisha kuongeza role="dialog"na aria-labelledby="...", ukirejelea kichwa cha modali, kwa .modal, na role="document"yenyewe .modal-dialog. Zaidi ya hayo, unaweza kutoa maelezo ya kidadisi chako cha modal na aria-describedbyon .modal.

Inapachika video za YouTube

Kupachika video za YouTube katika miundo kunahitaji JavaScript ya ziada si kwenye Bootstrap ili kusimamisha 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".

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

Matumizi

Programu-jalizi ya modal hugeuza maudhui yako yaliyofichwa inapohitajika, kupitia sifa za data au JavaScript. Pia huongeza .modal-openkwa <body>kubatilisha tabia ya kusogeza chaguo-msingi na hutoa 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-toggle="modal"kwenye kipengele cha kidhibiti, kama kitufe, pamoja na data-target="#foo"au href="#foo"kulenga modi mahususi ya kugeuza.

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

Kupitia JavaScript

Piga modali na kitambulisho myModalna safu moja ya JavaScript:

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

Chaguo

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

Jina Aina Chaguomsingi Maelezo
mandhari boolean au kamba'static' kweli Inajumuisha kipengele cha mandharinyuma ya modal. Vinginevyo, bainisha statickwa mandhari ambayo haifungi muundo wa kubofya au kwa kubonyeza kitufe cha kutoroka.
kibodi boolean kweli Hufunga modali wakati kitufe cha Escape kinapobozwa
kuzingatia boolean kweli Huweka mkazo kwenye modali inapoanzishwa.
onyesha boolean kweli Inaonyesha moduli 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 .

.modal(options)

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

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

.modal('toggle')

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

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

.modal('show')

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

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

.modal('hide')

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

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

.modal('handleUpdate')

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

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

.modal('dispose')

Huharibu modi ya kipengele.

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
onyesha.bs.modal Tukio hili huwaka mara moja wakati shownjia ya mfano inaitwa. Ikisababishwa na kubofya, kipengele kilichobofya kinapatikana kama sifa relatedTargetya tukio.
mfano.bs.ulioonyeshwa Tukio hili linafutwa wakati modali imefanywa kuonekana kwa mtumiaji (itasubiri mabadiliko ya CSS kukamilika). Ikisababishwa na kubofya, kipengele kilichobofya kinapatikana kama sifa relatedTargetya tukio.
ficha.bs.modal Tukio hili linafutwa mara moja wakati hidenjia ya mfano imeitwa.
fiche.bs.modal Tukio hili litafutwa wakati modali imekamilika kufichwa kutoka kwa mtumiaji (itasubiri mabadiliko ya CSS kukamilika).
FichaImezuiwa.b.modali Tukio hili hutupwa wakati modali inaonyeshwa, mandhari yake ni staticna kubofya nje ya modali au ubonyezo wa kitufe cha kutoroka unafanywa.
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})