gpt4 book ai didi

javascript - 将 sweetalert2 与 bootstrap 4 结合使用

转载 作者:行者123 更新时间:2023-11-29 14:38:27 24 4
gpt4 key购买 nike

我正在使用 sweetAlert2,我正在尝试使用 bootstrap 4 来设置按钮样式,设置属性:

buttonsStyling: false,
confirmButtonClass: 'btn btn-primary btn-lg',
cancelButtonClass: 'btn btn-lg'

它有效,但是当我在上面设置这些属性时,showLoaderOnConfirm 选项以非常难看的样式显示。

您可以查看以下示例:

重现步骤:

  • 输入一个有效的邮箱;
  • 提交
  • 查看第一个加载器(样式)(使用 bs4,第二个使用 swal2 的默认样式)。

$(function() {
$('#button').click(() => {
swal({
title: 'Submit email to run ajax request',
input: 'email',
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
buttonsStyling: false,
confirmButtonClass: 'btn btn-primary btn-lg',
cancelButtonClass: 'btn btn-lg',
preConfirm: function(email) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (email === 'taken@example.com') {
reject('This email is already taken.')
} else {
resolve()
}
}, 2000)
})
},
allowOutsideClick: false
}).then(function(email) {
swal({
type: 'success',
title: 'Ajax request finished!',
html: 'Submitted email: ' + email
})
}).catch(swal.noop)
});

$('#button1').click(() => {
swal({
title: 'Submit email to run ajax request',
input: 'email',
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
preConfirm: function(email) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (email === 'taken@example.com') {
reject('This email is already taken.')
} else {
resolve()
}
}, 2000)
})
},
allowOutsideClick: false
}).then(function(email) {
swal({
type: 'success',
title: 'Ajax request finished!',
html: 'Submitted email: ' + email
})
}).catch(swal.noop)
});
});
<!DOCTYPE html>
<html>

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.4.2/sweetalert2.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.4.2/sweetalert2.min.css" />
</head>

<body>
<button id="button">Show (Bootstrap)</button>
<hr />
<button id="button1">Show (w/o bootstrap)</button>
</body>

</html>

问题是:如何让加载程序使用默认样式(使用 bs4)?或者可以自定义 showLoaderOnConfirm 选项的样式...

最佳答案

来自官网的现场演示:https://sweetalert2.github.io/recipe-gallery/bootstrap.html

Swal.fire({
title: 'SweetAlert2 + Bootstrap 4',
input: 'text',
buttonsStyling: false,
showCancelButton: true,
customClass: {
confirmButton: 'btn btn-primary btn-lg',
cancelButton: 'btn btn-danger btn-lg',
loader: 'custom-loader'
},
loaderHtml: '<div class="spinner-border text-primary"></div>',
preConfirm: () => {
Swal.showLoading()
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, 5000)
})
}
})
.btn {
margin: 0 6px;
}

.custom-loader {
animation: none !important;
border-width: 0 !important;
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> 
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.css">

关于javascript - 将 sweetalert2 与 bootstrap 4 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42287895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com