Source

Awoṣe

Lo ohun itanna modal JavaScript Bootstrap lati ṣafikun awọn ibaraẹnisọrọ si aaye rẹ fun awọn apoti ina, awọn iwifunni olumulo, tabi akoonu aṣa patapata.

Bawo ni o ṣe n ṣiṣẹ

Ṣaaju ki o to bẹrẹ pẹlu paati modal Bootstrap, rii daju lati ka atẹle bi awọn aṣayan akojọ aṣayan wa ti yipada laipẹ.

  • Awọn awoṣe jẹ itumọ pẹlu HTML, CSS, ati JavaScript. Wọn wa ni ipo lori ohun gbogbo miiran ninu iwe naa ki o yọ yi lọ kuro ni <body>ki akoonu modal yi lọ dipo.
  • Tite lori modal “backdrop” yoo pa modal naa laifọwọyi.
  • Bootstrap nikan ṣe atilẹyin window modal kan ni akoko kan. Awọn awoṣe itẹle ko ni atilẹyin bi a ṣe gbagbọ pe wọn jẹ awọn iriri olumulo ti ko dara.
  • Modals lilo position: fixed, eyi ti o le ma jẹ kan bit pato nipa awọn oniwe- Rendering. Nigbakugba ti o ṣee ṣe, gbe HTML modal rẹ si ipo ipele oke lati yago fun kikọlu agbara lati awọn eroja miiran. O ṣee ṣe ki o lọ sinu awọn ọran nigbati o ba n gbe ile kan .modallaarin eroja ti o wa titi miiran.
  • Lekan si, nitori position: fixed, nibẹ ni o wa diẹ ninu awọn caveats pẹlu lilo modals lori awọn ẹrọ alagbeka. Wo awọn iwe atilẹyin ẹrọ aṣawakiri wa fun awọn alaye.
  • Nitori bi HTML5 ṣe n ṣalaye awọn itumọ-ọrọ rẹ, ẹda autofocusHTML ko ni ipa ni awọn awoṣe Bootstrap. Lati ṣaṣeyọri ipa kanna, lo diẹ ninu JavaScript aṣa:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Jeki kika fun demos ati awọn ilana lilo.

Awọn apẹẹrẹ

Ni isalẹ jẹ apẹẹrẹ modal aimi (itumọ rẹ positionati displaype o ti bori). To wa pẹlu akọsori modal, ara modal (ti a beere fun padding), ati ẹlẹsẹ modal (aṣayan). A beere pe ki o pẹlu awọn akọle modal pẹlu awọn iṣe yiyọ kuro nigbakugba ti o ṣee ṣe, tabi pese igbese ifasilẹ gbangba miiran.

<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

Yi demo modal ṣiṣẹ nipa tite bọtini ni isalẹ. Yoo rọra si isalẹ ati ipare ni lati oke ti oju-iwe naa.

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

Yi lọ akoonu gun

Nigbati awọn awoṣe ba gun ju fun wiwo olumulo tabi ẹrọ, wọn yi lọ ni ominira ti oju-iwe naa funrararẹ. Gbiyanju demo ni isalẹ lati wo kini a tumọ si.

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

Ni inaro aarin

Fi .modal-dialog-centeredsi .modal-dialoginaro aarin 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 ati popovers

Awọn imọran irinṣẹ ati awọn agbejade le wa ni gbe laarin awọn modal bi o ṣe nilo. Nigbati modals ti wa ni pipade, eyikeyi awọn imọran irinṣẹ ati awọn agbejade laarin tun jẹ yọkuro laifọwọyi.

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

Lilo awọn akoj

Lo eto akoj Bootstrap laarin modal nipasẹ itẹ-ẹiyẹ .container-fluidlaarin .modal-body. Lẹhinna, lo awọn kilasi eto akoj deede bi o ṣe le nibikibi miiran.

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

Akoonu modal ti o yatọ

Ni opo ti awọn bọtini ti gbogbo awọn okunfa kanna modal pẹlu die-die o yatọ si awọn akoonu? Lo event.relatedTargetati awọn abuda HTMLdata-* (o ṣee ṣe nipasẹ jQuery ) lati ṣe iyatọ awọn akoonu ti modal da lori bọtini wo ni o tẹ.

Ni isalẹ ni demo ifiwe kan atẹle nipa apẹẹrẹ HTML ati JavaScript. Fun alaye diẹ sii, ka awọn docs awọn iṣẹlẹ modal fun awọn alaye lori 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)
})

Yọ iwara kuro

Fun awọn awoṣe ti o han nirọrun kuku ju ipare sinu lati wo, yọ .fadekilasi kuro lati isamisi modal rẹ.

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

Awọn giga ti o ni agbara

Ti giga modal ba yipada lakoko ti o wa ni sisi, o yẹ ki o pe $('#myModal').modal('handleUpdate')lati tun ipo ipo modal ṣe ni ọran ti iwe-kilọ ba han.

Wiwọle

Rii daju lati ṣafikun role="dialog"ati aria-labelledby="...", tọka akọle modal, si .modal, ati role="document"si .modal-dialogfunrararẹ. Ni afikun, o le fun ni apejuwe ti ibaraẹnisọrọ modal rẹ pẹlu aria-describedbylori .modal.

Ifisinu YouTube awọn fidio

