Toasts
Thawb cov ntawv ceeb toom rau koj cov neeg tuaj saib nrog toast, lub teeb yuag thiab yooj yim customizable ceeb toom cov lus.
Toasts yog cov ntawv ceeb toom teeb pom kev zoo tsim los ua cov ntawv ceeb toom thawb uas tau nrov los ntawm cov xov tooj ntawm tes thiab desktop operating systems. Lawv tau tsim nrog flexbox, yog li lawv yooj yim rau kev sib dhos thiab txoj haujlwm.
Txheej txheem cej luam
Tej yam uas yuav tsum paub thaum siv lub toast plugin:
- Yog tias koj tab tom tsim peb JavaScript los ntawm qhov chaw, nws xav tau
util.js
. - Toasts yog opt-in rau kev ua tau zoo vim li cas, yog li koj yuav tsum pib lawv tus kheej .
- Thov nco ntsoov tias koj yog lub luag haujlwm rau kev tso cov toasts.
- Toasts yuav cia li nkaum yog tias koj tsis qhia
autohide: false
.
prefers-reduced-motion
lus nug xov xwm. Saib qhov
txo qis ntawm peb cov ntaub ntawv nkag mus tau .
Piv txwv
Basic
Txhawm rau txhawb nqa cov toasts uas tau nthuav dav thiab kwv yees tau, peb pom zoo kom lub taub hau thiab lub cev. Toast headers siv display: flex
, tso cai yooj yim sib dhos ntawm cov ntsiab lus ua tsaug rau peb cov paj thiab cov khoom siv flexbox.
Toasts yog hloov tau raws li koj xav tau thiab muaj tsawg heev xav tau markup. Qhov tsawg kawg nkaus, peb xav tau ib lub ntsiab lus kom muaj koj cov ntsiab lus "toasted" thiab txhawb kom lub pob tso tawm.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
Nyob
Nias lub pob hauv qab no los qhia ib qho toast (chaw nrog peb cov khoom siv hauv qab sab xis) uas tau muab zais los ntawm lub neej ntawd nrog .hide
.
<button type="button" class="btn btn-primary" id="liveToastBtn">Show live toast</button>
<div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 0; bottom: 0;">
<div id="liveToast" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="2000">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
</div>
Translucent
Toasts yog me ntsis translucent kom sib xyaw nrog dab tsi hauv qab lawv.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">11 mins ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
stacking
Thaum koj muaj ntau yam toasts, peb ua rau vertically stacking lawv nyob rau hauv ib tug nyeem tau.
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">just now</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</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 mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">2 seconds ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</div>
</div>
Qhov chaw
Muab cov toasts nrog kev cai CSS raws li koj xav tau. Sab saum toj txoj cai feem ntau yog siv rau kev ceeb toom, ib yam li sab saum toj nruab nrab. Yog tias koj tsuas yog yuav tsum tau qhia ib qho toast ib zaug, muab cov qauv tso cai rau ntawm .toast
.
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
<div class="toast" style="position: absolute; top: 0; right: 0;">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
</div>
Rau cov tshuab uas tsim cov ntawv ceeb toom ntxiv, xav txog kev siv cov khoom qhwv kom lawv tuaj yeem yooj yim pawg.
<div aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;">
<!-- Position it -->
<div style="position: absolute; top: 0; right: 0;">
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">just now</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</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 mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">2 seconds ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Heads up, toasts will stack automatically
</div>
</div>
</div>
</div>
Koj tuaj yeem tau txais kev zoo nkauj nrog cov khoom siv flexbox los ua kom cov toasts horizontally thiab / lossis vertically.
<!-- Flexbox container for aligning the toasts -->
<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-center align-items-center" style="height: 200px;">
<!-- Then put toasts within -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
</div>
Kev siv tau
Toasts yog npaj los cuam tshuam me me rau koj cov neeg tuaj saib lossis cov neeg siv, yog li txhawm rau pab cov neeg uas muaj cov ntawv nyeem thiab cov thev naus laus zis zoo sib xws, koj yuav tsum qhwv koj cov toasts hauv ib aria-live
cheeb tsam . Kev hloov pauv rau thaj chaw nyob (xws li txhaj tshuaj / hloov kho cov khoom noj toast) tau tshaj tawm los ntawm cov neeg siv tshuaj ntsuam yam tsis tas yuav txav tus neeg siv lub hom phiaj lossis cuam tshuam rau tus neeg siv. Tsis tas li ntawd, suav nrog aria-atomic="true"
kom paub meej tias tag nrho cov toast yog ib txwm tshaj tawm raws li ib qho (atomic) unit, es tsis yog tshaj tawm qhov hloov pauv (uas tuaj yeem ua rau muaj teeb meem yog tias koj tsuas yog hloov kho ib feem ntawm cov toast cov ntsiab lus, lossis yog tias tso saib tib lub ntsiab lus toast. tom qab lub sijhawm). Yog tias cov ntaub ntawv xav tau yog qhov tseem ceeb rau cov txheej txheem, piv txwv li rau ib daim ntawv teev cov kev ua yuam kev hauv ib daim ntawv, ces siv cov lus ceeb toom .tsis yog toast.
Nco ntsoov tias thaj av nyob yuav tsum muaj nyob rau hauv cov cim ua ntej lub toast yog tsim los yog hloov tshiab. Yog tias koj dynamically tsim ob qho tib si tib lub sijhawm thiab txhaj rau hauv nplooj ntawv, lawv feem ntau yuav tsis raug tshaj tawm los ntawm cov cuab yeej pabcuam.
Koj kuj yuav tsum tau hloov kho role
thiab aria-live
qib nyob ntawm cov ntsiab lus. Yog tias nws yog cov lus tseem ceeb xws li kev ua yuam kev, siv role="alert" aria-live="assertive"
, txwv tsis pub siv cov role="status" aria-live="polite"
cwj pwm.
Raws li cov ntsiab lus koj tab tom nthuav tawm cov kev hloov pauv, nco ntsoov hloov kho lub delay
sijhawm kom cov neeg siv sijhawm txaus los nyeem cov toast.
<div class="toast" role="alert" aria-live="polite" aria-atomic="true" data-delay="10000">
<div role="alert" aria-live="assertive" aria-atomic="true">...</div>
</div>
Thaum siv autohide: false
, koj yuav tsum ntxiv khawm kaw kom tso cai rau cov neeg siv tshem tawm cov toast.
<div role="alert" aria-live="assertive" aria-atomic="true" class="toast" data-autohide="false">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small>11 mins ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
Thaum technically nws muaj peev xwm ntxiv kev tsom xam / kev tswj tau (xws li cov nyees khawm ntxiv lossis txuas) hauv koj lub ci ci, koj yuav tsum tsis txhob ua qhov no rau autohiding toasts. Txawm hais tias koj muab lub khob cij rau lub sijhawm ntev delay
, cov keyboard thiab cov neeg siv thev naus laus zis tuaj yeem pom tias nws nyuaj rau kev ncav cuag lub ci ci hauv lub sijhawm ua haujlwm (vim cov toasts tsis tau txais kev tsom mus rau thaum lawv pom). Yog tias koj yuav tsum muaj kev tswj xyuas ntxiv, peb pom zoo kom siv lub toast nrog autohide: false
.
JavaScript cwj pwm
Kev siv
Pib pib toasts ntawm JavaScript:
$('.toast').toast(option)
Kev xaiv
Cov kev xaiv tuaj yeem dhau los ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript. Rau cov ntaub ntawv tus cwj pwm, ntxiv rau qhov kev xaiv npe rau data-
, xws li hauv data-animation=""
.
Lub npe | Hom | Default | Kev piav qhia |
---|---|---|---|
animation | boolean | muaj tseeb | Thov ib qho CSS ploj mus rau qhov toast |
autohide | boolean | muaj tseeb | Nws pib zais lub toast |
ncua | tus lej | 500 |
Ncua zais qhov ci ci (ms) |
Cov txheej txheem
Asynchronous txoj kev thiab kev hloov
Txhua txoj kev API yog asynchronous thiab pib hloov pauv . Lawv rov qab mus rau tus neeg hu xov tooj sai li sai tau thaum qhov kev hloov pauv pib tab sis ua ntej nws xaus . Tsis tas li ntawd, ib txoj kev hu rau ib qho kev hloov pauv yuav raug tsis quav ntsej .
$().toast(options)
Txuas lub toast handler rau ib qho khoom sau.
.toast('show')
Tshaj tawm ib qho khoom ci ci. Rov qab mus rau tus neeg hu ua ntej qhov toast tau tshwm sim tiag tiag (piv txwv li ua ntej qhov shown.bs.toast
xwm txheej tshwm sim). Koj yuav tsum manually hu rau txoj kev no, es tsis txhob koj toast yuav tsis qhia.
$('#element').toast('show')
.toast('hide')
Hide ib qho khoom ci ci. Rov qab mus rau tus neeg hu ua ntej qhov toast tau muab zais tiag tiag (piv txwv li ua ntej qhov hidden.bs.toast
xwm txheej tshwm sim). Koj yuav tsum manually hu rau txoj kev no yog tias koj ua autohide
rau false
.
$('#element').toast('hide')
.toast('dispose')
Hide ib qho khoom ci ci. Koj lub ncuav yuav nyob twj ywm ntawm DOM tab sis yuav tsis tshwm sim ntxiv lawm.
$('#element').toast('dispose')
Cov xwm txheej
Hom xwm txheej | Kev piav qhia |
---|---|
show.bs.toast | Qhov xwm txheej no tua hluav taws tam sim ntawd thaum show hu ua piv txwv. |
ua.bs.toast | Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub ci ci tau pom rau tus neeg siv. |
hide.bs.toast | Qhov xwm txheej no raug rho tawm haujlwm tam sim ntawd thaum hide hu ua piv txwv txoj kev. |
zais.bs.toast | Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub toast tiav lawm muab zais los ntawm tus neeg siv. |
$('#myToast').on('hidden.bs.toast', function () {
// do something...
})