Source

Modal

Anggo plugin modal JavaScript Bootstrap pikeun nambihan dialog kana situs anjeun pikeun kotak lampu, bewara pangguna, atanapi kontén anu lengkep.

Kumaha gawéna

Sateuacan ngamimitian sareng komponén modal Bootstrap, pastikeun maca ieu di handap sabab pilihan ménu kami parantos robih.

  • Modals diwangun ku HTML, CSS, jeung JavaScript. Aranjeunna nuju diposisikan leuwih sagalana sejenna dina dokumen jeung cabut gulung ti <body>ambéh eusi modal scrolls gantina.
  • Ngaklik dina "backdrop" modal bakal otomatis nutup modal.
  • Bootstrap ngan ngarojong hiji jandela modal dina hiji waktu. Modél bersarang henteu didukung sabab kami yakin yén éta mangrupikeun pangalaman pangguna anu goréng.
  • Modals make position: fixed, nu kadang bisa jadi bit husus ngeunaan rendering na. Sabisana, nempatkeun HTML modal anjeun dina posisi tingkat luhur pikeun nyegah poténsi gangguan ti elemen séjén. Anjeun kamungkinan bakal ngalaman masalah nalika nyarang .modaldina unsur tetep anu sanés.
  • Sakali deui, alatan position: fixed, aya sababaraha caveats kalawan ngagunakeun modals dina alat nu bagerak. Tingali dokumén pangrojong browser kami pikeun detil.
  • Kusabab kumaha HTML5 ngahartikeun semantik na, atribut autofocusHTML teu boga pangaruh dina modals Bootstrap. Pikeun ngahontal éfék anu sami, paké sababaraha JavaScript khusus:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Tetep maca pikeun demo sareng pedoman pamakean.

Contona

Di handap ieu conto modal statik (hartina na positiongeus displayoverridden). Kaasup header modal, awak modal (diperlukeun pikeun padding), jeung footer modal (opsional). Kami nyuhunkeun anjeun ngalebetkeun header modal sareng tindakan ngabubarkeun sabisana, atanapi nyayogikeun tindakan ngilangkeun eksplisit anu sanés.

<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-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Live demo

Toggle demo modal kerja ku ngaklik tombol di handap. Bakal ngageser ka handap sareng luntur ti luhur halaman.

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

Ngagulung eusi panjang

Lamun modals jadi panjang teuing pikeun viewport pamaké atawa alat, maranéhna ngagulung bebas tina kaca sorangan. Coba demo di handap pikeun ningali naon anu kami maksud.

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

Vertikal dipuseurkeun

Tambahkeun .modal-dialog-centeredka .modal-dialogka puseur vertikal 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="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>

Tooltips na popovers

Tooltips na popovers bisa disimpen dina modals sakumaha diperlukeun. Nalika modals ditutup, sagala tooltips na popovers dina ogé otomatis mecat.

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

Ngagunakeun grid nu

Anggo sistem grid Bootstrap dina modal ku nyarang .container-fluiddina .modal-body. Teras, nganggo kelas sistem grid normal sapertos anu anjeun lakukeun di mana waé.

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

Variasi eusi modal

Boga kebat tombol anu sadayana micu modal anu sami sareng eusi anu rada béda? Paké event.relatedTargetjeung atribut HTMLdata-* (jigana via jQuery ) pikeun rupa-rupa eusi modal gumantung kana tombol nu diklik.

Di handap ieu demo live dituturkeun ku conto HTML jeung JavaScript. Kanggo inpo nu leuwih lengkep, baca docs acara modal pikeun detil ngeunaan 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)
})

Leupaskeun animasi

Pikeun modal anu ngan saukur némbongan tinimbang luntur pikeun ditingali, cabut .fadekelas tina markup modal anjeun.

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

Jangkungna dinamis

Upami jangkungna modal robih nalika dibuka, anjeun kedah nelepon $('#myModal').modal('handleUpdate')pikeun nyaluyukeun deui posisi modal upami aya scrollbar.

Aksesibilitas

Pastikeun pikeun nambahkeun role="dialog"jeung aria-labelledby="...", ngarujuk kana judul modal, ka .modal, jeung role="document"ka .modal-dialogsorangan. Salaku tambahan, anjeun tiasa masihan pedaran ngeunaan dialog modal anjeun sareng aria-describedbyon .modal.

