你可以使用这些参数,通过一个对象传入到sweetAlert中:
| 参数 | 类型 | 概述 |
|---|---|---|
| text | string | 弹窗的内容 |
| title | string | 弹窗的标题 |
| icon | string | 弹窗的图标 |
| button | string boolean ButtonOptions | 修改弹窗默认按钮的样式 |
| buttons | boolean string[] ButtonOptions[] ButtonList | 自定义弹窗的按钮 |
| content | Node string | 用户自定义内容 |
| className | string | 为弹窗添加一个自定义CLASS |
| closeOnClickOutside | boolean | 是否可通过遮罩关闭弹窗 |
| closeOnEsc | boolean | 是否可通过esc键关闭弹窗 |
| dangerMode | boolean | 将弹窗样式变为关键提示样式 |
| timer | number | 设置弹窗自动关闭时间 |
textstring
""(空字符串)
弹窗的文本文字。它既可以作为配置的参数添加(如下例所示),也可以作为第一个也是唯一的参数传递(例如:swal("Hello world!")),如果你有多个字符串参数则传递给第二个参数(例如:swal("A title", "Hello world!"))。
swal({
text: "Hello world!",
});titlestring
""(空字符串)
弹窗的标题文字。它既可以作为配置的参数添加(如下例所示),也可以作为swal函数的第一个参数(当swal函数有多个参数时)进行传递(例如:swal("这是标题!","这是一些文字"))。
swal({
title: "这是一个标题!",
});iconstring
""(空字符串)
弹窗显示的图标。 SweetAlert预定义了4个可以使用的内置图标:
"warning""error""success""info"swal("Title", "Text", "success"))。swal({
icon: "success",
});buttonstring boolean ButtonOptions
{
text: "OK",
value: true,
visible: true,
className: "",
closeModal: true,
}默认显示确认按钮。您可以通过将button设置为string来更改其显示的文本,也可以通过传递ButtonOptions对象来调整更多设置。将其设置为false会隐藏按钮。
swal({
button: "Coolio",
});swal({
button: {
text: "Hey ho!",
},
});swal("Hello world!", {
button: false,
});buttonsboolean string[] ButtonOptions[] ButtonList
{
cancel: {
text: "Cancel",
value: null,
visible: false,
className: "",
closeModal: true,
},
confirm: {
text: "OK",
value: true,
visible: true,
className: "",
closeModal: true
}
}指定按钮的确切数量及每个按钮的配置。 如果使用字符串数组,则可以将元素设置为字符串(仅设置显示的文本),ButtonOptions对象可与字符串组合使用。 您还可以将其中一个元素设置为true,以便只获取按钮的默认选项。
如果您需要的不只默认的取消和确认按钮,则需要指定一个ButtonList对象,其中键(按钮的名称)的值为ButtonOptions。
您还可以指定false来隐藏所有按钮(与button参数的效果相同)。
swal({
buttons: ["Stop", "Do it!"],
});swal({
buttons: [true, "Do it!"],
});swal("Hello world!", {
buttons: false,
});swal({
buttons: {
cancel: true,
confirm: true,
},
});swal({
buttons: {
cancel: true,
confirm: "Confirm",
roll: {
text: "Do a barrell roll!",
value: "roll",
},
},
});contentNode string
null
用户自定义内容,可以但不限于修改文本与图标
swal({
content: "input",
});swal({
content: {
element: "input",
attributes: {
placeholder: "Type your password",
type: "password",
},
},
});var slider = document.createElement("input");
slider.type = "range";
swal({
content: slider,
});classNamestring
""(空字符串)
将自定义CLASS添加到SweetAlert元素中。 方便使用CSS样式改变外观。
/* CSS样式 */
.red-bg{
background-color: rgba(255,0,0,0.28);
}//Javascript代码
swal("Hello world!", {
className: "red-bg",
});closeOnClickOutsideboolean
true
通过设置该参数来决定用户是否能够通过点击弹窗外部来关闭弹窗。
swal({
closeOnClickOutside: false,
});closeOnEscboolean
true
通过设置该参数来决定用户是否能够通过按ESC键来关闭弹窗。
swal({
closeOnEsc: false,
});dangerModeboolean
false
如果设置为true,则确认按钮的颜色会变为红色,而默认焦点则会设置在取消按钮上。 当需要确认关键操作(例如删除项目)时,这很方便。
swal("Are you sure?", {
dangerMode: true,
buttons: true,
});timernumber
null
在一定时间后(单位为毫秒)关闭弹窗。 当button参数被设置为false时,这个参数很有用。
swal("This modal will disappear soon!", {
buttons: false,
timer: 3000,
});| 名称 | 描述 | 示例 |
|---|---|---|
| close | 关闭当前打开的SweetAlert弹窗,效果与手动按下取消按钮一样。 | swal.close() |
| getState | 获取当前SweetAlert弹窗的状态。 | swal.getState() |
| setActionValue | 修改当前弹窗按钮的文本。您可以只传一个字符串(默认情况下它会更改确认按钮的文本)或一个对象。 | swal.setActionValue({ confirm: 'Text from input' }) |
| stopLoading | 删除弹窗中所有按钮的加载状态,可将其与button参数中的closeModal:false结合使用。 | swal.stopLoading() |
=page=
通过编辑指定CLASS的CSS样式,可以自定义SweetAlert弹窗不同部位的样式
swal-overlay修改弹窗的遮罩样式
.swal-overlay {
background-color: rgba(43, 165, 137, 0.45);
}swal-modal修改弹窗主界面的样式
.swal-modal {
background-color: rgba(63,255,106,0.69);
border: 3px solid white;
}swal-title修改弹窗标题栏的样式
.swal-modal {
background-color: rgba(63,255,106,0.69);
border: 3px solid white;
}swal-text修改弹窗文本栏的样式
.swal-text {
background-color: #FEFAE3;
padding: 17px;
border: 1px solid #F0E1A1;
display: block;
margin: 22px;
text-align: center;
color: #61534e;
}swal-footer修改弹窗底部栏(按钮区域)的样式
.swal-footer {
background-color: rgb(245, 248, 250);
margin-top: 32px;
border-top: 1px solid #E9EEF1;
overflow: hidden;
}swal-button修改弹窗中所有按钮的样式,弹窗按钮除了都有swal-button类外,不同类型的按钮还有一个额外的类,格式为swal-button-[type]。例如,确认按钮的额外的类是swal-button-confirm,当仅需要自定义某个按钮的样式时,就可以用这个额外类去修改。
/* 修改全部按钮 */
.swal-button {
padding: 7px 19px;
border-radius: 2px;
background-color: #4962B3;
font-size: 12px;
border: 1px solid #3e549a;
text-shadow: 0px -1px 0px rgba(0, 0, 0, 0.3);
}
—— 评论区 ——