Modali
Gwiritsani ntchito pulogalamu yowonjezera ya JavaScript ya Bootstrap kuti muwonjezere zokambirana patsamba lanu la mabokosi opepuka, zidziwitso za ogwiritsa ntchito, kapena zomwe mumakonda.
Momwe zimagwirira ntchito
Musanayambe ndi gawo la Bootstrap, onetsetsani kuti mwawerenga zotsatirazi popeza zosankha zathu zasintha posachedwa.
- Ma modals amapangidwa ndi HTML, CSS, ndi JavaScript. Iwo ali pamwamba pa china chirichonse mu chikalatacho ndi kuchotsa mpukutu
<body>
kuti modal zolembedwa mipukutu m'malo. - Kusindikiza pa modal "backdrop" kudzatseka modal.
- Bootstrap imathandizira zenera limodzi la modal nthawi imodzi. Makhalidwe a Nested sagwiritsidwa ntchito chifukwa timakhulupirira kuti sagwiritsa ntchito bwino.
- Ma Modals amagwiritsa ntchito
position: fixed
, zomwe nthawi zina zimatha kukhala zodziwika bwino pakumasulira kwake. Ngati n'kotheka, ikani HTML yanu pamalo apamwamba kuti musasokonezedwe ndi zinthu zina. Mutha kukumana ndi zovuta mukamanga zisa.modal
mkati mwa chinthu china chokhazikika. - Apanso, chifukwa cha
position: fixed
, pali machenjezo ogwiritsira ntchito ma modals pazida zam'manja. Onani zolemba zathu zothandizira msakatuli kuti mumve zambiri. - Chifukwa cha momwe HTML5 imatanthauzira semantics yake, mawonekedwe a
autofocus
HTML alibe mphamvu mu ma modals a Bootstrap. Kuti mukwaniritse zomwezo, gwiritsani ntchito JavaScript yokhazikika:
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')
myModal.addEventListener('shown.bs.modal', function () {
myInput.focus()
})
prefers-reduced-motion
funso la media. Onani gawo
lochepetsedwa la zolemba zathu zofikira .
Pitilizani kuwerenga ma demo ndi malangizo ogwiritsira ntchito.
Zitsanzo
Zigawo za modal
Pansipa pali static modal chitsanzo (kutanthauza zake position
ndipo display
zasinthidwa). Kuphatikizidwa ndi mutu wa modal, thupi la modal (lofunikira padding
) ndi modal footer (posankha). Tikukupemphani kuti muphatikizepo mitu yamutu yokhala ndi zochita zochotsa ngati kuli kotheka, kapena muperekepo kanthu kena kochotsa.
<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>
Chiwonetsero chamoyo
Sinthani mawonekedwe ogwirira ntchito podina batani pansipa. Idzatsika ndikuzimiririka kuchokera pamwamba pa tsamba.
<!-- 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>
Zosasunthika
Kumbuyo kukakhala static, modal sitseka mukadina kunja kwake. Dinani batani pansipa kuti muyese.
<!-- 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>
Kusuntha zinthu zazitali
Ma mods akakhala ataliatali kwambiri kuti ayang'ane ndi wogwiritsa ntchito kapena chipangizo, amasuntha mosadalira tsambalo. Yesani chiwonetsero pansipa kuti muwone zomwe tikutanthauza.
Mukhozanso kupanga modal scrollable yomwe imalola kusuntha thupi la modal powonjezera .modal-dialog-scrollable
ku .modal-dialog
.
<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
...
</div>
Chokhazikika pakati
Onjezani .modal-dialog-centered
ku .modal-dialog
molunjika pakati pa 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>
Tooltips ndi popovers
Zida ndi popovers zitha kuyikidwa m'ma modal ngati pakufunika . Ma modals akatsekedwa, zida zilizonse ndi ma popovers mkati mwake amachotsedwanso.
<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>
Kugwiritsa ntchito grid
Gwiritsani ntchito gridi ya Bootstrap mkati mwa modal pomanga zisa .container-fluid
mkati mwa .modal-body
. Kenako, gwiritsani ntchito makalasi amtundu wa grid monga momwe mungachitire kwina kulikonse.
<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>
Mitundu yosiyanasiyana ya modal
Muli ndi mabatani angapo omwe onse amayambitsa modali yofanana ndi zomwe zili zosiyana pang'ono? Gwiritsani ntchito event.relatedTarget
ndi mawonekedwe a HTML data-bs-*
kuti musinthe zomwe zili mu modal kutengera batani lomwe ladina.
Pansipa pali chiwonetsero chamoyo chotsatiridwa ndi chitsanzo cha HTML ndi JavaScript. Kuti mumve zambiri, werengani zolemba zamodal zochitika kuti mumve zambiri 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
})
Sinthani pakati pa ma modals
Sinthani pakati pa ma mods angapo ndikuyika mochenjera kwa data-bs-target
ndi mawonekedwe data-bs-toggle
. Mwachitsanzo, mutha kusintha modal yokhazikitsira mawu achinsinsi kuchokera mu modal yotsegulidwa kale. Chonde dziwani kuti ma mods angapo sangatsegulidwe nthawi imodzi - njira iyi imangosintha pakati pa mitundu iwiri yosiyana.
<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">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">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>
Sinthani makanema ojambula
Kusinthaku $modal-fade-transform
kumatsimikizira kusintha kwa .modal-dialog
makanema ojambula a modal fade-in, $modal-show-transform
kusintha kumatsimikizira kusintha kwa .modal-dialog
kumapeto kwa makanema ojambula pa modal fade-in.
Ngati mukufuna mwachitsanzo makanema ojambula pazithunzi, mutha kukhazikitsa $modal-fade-transform: scale(.8)
.
Chotsani makanema ojambula
Kwa ma mods omwe amangowoneka m'malo mozimiririka kuti awoneke, chotsani .fade
kalasiyo pazolemba zanu.
<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
...
</div>
Matali amphamvu
Ngati kutalika kwa modal kumasintha pamene ili yotseguka, muyenera kuyimba myModal.handleUpdate()
kuti mukonzenso malo a modal ngati scrollbar ikuwoneka.
Kufikika
Onetsetsani kuti muwonjezere aria-labelledby="..."
, kutengera mutu wa modal, ku .modal
. Kuphatikiza apo, mutha kufotokoza za dialog yanu ya modal ndi aria-describedby
pa .modal
. Dziwani kuti simukuyenera kuwonjezera role="dialog"
popeza tidawonjezera kale kudzera pa JavaScript.
Kuyika makanema a YouTube
Kuyika makanema a YouTube mu ma modals kumafuna JavaScript yowonjezera osati mu Bootstrap kuti asiye kusewera ndi zina zambiri. Onani positi yothandiza ya Stack Overflow kuti mumve zambiri.
Zosankha zazikulu
Ma Modals ali ndi masaizi atatu osasankha, omwe amapezeka kudzera pamagulu osintha kuti ayikidwe pa .modal-dialog
. Kukula uku kumayambira pazipata zina kuti mupewe zopingasa zopingasa pamawonekedwe ocheperako.
Kukula | Kalasi | Modal max-width |
---|---|---|
Wamng'ono | .modal-sm |
300px |
Zosasintha | Palibe | 500px |
Chachikulu | .modal-lg |
800px |
Chachikulu kwambiri | .modal-xl |
1140px |
Modal yathu yosasinthika yopanda kalasi yosinthira imapanga "zapakatikati" kukula kwake.
<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>
Fullscreen Modal
Chowonjezera china ndi njira yopangira modal yomwe imakhudza mawonekedwe a ogwiritsa ntchito, yomwe imapezeka kudzera pamagulu osintha omwe amayikidwa pa .modal-dialog
.
Kalasi | Kupezeka |
---|---|
.modal-fullscreen |
Nthawizonse |
.modal-fullscreen-sm-down |
M'munsimu576px |
.modal-fullscreen-md-down |
M'munsimu768px |
.modal-fullscreen-lg-down |
M'munsimu992px |
.modal-fullscreen-xl-down |
M'munsimu1200px |
.modal-fullscreen-xxl-down |
M'munsimu1400px |
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
...
</div>
Sass
Zosintha
$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);
Lupu
Mawonekedwe omvera a sikirini yonse amapangidwa kudzera $breakpoints
pamapu ndi kuzungulira mu 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);
}
}
}
}
Kugwiritsa ntchito
Pulogalamu yowonjezera ya modal imasintha zinthu zanu zobisika mukafuna, kudzera pa data kapena JavaScript. Imadutsanso machitidwe osasinthika osasunthika ndikupanga a .modal-backdrop
kuti apereke malo odina kuti muchotse ma modal owonetsedwa mukadina kunja kwa modal.
Kudzera muzochita za data
Sinthani
Yambitsani modali osalemba JavaScript. Khazikitsani data-bs-toggle="modal"
chinthu chowongolera, ngati batani, limodzi ndi data-bs-target="#foo"
kapena href="#foo"
kutsata njira inayake kuti musinthe.
<button type="button" data-bs-toggle="modal" data-bs-target="#myModal">Launch modal</button>
Chotsani
Kuthamangitsidwa kumatha kutheka ndi lingaliro lomwe lili data
pa batani mkati mwa modal monga momwe zasonyezedwera pansipa:
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
kapena pa batani kunja kwa modali pogwiritsa ntchito data-bs-target
zomwe zili pansipa:
<button type="button" class="btn-close" data-bs-dismiss="modal" data-bs-target="#my-modal" aria-label="Close"></button>
Kudzera pa JavaScript
Pangani modali ndi mzere umodzi wa JavaScript:
var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)
Zosankha
Zosankha zitha kuperekedwa kudzera pa data kapena JavaScript. Pamawonekedwe a data, yonjezerani dzina lachisankho ku data-bs-
, monga mu data-bs-backdrop=""
.
Dzina | Mtundu | Zosasintha | Kufotokozera |
---|---|---|---|
backdrop |
boolean kapena chingwe'static' |
true |
Mulinso modal-backdrop element. Kapenanso, tchulani static zakumbuyo zomwe sizitseka moduli mukadina. |
keyboard |
boolean | true |
Amatseka modali pamene kiyi yothawa ikanikizidwa |
focus |
boolean | true |
Imayika chidwi pa modal ikakhazikitsidwa. |
Njira
Asynchronous njira ndi kusintha
Njira zonse za API ndizosasinthika ndipo zimayamba kusintha . Amabwerera kwa woyimbayo atangoyamba kusintha koma asanathe . Kuonjezera apo, kuyitana kwa njira pa gawo losintha kudzanyalanyazidwa .
Zosankha zodutsa
Imayambitsa zomwe zili ngati modal. Imavomereza zomwe mungachite object
.
var myModal = new bootstrap.Modal(document.getElementById('myModal'), {
keyboard: false
})
kusintha
Imatembenuza pamanja modali. Imabwereranso kwa woyimba foniyo isanasonyezedwe kapena kubisidwa (ie zisanachitike shown.bs.modal
kapena hidden.bs.modal
chochitikacho).
myModal.toggle()
chiwonetsero
Pamanja amatsegula modali. Imabwereranso kwa woyimba foniyo isanasonyezedwe (ie zisanachitike shown.bs.modal
).
myModal.show()
Komanso, mutha kudutsa gawo la DOM ngati mkangano womwe ungalandilidwe muzochitika za modal (monga relatedTarget
katundu).
var modalToggle = document.getElementById('toggleMyModal') // relatedTarget
myModal.show(modalToggle)
kubisa
Amabisa pamanja modali. Imabwereranso kwa woyimbira foniyo isanabisike (ie zisanachitike hidden.bs.modal
).
myModal.hide()
handleUpdate
Sinthani pamanja malo a modal ngati kutalika kwa modali kukusintha pamene ili lotseguka (mwachitsanzo, ngati mpukutu utawonekera).
myModal.handleUpdate()
kutaya
Imawononga modali ya chinthu. (Imachotsa deta yosungidwa pa chinthu cha DOM)
myModal.dispose()
getInstance
Njira yosasunthika yomwe imakupatsani mwayi wopeza modal yolumikizidwa ndi chinthu cha DOM
var myModalEl = document.getElementById('myModal')
var modal = bootstrap.Modal.getInstance(myModalEl) // Returns a Bootstrap modal instance
GetOrCreateInstance
Njira yosasunthika yomwe imakupatsani mwayi wopeza mawonekedwe olumikizidwa ndi chinthu cha DOM, kapena pangani china china ngati sichinayambike.
var myModalEl = document.querySelector('#myModal')
var modal = bootstrap.Modal.getOrCreateInstance(myModalEl) // Returns a Bootstrap modal instance
Zochitika
Kalasi ya modal ya Bootstrap imawulula zochitika zingapo kuti zigwirizane ndi machitidwe a modal. Zochitika zonse za modal zimachotsedwa pa modal yokha (ie pa <div class="modal">
).
Mtundu wa chochitika | Kufotokozera |
---|---|
show.bs.modal |
Chochitika ichi chimayaka nthawi yomweyo show njira yachitsanzo itayitanidwa. Ngati chifukwa cha kudina, chinthu chodina chimapezeka ngati relatedTarget katundu wa chochitikacho. |
shown.bs.modal |
Chochitikachi chimachotsedwa pamene modal yawonetsedwa kwa wogwiritsa ntchito (idzadikirira kuti kusintha kwa CSS kumalize). Ngati chifukwa cha kudina, chinthu chodina chimapezeka ngati relatedTarget katundu wa chochitikacho. |
hide.bs.modal |
Chochitika ichi chimachotsedwa nthawi yomweyo pamene hide njira yachitsanzo yayitanidwa. |
hidden.bs.modal |
Chochitikachi chimachotsedwa pamene modal yatha kubisidwa kwa wogwiritsa ntchito (idikira kuti kusintha kwa CSS kumalize). |
hidePrevented.bs.modal |
Chochitikachi chimathamangitsidwa pamene modal ikuwonetsedwa, kumbuyo kwake static ndikudina kunja kwa modal kapena makina othawa amachitidwa ndi njira ya kiyibodi kapena data-bs-keyboard kukhazikitsidwa kwa false . |
var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', function (event) {
// do something...
})