javascript sweetaler 2 target opt - swal仍然在目标之外

jfewjypa  于 2024-01-05  发布在  Java
关注(0)|答案(2)|浏览(157)

我在sweetalert2中尝试了目标选项,但吐司仍然出现在目标之外
我做错了什么?
这里的代码

  1. #trial {
  2. position: absolute;
  3. display: block;
  4. height:200px;
  5. width: 500px;
  6. border: 1px solid;
  7. }
  8. <div id="trial"></div>
  9. <script>
  10. $(document).ready(function() {
  11. var id = document.getElementById('trial');
  12. Swal.fire({
  13. title: 'I will be placed inside the #swal2-container',
  14. toast: true,
  15. target: id,
  16. position: 'bottom-left'
  17. });
  18. });
  19. </script>

字符串
结果如下:


的数据

cuxqih21

cuxqih211#

使用target属性,所发生的只是DOM容器.swal2-container被追加到目标元素,但它具有position: fixed;样式,您需要如下所示覆盖它:

  1. $(document).ready(function() {
  2. var id = document.getElementById('trial');
  3. Swal.fire({
  4. title: 'I will be placed inside the #swal2-container',
  5. toast: true,
  6. target: id,
  7. position: 'bottom-left'
  8. });
  9. });
  1. #trial {
  2. position: relative;
  3. left: 50px;
  4. display: block;
  5. height: 200px;
  6. width: 500px;
  7. border: 1px solid;
  8. }
  9. .swal2-container{
  10. position: absolute !important;
  11. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
  3. <div id="trial"></div>
展开查看全部
bgtovc5b

bgtovc5b2#

SweetAlert2有一个customClass属性,你可以用它来添加自定义类,这包括到container。所以你可以只创建一个专用的类,并为你的特定吐司添加它(或者如果你使用Bootstrap 5,你甚至可以使用position-absolute)。

  1. $(document).ready(function() {
  2. var id = document.getElementById('trial');
  3. Swal.fire({
  4. title: 'I will be placed inside the #swal2-container',
  5. toast: true,
  6. target: id,
  7. position: 'bottom-left',
  8. customClass: {
  9. 'container': 'trial-swal-container'
  10. }
  11. });
  12. });
  1. #trial {
  2. position: absolute;
  3. display: block;
  4. height: 200px;
  5. width: 500px;
  6. border: 1px solid;
  7. }
  8. .trial-swal-container {
  9. position: absolute !important;
  10. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
  3. <div id="trial"></div>
展开查看全部

相关问题