【问题标题】:sweetaler2 target opt - swal still outside the targetsweetalert2 target opt - swal 仍然在目标之外
【发布时间】:2020-04-12 17:25:45
【问题描述】:

我在 sweetalert2 中尝试了 target opt,但 toast 仍然出现在 target 之外

我做错了什么?

这是代码

#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>

结果如下:

【问题讨论】:

    标签: javascript css sweetalert sweetalert2


    【解决方案1】:

    使用target 属性,所发生的只是将DOM 容器.swal2-container 附加到目标元素,但它具有position: 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>

    【讨论】:

    • 非常感谢您的快速响应和简单的解释,效果很好:D,再次非常感谢您
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多