Source

Modal

Sebelisa mokhoa oa ho kenya JavaScript oa Bootstrap ho kenya lipuisano sebakeng sa hau sa marang-rang bakeng sa mabokose a khanyang, litsebiso tsa basebelisi, kapa litaba tse ikhethileng ka botlalo.

Kamoo e sebetsang kateng

Pele o qala ka karolo ea modal ea Bootstrap, etsa bonnete ba hore u bala tse latelang kaha likhetho tsa rona tsa menyetla li sa tsoa fetoha.

  • Li-modal li hahiloe ka HTML, CSS, le JavaScript. Li behiloe holim'a ntho e 'ngoe le e' ngoe e ka har'a tokomane 'me li tlose moqolo ho tsoa ho <body>e le hore litaba tsa modal li tsamaee.
  • Ho tobetsa "backdrop" ea modal ho tla koala modal ka bo eona.
  • Bootstrap e tšehetsa fensetere e le 'ngoe feela ka nako. Mekhoa e hlophisitsoeng ha e tšehetsoe kaha re lumela hore ha e na boiphihlelo ba basebelisi.
  • Li-modals li sebelisa position: fixed, tseo ka linako tse ling li ka bang tse khethehileng mabapi le phetolelo ea eona. Nako le nako ha ho khonahala, beha HTML ea hau ea modal sebakeng sa boemo bo holimo ho qoba tšitiso e ka bang teng ho tsoa linthong tse ling. U ka 'na ua tobana le mathata ha u theha sehlaha ka .modalhar'a ntho e' ngoe e tsitsitseng.
  • Hape, ka lebaka la position: fixed, ho na le li-caveats mabapi le ho sebelisa mekhoa ea lisebelisoa tsa mehala. Sheba litokomane tsa rona tsa tšehetso ea sebatli ho fumana lintlha.
  • Ka lebaka la hore na HTML5 e hlalosa semantics ea eona joang, tšobotsi ea autofocusHTML ha e na phello ho mekhoa ea Bootstrap. Ho fihlela phello e tšoanang, sebelisa JavaScript e tloaelehileng:
$('#myModal').on('shown.bs.modal', function () {
  $('#myInput').trigger('focus')
})

Liphetho tsa animation tsa karolo ena li ipapisitse le prefers-reduced-motionpotso ea media. Sheba karolo e fokolitsoeng ea motsamao oa litokomane tsa phihlello ea rona .

Tsoela pele ho bala bakeng sa li-demos le litataiso tsa tšebeliso.

Mehlala

Ka tlase ke mohlala o tsitsitseng oa modal (o bolelang hore o na positionle 'me displayo tlositsoe). Ho kenyelelitsoe hlooho ea modal, 'mele oa modal (ea hlokehang bakeng sa padding) le botlaaseng ba modal (boikhethelo). Re kopa hore o kenyeletse lihlooho tsa modal le liketso tsa ho qhala ha ho khonahala, kapa o fane ka ketso e 'ngoe e hlakileng ea ho qhala.

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

Pontšo e phelang

Fetolela mokhoa oa ho sebetsa oa modal ka ho tobetsa konopo e ka tlase. E tla thellela fatše ebe e nyamela ho tloha holimo leqepheng.

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

Ho tsamaisa litaba tse telele

Ha li-modal li nka nako e telele bakeng sa sebaka sa pono sa mosebelisi kapa sesebelisoa, li tsamaea ntle le leqephe ka bolona. Leka demo e ka tlase ho bona seo re se bolelang.

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

E tsepamisitsoe holimo

Eketsa .modal-dialog-centeredho .modal-dialogmodal ho thea bohareng.

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

Lisebelisoa le li-popovers

Lisebelisoa le li-popovers li ka kenngoa ka har'a mekhoa e hlokahalang. Ha li-modal li koetsoe, lisebelisoa life kapa life tsa popover tse ka hare le tsona li qheleloa ka thoko.

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

Ho sebelisa grid

Sebelisa sistimi ea grid ea Bootstrap ka har'a modal ka ho haha .container-fluid​​​​ka har'a .modal-body. Ebe u sebelisa litlelase tse tloaelehileng tsa grid joalo ka ha u ne u ka etsa kae kapa kae.

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

Likahare tse fapaneng tsa modal

Na u na le sehlopha sa likonopo tse hlahisang modal e tšoanang ka litaba tse fapaneng hanyane? Sebelisa event.relatedTargetle litšobotsi tsa HTMLdata-* (mohlomong ka jQuery ) ho fetola litaba tsa modal ho latela hore na konopo e tobetse.

Ka tlase ke demo e phelang e lateloang ke mohlala oa HTML le JavaScript. Ho fumana lintlha tse ling, bala litokomane tsa liketsahalo tsa modal bakeng sa lintlha tse mabapi le 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)
})

Fetola animation

Phetoho $modal-fade-transforme etsa qeto ea boemo ba .modal-dialogpele ho animation ea modal fade-in, phetoho $modal-show-transforme etsa qeto ea phetoho .modal-dialogqetellong ea modal fade-in animation.

Haeba u batla mohlala animation ea katametso, u ka seta $modal-fade-transform: scale(.8).

Tlosa animation

Bakeng sa li-modal tse hlahang feela ho e-na le ho nyamela, tlosa .fadesehlopha ho li-modal markup tsa hau.

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

Bophahamo bo matla

Haeba bophahamo ba modal bo fetoha ha e ntse e butsoe, o lokela ho letsetsa $('#myModal').modal('handleUpdate')ho lokisa boemo ba modal haeba ho hlaha scrollbar.

Ho fihlella

