我在sweetalert2中尝试了目标选项,但吐司仍然出现在目标之外
我做错了什么?
这里的代码
#trial {
position: absolute;
display: block;
height:200px;
width: 500px;
border: 1px solid;
}
<div id="trial"></div>
<script>
$(document).ready(function() {
var id = document.getElementById('trial');
Swal.fire({
title: 'I will be placed inside the #swal2-container',
toast: true,
target: id,
position: 'bottom-left'
});
});
</script>
字符串
结果如下:
的数据
2条答案
按热度按时间cuxqih211#
使用
target
属性,所发生的只是DOM容器.swal2-container
被追加到目标元素,但它具有position: fixed;
样式,您需要如下所示覆盖它:bgtovc5b2#
SweetAlert2有一个
customClass
属性,你可以用它来添加自定义类,这包括到container
。所以你可以只创建一个专用的类,并为你的特定吐司添加它(或者如果你使用Bootstrap 5,你甚至可以使用position-absolute
)。