我嘗試了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>
結果如下:
有了target屬性,發生的只是DOM container .swal2-container追加到target元素,但是它有一個位置:fixed樣式,您需要覆蓋它,如下所示:
$(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'
});
});
#trial {
position: relative;
left: 50px;
display: block;
height: 200px;
width: 500px;
border: 1px solid;
}
.swal2-container{
position: absolute !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<div id="trial"></div>