Etsa bonnete ba ho eketsa role="dialog"le aria-labelledby="...", ho supa sehlooho sa modal, ho .modal, le role="document"ho eona .modal-dialogka boeona. Ho feta moo, o ka fana ka tlhaloso ea puisano ea hau ea modal le aria-describedbyon .modal.

Ho kenyelletsa livideo tsa YouTube

Ho kenya livideo tsa YouTube ho li-modal ho hloka JavaScript e eketsehileng eseng ho Bootstrap ho emisa ho bapala le tse ling. Sheba poso ena e thusang ea Stack Overflow bakeng sa lintlha tse ling.

Boikhethelo ba boholo

Li-modal li na le mefuta e meraro ea boikhethelo, e fumanehang ka litlelase tsa ho fetola tse tla behoa ho .modal-dialog. Lisaese tsena li kenella ka linako tse ling ho qoba mekoallo e tšekaletseng maema-kepeng a manyane.

Boholo Sehlopha Modal max-width
Nyenyane .modal-sm 300px
Ea kamehla Ha ho letho 500px
E kholoanyane .modal-lg 800px
E kholo ho feta .modal-xl 1140px

Modal ea rona ea kamehla ntle le sehlopha sa mofetoleli e etsa "bohareng" ba boholo.

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

Tšebeliso

Modal plugin e fetola litaba tsa hau tse patiloeng ha li batloa, ka likarolo tsa data kapa JavaScript. E boetse e ekelletsa .modal-openho <body>tloleng boits'oaro ba kamehla ba ho phutha 'me e hlahisa .modal-backdropho fana ka sebaka sa ho tobetsa bakeng sa ho leleka mekhoa e bontšitsoeng ha o tobetsa ka ntle ho modal.

Ka litšobotsi tsa data

Kenya modal ntle le ho ngola JavaScript. Beha data-toggle="modal"karolo ea taolo, joalo ka konopo, hammoho le data-target="#foo"kapa href="#foo"ho shebisa mokhoa o itseng oa ho fetola.

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

Ka JavaScript

Letsetsa modal ka id myModalka mola o le mong oa JavaScript:

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

Dikgetho

Likhetho li ka fetisoa ka lintlha tsa data kapa JavaScript. Bakeng sa litšobotsi tsa data, kenya lebitso la khetho ho data-, joalo ka data-backdrop="".

Lebitso Mofuta Ea kamehla Tlhaloso
bokamorao boolean kapa khoele'static' 'nete E kenyelletsa karolo ea modal-backdrop. Ntle le moo, hlakisa staticboemo ba morao-rao bo sa koaheleng mokhoa oa ho tobetsa.
keyboard boolean 'nete E koala modal ha konopo ea escape e tobetsoa
tsepamisa maikutlo boolean 'nete E tsepamisa maikutlo ho modal ha e qala.
bontsha boolean 'nete E bonts'a modal ha e qala.

Mekhoa

Mekhoa ea Asynchronous le liphetoho

Mekhoa eohle ea API ha e- synchronous 'me e qala phetoho . Ba khutlela ho motho ea letselitseng hang ha phetoho e qala empa pele e fela . Ho feta moo, mohala oa mokhoa ho karolo ea phetoho o tla hlokomolohuoa .

Sheba litokomane tsa rona tsa JavaScript bakeng sa lintlha tse ling .

.modal(options)

E kenya tšebetsong litaba tsa hau joalo ka mokhoa. E amohela khetho ea boikhethelo object.

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

.modal('toggle')

E fetola modal ka letsoho. E kgutlela ho motho ya letsetsang pele modal e bontshwa kapa e patilwe (ke hore pele ketsahalo shown.bs.modalkapa hidden.bs.modalketsahalo e etsahala).

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

.modal('show')

E bula modal ka letsoho. E khutlela ho motho ea letselitseng pele mokha o bonts'oa (ke hore pele shown.bs.modalketsahalo e etsahala).

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

.modal('hide')

E pata modal ka letsoho. E khutlela ho motho ea letselitseng pele mohale o patiloe (ke hore pele hidden.bs.modalketsahalo e etsahala).

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

.modal('handleUpdate')

Ka bouena fetola boemo ba modal haeba bophahamo ba modal bo fetoha ha e ntse e butse (ke hore haeba ho hlaha scrollbar).

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

.modal('dispose')

E senya modal oa element.

Liketsahalo

Sehlopha sa modal sa Bootstrap se pepesa liketsahalo tse 'maloa tsa ho hokahana le ts'ebetso ea modal. Liketsahalo tsohle tsa modal li lelekoa ho modal ka boeona (ke hore ho <div class="modal">).

Mofuta oa Ketsahalo Tlhaloso
show.bs.modal Ketsahalo ena e tuka hang hang ha showmokhoa oa mohlala o bitsoa. Haeba e bakoa ke ho tobetsa, ntho e tobelitsoeng e fumaneha e le relatedTargetthepa ea ketsahalo.
e bonts'itsoeng.bs.modal Ketsahalo ena e ts'oaroa ha modal e entsoe hore e bonahale ho mosebelisi (e tla emela hore liphetoho tsa CSS li phethehe). Haeba e bakoa ke ho tobetsa, ntho e tobelitsoeng e fumaneha e le relatedTargetthepa ea ketsahalo.
pata.bs.modal Ketsahalo ena e lelekoa hang hang ha hidemokhoa oa mohlala o bitsoa.
patiloeng.bs.modal Ketsahalo ena e ts'oaroa ha modal e qeta ho patoa ho mosebelisi (e tla emela hore liphetoho tsa CSS li phethehe).
$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})