javascript - 如何让 Bootstrap Toasts 不自动消失?

标签 javascript html twitter-bootstrap bootstrap-4 toast

我有一个 Bootstrap 5 网站和一个在访问者离线时显示的 Toasts。

如果我离线,Toasts 会出现并自动消失。我希望它一直保留到访问者单击十字架或恢复连接为止。我试图在我的代码中添加 data-autohide="false" 但它不起作用。如何确保 Toasts 不会在没有操作的情况下自动关闭?

如果我测试实时版本,Toasts 会完全消失:

https://getbootstrap.com/docs/5.0/components/toasts/#live

索引.html :

<!doctype html>
<html lang="fr" class="h-100">

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>

  <body class="position-relative d-flex flex-column bg-dark text-white text-center" data-bs-spy="scroll" data-target="#navbar" data-bs-offset="85" tabindex="0">

    <div class="toast-container position-fixed bottom-0 start-50 translate-middle-x mb-3">
      <div id="updateToast" class="toast d-none" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
          <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-arrow-repeat text-success" viewBox="0 0 16 16">
            <path d="M11.534 7h3.932a.25.25 0 0 1 .192.41l-1.966 2.36a.25.25 0 0 1-.384 0l-1.966-2.36a.25.25 0 0 1 .192-.41zm-11 2h3.932a.25.25 0 0 0 .192-.41L2.692 6.23a.25.25 0 0 0-.384 0L.342 8.59A.25.25 0 0 0 .534 9z"/>
            <path fill-rule="evenodd" d="M8 3c-1.552 0-2.94.707-3.857 1.818a.5.5 0 1 1-.771-.636A6.002 6.002 0 0 1 13.917 7H12.9A5.002 5.002 0 0 0 8 3zM3.1 9a5.002 5.002 0 0 0 8.757 2.182.5.5 0 1 1 .771.636A6.002 6.002 0 0 1 2.083 9H3.1z"/>
          </svg>
          <strong class="me-auto">Mise à jour disponible</strong>
        </div>
        <div class="toast-body text-start text-dark">
          Cliquez ICI pour mettre à jour.
        </div>
      </div>
      <div id="offlineToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
          <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-wifi-off text-danger" viewBox="0 0 16 16">
            <path d="M10.706 3.294A12.545 12.545 0 0 0 8 3C5.259 3 2.723 3.882.663 5.379a.485.485 0 0 0-.048.736.518.518 0 0 0 .668.05A11.448 11.448 0 0 1 8 4c.63 0 1.249.05 1.852.148l.854-.854zM8 6c-1.905 0-3.68.56-5.166 1.526a.48.48 0 0 0-.063.745.525.525 0 0 0 .652.065 8.448 8.448 0 0 1 3.51-1.27L8 6zm2.596 1.404.785-.785c.63.24 1.227.545 1.785.907a.482.482 0 0 1 .063.745.525.525 0 0 1-.652.065 8.462 8.462 0 0 0-1.98-.932zM8 10l.933-.933a6.455 6.455 0 0 1 2.013.637c.285.145.326.524.1.75l-.015.015a.532.532 0 0 1-.611.09A5.478 5.478 0 0 0 8 10zm4.905-4.905.747-.747c.59.3 1.153.645 1.685 1.03a.485.485 0 0 1 .047.737.518.518 0 0 1-.668.05 11.493 11.493 0 0 0-1.811-1.07zM9.02 11.78c.238.14.236.464.04.66l-.707.706a.5.5 0 0 1-.707 0l-.707-.707c-.195-.195-.197-.518.04-.66A1.99 1.99 0 0 1 8 11.5c.374 0 .723.102 1.021.28zm4.355-9.905a.53.53 0 0 1 .75.75l-10.75 10.75a.53.53 0 0 1-.75-.75l10.75-10.75z"/>
          </svg>
          <strong class="me-auto">Vous êtes hors-ligne</strong>
          <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
        <div class="toast-body text-start text-dark">
          Les informations de cette page peuvent être obsolètes.
        </div>
      </div>
    </div>

    <script src="bootstrap.bundle.min.js"></script>
    <script src="toasts.js"></script>

  </body>

</html>

toast .js:

window.addEventListener('offline', () => {
  var toastElList = [].slice.call(document.querySelectorAll('#offlineToast'))
  var toastList = toastElList.map(function(toastEl) {
    return new bootstrap.Toast(toastEl)
  });
  toastList.forEach(toast => toast.show());
});

window.addEventListener('online', () => {
  var toastElList = [].slice.call(document.querySelectorAll('#offlineToast'))
  var toastList = toastElList.map(function(toastEl) {
    return new bootstrap.Toast(toastEl)
  });
  toastList.forEach(toast => toast.hide());
});

最佳答案

在 bootstrap 5 中,自动隐藏数据属性是 data-bs-autohide="false"

关于javascript - 如何让 Bootstrap Toasts 不自动消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67033758/

相关文章:

javascript - 连接一段时间后 Redis ETIMEDOUT 错误

javascript - 如何在 javascript 中重新创建 pmouseY 和 pmouseX (processing.JS 变量)

javascript - AngularJS 将字符串数组转换为 float 组

css - twitter-bootstrap 将搜索图像对齐到输入字段旁边

html - Bootstrap 4 : text-center & aligned left?

javascript - 从 Autodesk Forge 3D 查看器下载文件

javascript - 根据页面加载时的下拉值显示文本框

javascript - 如何让 <select> 框从包含的 <div> 中溢出?

html - div 中的图像在图像下方有额外的空间

jquery - Blueimp 图片库 : More than one Gallery getting Collapsed