Embedding video YouTube

Embedding video YouTube dina modals merlukeun JavaScript tambahan teu di Bootstrap pikeun otomatis ngeureunkeun playback sareng nu sanesna. Tingali tulisan Stack Overflow ieu pikeun inpormasi anu langkung lengkep.

Ukuran pilihan

Modals gaduh dua ukuran pilihan, sadia via kelas modifier pikeun ditempatkeun dina .modal-dialog. Ukuran ieu kick dina titik breakpoints tangtu pikeun nyingkahan scrollbars horizontal dina viewports sempit.

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

Pamakéan

Modél plugin toggles eusi disumputkeun Anjeun on demand, via atribut data atawa JavaScript. Ogé nambihan .modal-openka <body>override kabiasaan ngagugulung standar sarta dibangkitkeun a .modal-backdropnyadiakeun wewengkon klik pikeun dismissing modals ditémbongkeun nalika ngaklik luar modal.

Via atribut data

Aktipkeun modal tanpa nulis JavaScript. Disetél data-toggle="modal"dina unsur controller, kawas tombol, babarengan jeung data-target="#foo"atawa href="#foo"pikeun nargétkeun modal husus pikeun toggle.

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

Ngaliwatan JavaScript

Nelepon modal sareng id myModalsareng hiji baris JavaScript:

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

Pilihan

Pilihan bisa diliwatan via atribut data atawa JavaScript. Pikeun atribut data, tambahkeun nami pilihan ka data-, sapertos dina data-backdrop="".

Ngaran Tipe Default Katerangan
latar boolean atawa string'static' leres Ngawengku unsur modal-backdrop. Alternatipna, tangtukeun staticbackdrop anu henteu nutup modal dina klik.
kibor boolean leres Nutup modal nalika konci kabur dipencet
fokus boolean leres Nempatkeun fokus kana modal nalika initialized.
nempokeun boolean leres Némbongkeun modal nalika dimimitian.

Métode

Métode Asynchronous sareng transisi

Sadaya metode API henteu sinkron sareng ngamimitian transisi . Aranjeunna balik deui ka panelepon pas transisi dimimitian tapi saméméh éta ends . Sajaba ti éta, panggero métode dina komponén transisi bakal dipaliré .

Tingali dokuméntasi JavaScript kami kanggo inpormasi lengkep.

.modal(options)

Aktipkeun eusi anjeun salaku modal. Narima hiji pilihan pilihan object.

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

.modal('toggle')

Sacara manual toggles modal. Mulih ka panelepon saméméh modal sabenerna geus ditémbongkeun atawa disumputkeun (ie saméméh shown.bs.modalatawa hidden.bs.modalkajadian lumangsung).

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

.modal('show')

Sacara manual muka modal. Mulih ka panelepon saméméh modal sabenerna geus ditémbongkeun (ie saméméh shown.bs.modalacara lumangsung).

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

.modal('hide')

Sacara manual nyumputkeun modal. Mulih ka panelepon saméméh modal geus sabenerna disumputkeun (ie saméméh hidden.bs.modalacara lumangsung).

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

.modal('handleUpdate')

Nyaluyukeun posisi modal sacara manual upami jangkungna modalna robih nalika dibuka (upami upami aya scrollbar).

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

.modal('dispose')

Ngancurkeun modal hiji unsur.

Kajadian

Kelas modal Bootstrap ngungkabkeun sababaraha acara pikeun nyambungkeun kana fungsionalitas modal. Sadaya acara modal dipecat dina modal sorangan (ie di <div class="modal">).

Jenis Acara Katerangan
show.bs.modal Kajadian ieu langsung hurung nalika showmetode conto disebut. Upami disababkeun ku klik, unsur anu diklik sayogi salaku relatedTargethak milik acara.
ditémbongkeun.bs.modal Kajadian ieu dipecat nalika modalna parantos katingali ku pangguna (bari ngantosan transisi CSS réngsé). Upami disababkeun ku klik, unsur anu diklik sayogi salaku relatedTargethak milik acara.
hide.bs.modal Kajadian ieu langsung dipecat nalika hidemetode conto parantos disebut.
hidden.bs.modal Acara ieu dipecat nalika modal parantos disumputkeun tina pangguna (bari ngadagoan transisi CSS réngsé).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})