警告框(Alerts)
为典型用户操作提供上下文反馈消息,并提供少量可用且灵活的警报消息。
Examples
警报可用于任何长度的文本,以及可选的关闭按钮。要获得正确的样式,请使用八个必需的上下文类之一(例如,.alert-success
)。对于内联移除,请使用alerts JavaScript插件。
<div class="alert alert-primary" role="alert">
A simple primary alert—check it out!
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert—check it out!
</div>
<div class="alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert—check it out!
</div>
<div class="alert alert-info" role="alert">
A simple info alert—check it out!
</div>
<div class="alert alert-light" role="alert">
A simple light alert—check it out!
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert—check it out!
</div>
向辅助技术传达意义
使用颜色来增加意义只会提供一种视觉指示,而不会传达给辅助技术(如屏幕阅读器)的用户。确保由颜色表示的信息在内容本身(例如可见文本)中是明显的,或者通过其他方式包含,例如使用
.visually-hidden
类隐藏的其他文本。
链接颜色
使用 .alert-link
实用程序类可以在任何警报中快速提供匹配的彩色链接。
<div class="alert alert-primary" role="alert">
A simple primary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-success" role="alert">
A simple success alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-info" role="alert">
A simple info alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-light" role="alert">
A simple light alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
附加内容
警报还可以包含其他HTML元素,如标题、段落和分隔符。
Well done!
Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.
Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Well done!</h4>
<p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p>
<hr>
<p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</div>
移除
使用alert JavaScript插件,可以关闭任何内联警报。方法如下:
- 确保已加载警报插件或已编译的引导JavaScript。
- 添加一个关闭按钮和
.alert-dismissible
类,该类在警报的右侧添加额外的填充,并定位关闭按钮。 - 在close按钮上,添加code>data-bs-dismiss="alert"属性,该属性触发JavaScript功能。一定要使用button元素在所有设备上进行正确的操作。
- 要在解除警报时设置警报动画,请确保添加.fade和.show类。
您可以在现场演示中看到这一点:
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
tabindex="-1"
添加到该元素。
Sass
Variables
$alert-padding-y: $spacer;
$alert-padding-x: $spacer;
$alert-margin-bottom: 1rem;
$alert-border-radius: $border-radius;
$alert-link-font-weight: $font-weight-bold;
$alert-border-width: $border-width;
$alert-bg-scale: -80%;
$alert-border-scale: -70%;
$alert-color-scale: 40%;
$alert-dismissible-padding-r: $alert-padding-x * 3; // 3x covers width of x plus default padding on either side
Variant mixin
Used in combination with $theme-colors
to create contextual modifier classes for our alerts.
@mixin alert-variant($background, $border, $color) {
color: $color;
@include gradient-bg($background);
border-color: $border;
.alert-link {
color: shade-color($color, 20%);
}
}
Loop
Loop that generates the modifier classes with the alert-variant()
mixin.
// Generate contextual modifier classes for colorizing the alert.
@each $state, $value in $theme-colors {
$alert-background: shift-color($value, $alert-bg-scale);
$alert-border: shift-color($value, $alert-border-scale);
$alert-color: shift-color($value, $alert-color-scale);
@if (contrast-ratio($alert-background, $alert-color) < $min-contrast-ratio) {
$alert-color: mix($value, color-contrast($alert-background), abs($alert-color-scale));
}
.alert-#{$state} {
@include alert-variant($alert-background, $alert-border, $alert-color);
}
}
JavaScript behavior
Triggers
Enable dismissal of an alert via JavaScript:
var alertList = document.querySelectorAll('.alert')
alertList.forEach(function (alert) {
new bootstrap.Alert(alert)
})
Or with data
attributes on a button within the alert, as demonstrated above:
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
Note that closing an alert will remove it from the DOM.
Methods
You can create an alert instance with the alert constructor, for example:
var myAlert = document.getElementById('myAlert')
var bsAlert = new bootstrap.Alert(myAlert)
This makes an alert listen for click events on descendant elements which have the
data-bs-dismiss="alert"
attribute. (Not necessary when using the data-api’s auto-initialization.)
Method | Description |
---|---|
close
|
Closes an alert by removing it from the DOM. If the .fade and .show classes are
present on the element, the alert will fade out before it is removed.
|
dispose
|
Destroys an element's alert. (Removes stored data on the DOM element) |
getInstance
|
Static method which allows you to get the alert instance associated to a DOM element, you can use it like
this: bootstrap.Alert.getInstance(alert)
|
var alertNode = document.querySelector('.alert')
var alert = bootstrap.Alert.getInstance(alertNode)
alert.close()
Events
Bootstrap’s alert plugin exposes a few events for hooking into alert functionality.
Event | Description |
---|---|
close.bs.alert |
Fires immediately when the close instance method is called.
|
closed.bs.alert |
Fired when the alert has been closed and CSS transitions have completed. |
var myAlert = document.getElementById('myAlert')
myAlert.addEventListener('closed.bs.alert', function () {
// do something, for instance, explicitly move focus to the most appropriate element,
// so it doesn't get lost/reset to the start of the page
// document.getElementById('...').focus()
})