Ifisinu awọn fidio YouTube ni awọn awoṣe nilo afikun JavaScript kii ṣe ni Bootstrap lati da ṣiṣiṣẹsẹhin duro laifọwọyi ati diẹ sii. Wo ifiweranṣẹ Stack Overflow ti o ṣe iranlọwọ fun alaye diẹ sii.

Awọn iwọn iyan

Modals ni awọn iwọn iyan meji, wa nipasẹ awọn kilasi modifier lati gbe sori faili .modal-dialog. Awọn iwọn wọnyi tapa ni awọn aaye isinmi kan lati yago fun awọn yiyi ti petele lori awọn iwoye ti o dín.

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

Lilo

Ohun itanna modal yi akoonu ti o farapamọ pada lori ibeere, nipasẹ awọn abuda data tabi JavaScript. O tun ṣe afikun .modal-opensi <body>lati bori ihuwasi yiyi lọ aiyipada ati ṣe ipilẹṣẹ kan .modal-backdroplati pese agbegbe tẹ kan fun yiyọ awọn awoṣe ti o han nigbati titẹ si ita modal.

Nipasẹ awọn abuda data

Mu modal ṣiṣẹ laisi kikọ JavaScript. Ṣeto data-toggle="modal"lori eroja oludari, bii bọtini kan, pẹlu kan data-target="#foo"tabi href="#foo"lati fojusi modal kan pato lati yi.

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

Nipasẹ JavaScript

Pe modal pẹlu id myModalpẹlu laini JavaScript kan:

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

Awọn aṣayan

Awọn aṣayan le ṣee kọja nipasẹ awọn abuda data tabi JavaScript. Fun awọn abuda data, fi orukọ aṣayan si data-, bi ninu data-backdrop="".

Oruko Iru Aiyipada Apejuwe
sile boolian tabi okun'static' ooto Pẹlu a modal-backdrop ano. Ni omiiran, pato staticfun ẹhin ẹhin eyiti ko pa modal naa ni titẹ.
keyboard boolianu ooto Tilekun modal nigbati o ba tẹ bọtini ona abayo
idojukọ boolianu ooto Fi idojukọ lori modal nigba ti ipilẹṣẹ.
ifihan boolianu ooto Ṣe afihan modal nigbati o bẹrẹ.

Awọn ọna

Awọn ọna Asynchronous ati awọn iyipada

Gbogbo awọn ọna API jẹ asynchronous ati bẹrẹ iyipada kan . Wọn pada si ọdọ olupe ni kete ti iyipada ti bẹrẹ ṣugbọn ṣaaju ki o to pari . Ni afikun, ipe ọna kan lori paati iyipada yoo jẹ kọbikita .

Wo iwe JavaScript wa fun alaye diẹ sii.

.modal(options)

Mu akoonu rẹ ṣiṣẹ bi modal. Gba awọn aṣayan iyan object.

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

.modal('toggle')

Pẹlu ọwọ yipada modal kan. Pada si olupe ṣaaju ki modal ti han gangan tabi farapamọ (ie ṣaaju ki iṣẹlẹ shown.bs.modaltabi hidden.bs.modaliṣẹlẹ waye).

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

.modal('show')

Pẹlu ọwọ ṣii modal kan. Pada si olupe ṣaaju ki modal ti han gangan (ie ṣaaju ki shown.bs.modaliṣẹlẹ naa to waye).

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

.modal('hide')

Pẹlu ọwọ hides a modal. Pada si olupe ṣaaju ki modal naa ti farapamọ gangan (ie ṣaaju ki hidden.bs.modaliṣẹlẹ naa to waye).

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

.modal('handleUpdate')

Ṣe atunṣe ipo modal pẹlu ọwọ ti giga modal ba yipada lakoko ti o wa ni sisi (ie ti o ba jẹ pe yiyi yoo han).

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

.modal('dispose')

Pa ohun ano ká modal.

Awọn iṣẹlẹ

Kilasi modal Bootstrap ṣafihan awọn iṣẹlẹ diẹ fun sisọ sinu iṣẹ ṣiṣe modal. Gbogbo awọn iṣẹlẹ modal jẹ ina ni modal funrararẹ (ie ni <div class="modal">).

Iṣẹlẹ Iru Apejuwe
fihan.bs.modal Yi iṣẹlẹ ina lẹsẹkẹsẹ nigbati awọn showapẹẹrẹ ọna ti a npe ni. Ti o ba ṣẹlẹ nipasẹ titẹ kan, eroja ti o tẹ wa bi relatedTargetohun-ini ti iṣẹlẹ naa.
han.bs.modal Iṣẹlẹ yii jẹ ina nigbati modal ti jẹ ki o han si olumulo (yoo duro fun awọn iyipada CSS lati pari). Ti o ba ṣẹlẹ nipasẹ titẹ kan, eroja ti o tẹ wa bi relatedTargetohun-ini ti iṣẹlẹ naa.
tọju.bs.modal Iṣẹlẹ yii jẹ ina lẹsẹkẹsẹ nigbati ọna hideapẹẹrẹ ti pe.
farasin.bs.modal Iṣẹlẹ yii jẹ ina nigbati modal naa ba ti pari fifipamọ lati ọdọ olumulo (yoo duro fun awọn iyipada CSS lati pari).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})