简体   繁体   English

Bootstrap4 初始化 Toast

[英]Bootstrap4 Initializing Toasts

I am working with toasts in Bootstrap 4 and there is one things that's bothering me:我正在使用 Bootstrap 4 中的 toasts,有一件事情困扰着我:

If I initialize the toast如果我初始化吐司

<div class="toast my-3" data-delay="2500">
    <div class="toast-header text-dark">
        //fill this later
    </div>
    <div class="toast-body text-dark">
        //fill this later
    </div>
</div>

like in the documentation就像在文档中一样

$(document).ready(function () {
  $(".toast").toast("show");
});

the toast is show as expected, when the page loads.当页面加载时,toast 会按预期显示。 If I don't initialize the toast, the html code takes some place.如果我不初始化 toast,html 代码就会出现。 at the bottom of my page.在我的页面底部。

How can I make the toast not to be shown when loading the page and not taking some place due to html code?由于 html 代码,如何使吐司在加载页面时不显示并且不占据某个位置? I tried initialize them with toast("hide") or toast("dispose") as well.我也尝试用 toast("hide") 或 toast("dispose") 初始化它们。

Thanks谢谢

When you write this:当你写这个:

$(document).ready(function () {
  $(".toast").toast("show");
});

It means you're expecting the toast to show as soon as the DOM is loaded.这意味着您希望吐司在 DOM 加载后立即显示。 If you want the toast to appear conditionnally, you have for example to create a button in your html file (click on "Run snippet" and click on the button).如果您希望 toast 有条件地出现,例如,您必须在 html 文件中创建一个按钮(单击“运行片段”并单击按钮)。

 $(document).ready(function () { $('#toastThis').on( "click", function() { $(".toast").toast("show"); }); });
 .toast { position: absolute; top: 0; right: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/> <button type="button" id="toastThis" class="btn btn-primary">Primary</button> <div class="toast" role="alert" aria-live="assertive" aria-atomic="true"> <div class="toast-header"> <img src="..." class="rounded mr-2" alt="..."> <strong class="mr-auto">Bootstrap</strong> <small>11 mins ago</small> <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="toast-body"> Hello, world. This is a toast message. </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM