javascript - Bootstrap4 初始化 Toast
问题描述
我正在使用 Bootstrap 4 中的 toasts,有一件事情困扰着我:
如果我初始化吐司
<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>
就像在文档中一样
$(document).ready(function () {
$(".toast").toast("show");
});
当页面加载时,toast 会按预期显示。如果我不初始化 toast,则 html 代码会占据一些位置。在我的页面底部。
如何在加载页面时不显示吐司,并且由于 html 代码而没有占据一些位置?我也尝试用 toast("hide") 或 toast("dispose") 初始化它们。
谢谢
解决方案
当你写这个:
$(document).ready(function () {
$(".toast").toast("show");
});
这意味着您希望吐司在 DOM 加载后立即显示。如果您希望 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">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
推荐阅读
- python - 如何使用 flow_from_directory 仅选择特定的文件格式?
- python - 将网页截图附加到所有“then”的诱惑步骤(pytest-bdd、selenium、allure)
- excel - MS Excel“if else if”条件
- java - 使用 Jasypt 在 AWS Elastic Beanstalk 上为 Spring Boot App 传递环境变量
- javascript - React UseEffect - 在 React 状态下设置单个表行
- java - 启动 Spring Boot 2.1.1 和 Java 11 应用程序时出现 java.lang.IncompatibleClassChangeError
- java - 当“任务”在日志消息中时,log4j 不记录?
- google-chrome-extension - 后台脚本中的 Chrome 扩展跨域请求被阻止
- python - 如何在 Python 中确定变量是 int 还是 list for 循环
- javascript - 之前创建的空数组中的 Puh 数组