Toasts
Shiriki arifa kwa wageni wako na toast, ujumbe wa tahadhari unaoweza kubinafsishwa kwa urahisi.
Toasts ni arifa nyepesi zilizoundwa kuiga arifa zinazotumwa na programu hata wakati huitumii ambazo zimeenezwa na mifumo ya uendeshaji ya simu na kompyuta ya mezani. Zimeundwa kwa flexbox, kwa hivyo ni rahisi kupangilia na kuziweka.
Muhtasari
Mambo ya kujua unapotumia programu-jalizi ya toast:
- Toasts ni kuchagua kuingia kwa sababu za utendaji, kwa hivyo ni lazima uzianzishe wewe mwenyewe .
- Toasts zitajificha kiotomatiki ikiwa hutabainisha
autohide: false
.
prefers-reduced-motion
hoja ya midia. Tazama
sehemu ya mwendo iliyopunguzwa ya hati zetu za ufikivu .
Mifano
Msingi
Ili kuhimiza toasts kupanuliwa na kutabirika, tunapendekeza kichwa na mwili. Vijajuu vya toast hutumia display: flex
, kuruhusu upangaji rahisi wa maudhui kutokana na ukingo wetu na huduma za flexbox.
Toasts ni rahisi kama unavyohitaji na zina alama ndogo sana zinazohitajika. Kwa uchache, tunahitaji kipengele kimoja ili kuwa na maudhui yako "yaliyochapwa" na kuhimiza kwa nguvu kitufe cha kukataa.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
.hide
darasa kwa nguvu ili kuficha toast kabisa (na
display:none
, badala ya na
opacity:0
). Hii sasa sio lazima tena. Walakini, kwa utangamano wa nyuma, hati yetu itaendelea kugeuza darasa (ingawa hakuna hitaji la vitendo) hadi toleo kuu linalofuata.
Mfano hai
Bofya kitufe kilicho hapa chini ili kuonyesha toast (iliyowekwa pamoja na huduma zetu kwenye kona ya chini kulia) ambayo imefichwa kwa chaguo-msingi.
<button type="button" class="btn btn-primary" id="liveToastBtn">Show live toast</button>
<div class="position-fixed bottom-0 end-0 p-3" style="z-index: 11">
<div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
</div>
Tunatumia JavaScript ifuatayo ili kuanzisha onyesho letu la toast moja kwa moja:
var toastTrigger = document.getElementById('liveToastBtn')
var toastLiveExample = document.getElementById('liveToast')
if (toastTrigger) {
toastTrigger.addEventListener('click', function () {
var toast = new bootstrap.Toast(toastLiveExample)
toast.show()
})
}
Uwazi
Toasts hung'aa kidogo ili kuchanganyika na kile kilicho chini yao.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="text-muted">11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
Kuweka mrundikano
Unaweza kuweka toasts kwa kuifunga kwenye chombo cha toast, ambacho kitaongeza nafasi kwa wima.
<div class="toast-container">
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="text-muted">just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
See? Just like this.
</div>
</div>
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="text-muted">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</div>
</div>
</div>
Maudhui maalum
Binafsisha toast zako kwa kuondoa vijenzi vidogo, kuvirekebisha na huduma , au kwa kuongeza alama zako mwenyewe. Hapa tumeunda toast rahisi zaidi kwa kuondoa chaguo-msingi .toast-header
, kuongeza ikoni maalum ya kujificha kutoka kwa Aikoni za Bootstrap , na kutumia baadhi ya huduma za flexbox kurekebisha mpangilio.
<div class="toast align-items-center" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body">
Hello, world! This is a toast message.
</div>
<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
Vinginevyo, unaweza pia kuongeza vidhibiti vya ziada na vipengele kwa toasts.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-body">
Hello, world! This is a toast message.
<div class="mt-2 pt-2 border-top">
<button type="button" class="btn btn-primary btn-sm">Take action</button>
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="toast">Close</button>
</div>
</div>
</div>
Mipango ya rangi
Kwa kuzingatia mfano hapo juu, unaweza kuunda mipango tofauti ya rangi ya toast na huduma zetu za rangi na mandharinyuma . Hapa tumeongeza .bg-primary
na .text-white
kwa .toast
, na kisha tukaongeza .btn-close-white
kwenye kitufe chetu cha kufunga. Kwa ukingo mkali, tunaondoa mpaka chaguo-msingi na .border-0
.
<div class="toast align-items-center text-white bg-primary border-0" role="alert" aria-live="assertive" aria-atomic="true">
<div class="d-flex">
<div class="toast-body">
Hello, world! This is a toast message.
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
</div>
Uwekaji
Weka toasts kwa kutumia CSS maalum unavyohitaji. Sehemu ya juu kulia mara nyingi hutumiwa kwa arifa, kama ilivyo sehemu ya juu ya kati. Iwapo utawahi tu kuonyesha toast moja kwa wakati mmoja, weka mitindo ya kuweka kwenye .toast
.
<form>
<div class="mb-3">
<label for="selectToastPlacement">Toast placement</label>
<select class="form-select mt-2" id="selectToastPlacement">
<option value="" selected>Select a position...</option>
<option value="top-0 start-0">Top left</option>
<option value="top-0 start-50 translate-middle-x">Top center</option>
<option value="top-0 end-0">Top right</option>
<option value="top-50 start-0 translate-middle-y">Middle left</option>
<option value="top-50 start-50 translate-middle">Middle center</option>
<option value="top-50 end-0 translate-middle-y">Middle right</option>
<option value="bottom-0 start-0">Bottom left</option>
<option value="bottom-0 start-50 translate-middle-x">Bottom center</option>
<option value="bottom-0 end-0">Bottom right</option>
</select>
</div>
</form>
<div aria-live="polite" aria-atomic="true" class="bg-dark position-relative bd-example-toasts">
<div class="toast-container position-absolute p-3" id="toastPlacement">
<div class="toast">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
</div>
</div>
Kwa mifumo inayozalisha arifa zaidi, zingatia kutumia kipengee cha kufunga ili ziweze kupangwa kwa urahisi.
<div aria-live="polite" aria-atomic="true" class="position-relative">
<!-- Position it: -->
<!-- - `.toast-container` for spacing between toasts -->
<!-- - `.position-absolute`, `top-0` & `end-0` to position the toasts in the upper right corner -->
<!-- - `.p-3` to prevent the toasts from sticking to the edge of the container -->
<div class="toast-container position-absolute top-0 end-0 p-3">
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="text-muted">just now</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
See? Just like this.
</div>
</div>
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small class="text-muted">2 seconds ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</div>
</div>
</div>
</div>
Unaweza pia kupendezwa na huduma za flexbox ili kupanga toasts mlalo na/au wima.
<!-- Flexbox container for aligning the toasts -->
<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center w-100">
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
</div>
Ufikivu
Toast inakusudiwa kuwa usumbufu mdogo kwa wageni au watumiaji wako, kwa hivyo ili kuwasaidia wale walio na visoma skrini na teknolojia sawa za usaidizi, unapaswa kukunja toast zako katika aria-live
eneo . Mabadiliko kwenye maeneo ya moja kwa moja (kama vile kuingiza/kusasisha kijenzi cha toast) hutangazwa kiotomatiki na visoma skrini bila kuhitaji kusogeza lengo la mtumiaji au kumkatiza mtumiaji vinginevyo. Zaidi ya hayo, jumuisha aria-atomic="true"
kuhakikisha kuwa toast nzima inatangazwa kila wakati kama kitengo kimoja (atomiki), badala ya kutangaza tu kile kilichobadilishwa (jambo ambalo linaweza kusababisha matatizo ikiwa utasasisha tu sehemu ya maudhui ya toast, au ikiwa inaonyesha maudhui sawa ya toast. wakati wa baadaye). Ikiwa taarifa inayohitajika ni muhimu kwa mchakato, kwa mfano kwa orodha ya makosa katika fomu, basi tumia kipengele cha tahadhari .badala ya toast.
Kumbuka kuwa eneo la moja kwa moja linahitaji kuwepo katika ghafi kabla ya toast kuzalishwa au kusasishwa. Ukitengeneza zote mbili kwa wakati mmoja na kuziingiza kwenye ukurasa, kwa ujumla hazitatangazwa na teknolojia za usaidizi.
Pia unahitaji kurekebisha role
na aria-live
kiwango kulingana na yaliyomo. Ikiwa ni ujumbe muhimu kama kosa, tumia role="alert" aria-live="assertive"
, vinginevyo tumia role="status" aria-live="polite"
sifa.
Kadiri maudhui unayoonyesha yanavyobadilika, hakikisha kwamba umesasisha muda wa delay
kuisha ili watumiaji wapate muda wa kutosha wa kusoma toast.
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-bs-delay="10000">
<div role="alert" aria-live="assertive" aria-atomic="true">...</div>
</div>
Unapotumia autohide: false
, lazima uongeze kitufe cha kufunga ili kuruhusu watumiaji kuondoa toast.
<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-bs-autohide="false">
<div class="toast-header">
<img src="..." class="rounded me-2" alt="...">
<strong class="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
Ingawa kitaalamu inawezekana kuongeza vidhibiti vinavyoweza kuangaziwa/kutekelezeka (kama vile vitufe au viungo vya ziada) kwenye toast yako, unapaswa kuepuka kufanya hivi kwa kuficha toast kiotomatiki. Hata ukitoa toast muda mrefu delay
kuisha , watumiaji wa kibodi na teknolojia ya usaidizi wanaweza kupata ugumu wa kufikia toast kwa wakati ili kuchukua hatua (kwa kuwa toasts hazizingatiwi zinapoonyeshwa). Ikiwa lazima uwe na vidhibiti zaidi, tunapendekeza utumie toast na autohide: false
.
Sass
Vigezo
$toast-max-width: 350px;
$toast-padding-x: .75rem;
$toast-padding-y: .5rem;
$toast-font-size: .875rem;
$toast-color: null;
$toast-background-color: rgba($white, .85);
$toast-border-width: 1px;
$toast-border-color: rgba($black, .1);
$toast-border-radius: $border-radius;
$toast-box-shadow: $box-shadow;
$toast-spacing: $container-padding-x;
$toast-header-color: $gray-600;
$toast-header-background-color: rgba($white, .85);
$toast-header-border-color: rgba($black, .05);
Matumizi
Anzisha toasts kupitia JavaScript:
var toastElList = [].slice.call(document.querySelectorAll('.toast'))
var toastList = toastElList.map(function (toastEl) {
return new bootstrap.Toast(toastEl, option)
})
Vichochezi
Kuachishwa kunaweza kupatikana kwa data
sifa kwenye kitufe ndani ya toast kama inavyoonyeshwa hapa chini:
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
au kwenye kitufe nje ya toast kwa kutumia data-bs-target
kama inavyoonyeshwa hapa chini:
<button type="button" class="btn-close" data-bs-dismiss="toast" data-bs-target="#my-toast" aria-label="Close"></button>
Chaguo
Chaguzi zinaweza kupitishwa kupitia sifa za data au JavaScript. Kwa sifa za data, ongeza jina la chaguo kwa data-bs-
, kama katika data-bs-animation=""
.
Jina | Aina | Chaguomsingi | Maelezo |
---|---|---|---|
animation |
boolean | true |
Tumia mpito wa kufifia kwa CSS kwenye toast |
autohide |
boolean | true |
Ficha toast kiotomatiki |
delay |
nambari | 5000 |
Kuchelewa kuficha toast (ms) |
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 .
onyesha
Huonyesha toast ya kipengele. Hurejesha kwa mpigaji simu kabla toast haijaonyeshwa (yaani kabla ya shown.bs.toast
tukio kutokea). Utalazimika kupiga simu kwa njia hii mwenyewe, badala yake toast yako haitaonekana.
toast.show()
kujificha
Huficha toast ya kipengele. Hurejesha kwa mpigaji simu kabla toast haijafichwa (yaani kabla ya hidden.bs.toast
tukio kutokea). Lazima upigie simu njia hii mwenyewe ikiwa ulifanya autohide
.false
toast.hide()
tupa
Huficha toast ya kipengele. Toast yako itasalia kwenye DOM lakini haitaonekana tena.
toast.dispose()
getInstance
Njia tuli ambayo hukuruhusu kupata mfano wa toast unaohusishwa na kipengee cha DOM
var myToastEl = document.getElementById('myToastEl')
var myToast = bootstrap.Toast.getInstance(myToastEl) // Returns a Bootstrap toast instance
getOrCreateInstance
Njia tuli ambayo hukuruhusu kupata mfano wa toast unaohusishwa na kipengee cha DOM, au kuunda mpya ikiwa haikuanzishwa.
var myToastEl = document.getElementById('myToastEl')
var myToast = bootstrap.Toast.getOrCreateInstance(myToastEl) // Returns a Bootstrap toast instance
Matukio
Aina ya tukio | Maelezo |
---|---|
show.bs.toast |
Tukio hili huwaka mara moja wakati show njia ya mfano inaitwa. |
shown.bs.toast |
Tukio hili huwashwa wakati toast imefanywa kuonekana kwa mtumiaji. |
hide.bs.toast |
Tukio hili linafutwa mara moja wakati hide njia ya mfano imeitwa. |
hidden.bs.toast |
Tukio hili huwashwa wakati toast imekamilika kufichwa kutoka kwa mtumiaji. |
var myToastEl = document.getElementById('myToast')
myToastEl.addEventListener('hidden.bs.toast', function () {
// do something...
})