iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > html >Bootstrap中如何使用Toasts组件
  • 689
分享到

Bootstrap中如何使用Toasts组件

2024-04-02 19:04:59 689人浏览 安东尼
摘要

这篇文章主要介绍Bootstrap中如何使用Toasts组件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1 吐司消息(Toasts)示例吐司(Toasts)是一种轻量级通知,旨在模

这篇文章主要介绍Bootstrap中如何使用Toasts组件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Bootstrap中如何使用Toasts组件

1 吐司消息(Toasts)示例

吐司(Toasts)是一种轻量级通知,旨在模仿移动和桌面操作系统已经普及的推送通知。它们是用flexbox构建的,所以它们很容易对齐和定位。

和弹出提示一样,吐司消息也需要自己初始化,不知为什么官网的初始化方法无效,我在国外网站找到一个可行的方法。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keyWords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.CSS" rel="stylesheet">
    <title>Popovers</title>
  </head>
  <body>
    <div>
        <br><br><br><br>
        <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button>
        <div class="position-fixed bottom-0 end-0 p-3" style="z-index: 5">
          <div id="liveToast" class="toast hide" data-bs-animation="false" role="alert" aria-live="assertive" aria-atomic="true">
            <div>
            <strong>吐司消息提示</strong>
            <small>11 mins aGo</small>
            <button type="button" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
            <div>
            你有一条短消息!
            </div>
          </div>
        </div>

      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
     <script>
      document.querySelector("#liveToastBtn").onclick = function() {
        new bootstrap.Toast(document.querySelector('.toast')).show();
      }
   </script>
  </body>
</html>

Bootstrap中如何使用Toasts组件

2 设置选项

选项可以透过数据属性或是javascript传递。对于数据属性,将选项名称附加到data-bs-,如:data-bs-animation=""。

  • data-bs-animation="true" 在吐司应用CSS fade转换效果

  • data-bs-autohide="true" 自动将吐司隐藏

  • data-bs-delay="5000" ,延迟隐藏吐司5s(默认单位毫秒)

以上值为默认值,如果你对磨人的效果满意,根本不需要写那个,27.3.1例子中,我设置了data-bs-autohide="false"设置不自动将吐司隐藏,这样好方便截图,否则鼠标只要在任何地方一点,消息框就消失了。

3 半透明的

吐司也可以是半透明的,因此能混合在它们可能出现的任何东西上。在支持CSS属性backdrop-filter的浏览器,还会尝试对吐司下方的元素进行模糊效果。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>吐司消息</title>
  </head>
  <body>
    <div>
        <br><br><br><br>
        <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button>
        <div role="alert" aria-live="assertive" aria-atomic="true">
          <div>
          <strong>半透明吐司</strong>
          <small>11 mins ago</small>
          <button type="button" data-bs-dismiss="toast" aria-label="Close"></button>
          </div>
          <div>
            你有一条短消息!
          </div>
          </div>

      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
     <script>
      document.querySelector("#liveToastBtn").onclick = function() {
        new bootstrap.Toast(document.querySelector('.toast')).show();
      }
   </script>
  </body>
</html>

Bootstrap中如何使用Toasts组件

4 堆叠

可以透过将吐司包装于toast-container容器来推叠它们,这将会在垂直方向上增加一些间距。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>吐司消息</title>
  </head>
  <body>
    <div>
        <br><br><br><br>
        <button type="button" class="btn btn-primary" id="liveToastBtn1">显示吐司消息1</button>
        <button type="button" class="btn btn-primary" id="liveToastBtn2">显示吐司消息2</button>
        <div>
          <div id="toast1" role="alert" aria-live="assertive" aria-atomic="true">
          <div>
          <strong>吐司消息</strong>
          <small>刚刚发送</small>
          <button type="button" data-bs-dismiss="toast" aria-label="Close"></button>
          </div>
          <div>
          第一条消息
          </div>
          </div>
          
          <div  id="toast2" role="alert" aria-live="assertive" aria-atomic="true">
          <div>
          <strong>吐司消息</strong>
          <small>2分钟前</small>
          <button type="button" data-bs-dismiss="toast" aria-label="Close"></button>
          </div>
          <div>
            第二条消息
          </div>
          </div>
        </div>

      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
     <script>
       document.querySelector("#liveToastBtn1").onclick = function() {
        new bootstrap.Toast(document.querySelector('#toast1')).show();
      }
      document.querySelector("#liveToastBtn2").onclick = function() {
        new bootstrap.Toast(document.querySelector('#toast2')).show();
      }
   </script>
  </body>
</html>

Bootstrap中如何使用Toasts组件

5 自定义内容

透过移除子元件、调整通用类或是增加标记以自定义吐司。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>吐司消息</title>
  </head>
  <body>
    <div>
        <br><br><br><br>
        <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button>
        <div role="alert" aria-live="assertive" aria-atomic="true">
            <div>
            邀请你穿越到三国!
            <div class="mt-2 pt-2 border-top">
            <button type="button" class="btn btn-primary btn-sm">接受邀请</button>
            <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="toast">关闭</button>
            </div>
            </div>
         </div>

      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
     <script>
      document.querySelector("#liveToastBtn").onclick = function() {
        new bootstrap.Toast(document.querySelector('.toast')).show();
      }
   </script>
  </body>
</html>

Bootstrap中如何使用Toasts组件

6 配色方案

基于以上的示例,您也可以透过我们的颜色通用类别建立不同的吐司配色方案。以下我们将bg-danger与text-white添加到toast,再把text-white添加到关闭按钮上。为了让边缘清晰显示,透过border-0移除了预设的边框。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>吐司消息</title>
  </head>
  <body>
    <div>
        <br><br><br><br>
        <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button>

        <div class="toast align-items-center text-white bg-danger border-0" role="alert" aria-live="assertive" aria-atomic="true">
            <div>
            <div>
            这里是红色背景的
            </div>
            <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
        </div>

      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
     <script>
      document.querySelector("#liveToastBtn").onclick = function() {
        new bootstrap.Toast(document.querySelector('.toast')).show();
      }
   </script>
  </body>
</html>

Bootstrap中如何使用Toasts组件

7 设置显示位置

默认吐司消息显示在浏览器右下角,根据需求,使用自定义的CSS指定吐司位置。右上角通常用于通知,顶部的中间也是如此。如果您一次只要展示一个吐司,请将定位样式放在toast上。

<fORM>
<div class="mb-3">
<label for="selectToastPlacement">Toast placement</label>
<select class="form-select mt-2" id="selectToastPlacement">
<option value="" selected>Select a position...</option>
<option value="top-0 start-0">Top left</option>
<option value="top-0 start-50 translate-middle-x">Top center</option>
<option value="top-0 end-0">Top right</option>
<option value="top-50 start-0 translate-middle-y">Middle left</option>
<option value="top-50 start-50 translate-middle">Middle center</option>
<option value="top-50 end-0 translate-middle-y">Middle right</option>
<option value="bottom-0 start-0">Bottom left</option>
<option value="bottom-0 start-50 translate-middle-x">Bottom center</option>
<option value="bottom-0 end-0">Bottom right</option>
</select>
</div>
</form>
<div aria-live="polite" aria-atomic="true" class="bg-dark position-relative bd-example-toasts">
<div class="toast-container position-absolute p-3" id="toastPlacement">
<div class="toast">
<div class="toast-header">
  <img src="..." class="rounded me-2" alt="...">
  <strong class="me-auto">Bootstrap</strong>
  <small>11 mins ago</small>
</div>
<div class="toast-body">
  Hello, world! This is a toast message.
</div>
</div>
</div>
</div>

上面是官方例子,Bootstrap5 Toasts我也没找到其中驱动的js代码。不过可以给大家参考一下,有兴趣的可以去研究一下,在这里我根据上面的代码,修改了个显示在左上角的。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>吐司消息</title>
  </head>
  <body>
    <div>
        <br><br><br><br>
        <button type="button" class="btn btn-primary" id="liveToastBtn">显示吐司消息</button>
        <div class="position-fixed top-0 start-0 p-3" style="z-index: 5">
          <div id="liveToast" class="toast hide" data-bs-animation="false" role="alert" aria-live="assertive" aria-atomic="true">
            <div>
            <strong>吐司消息提示</strong>
            <small>11 mins ago</small>
            <button type="button" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
            <div>
            你有一条短消息!
            </div>
          </div>
        </div>

      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
     <script>
      document.querySelector("#liveToastBtn").onclick = function() {
        new bootstrap.Toast(document.querySelector('.toast')).show();
      }
   </script>
  </body>
</html>

Bootstrap中如何使用Toasts组件

以上是“Bootstrap中如何使用Toasts组件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网html频道!

--结束END--

本文标题: Bootstrap中如何使用Toasts组件

本文链接: https://www.lsjlt.com/news/87478.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Bootstrap中如何使用Toasts组件
    这篇文章主要介绍Bootstrap中如何使用Toasts组件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1 吐司消息(Toasts)示例吐司(Toasts)是一种轻量级通知,旨在模...
    99+
    2024-04-02
  • bootstrap组件中如何使用导航组件
    这篇文章主要介绍了bootstrap组件中如何使用导航组件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Bootstrap 中的导航组件都依...
    99+
    2024-04-02
  • Bootstrap中Blazor组件如何使用
    这篇文章给大家介绍Bootstrap中Blazor组件如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1、安装模板dotnet new -i Bootstrap.Blazor.T...
    99+
    2024-04-02
  • Bootstrap中如何使用Card卡片组件
    本篇内容介绍了“Bootstrap中如何使用Card卡片组件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!...
    99+
    2024-04-02
  • Bootstrap中如何使用徽章图标组件
    本篇内容主要讲解“Bootstrap中如何使用徽章图标组件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Bootstrap中如何使用徽章图标组件”吧!1 徽章(...
    99+
    2024-04-02
  • Bootstrap中列表组组件的使用示例
    小编给大家分享一下Bootstrap中列表组组件的使用示例,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!列表组可以用来制作列表清单、垂直导航等效果,也可以配合其他的组件制作出更漂亮的组件,列表组在bootstrap框架中也...
    99+
    2023-06-06
  • ng-bootstrap组件集中tabset组件如何实现
    这篇文章给大家分享的是有关ng-bootstrap组件集中tabset组件如何实现的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。ng-bootstrap: tabset ...
    99+
    2024-04-02
  • Bootstrap中如何导航组件和选项卡组件
    本篇内容介绍了“Bootstrap中如何导航组件和选项卡组件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!...
    99+
    2024-04-02
  • Bootstrap中分页组件的使用方法
    这篇文章主要介绍Bootstrap中分页组件的使用方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!概述在Bootstrap框架中提供了两种分页导航:☑   带页码的分页导航☑   带翻页的分页导...
    99+
    2023-06-14
  • Bootstrap按钮组件怎么使用
    这篇文章主要介绍“Bootstrap按钮组件怎么使用”,在日常操作中,相信很多人在Bootstrap按钮组件怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Bootstrap按钮组件怎么使用”的疑惑有所...
    99+
    2023-07-04
  • bootstrap中select插件如何封装成Vue2.0组件
    这篇文章主要介绍bootstrap中select插件如何封装成Vue2.0组件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!因为bootstrap-select功能比较强大,而且样式...
    99+
    2024-04-02
  • bootstrap中select插件如何封装成Vue组件
    本篇内容主要讲解“bootstrap中select插件如何封装成Vue组件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“bootstrap中select插件如何封装成Vue组件”吧!因为boot...
    99+
    2023-07-04
  • Bootstrap学习之如何使用缩略图组件和警示框组件
    这篇“Bootstrap学习之如何使用缩略图组件和警示框组件”除了程序员外大部分人都不太理解,今天小编为了让大家更加理解“Bootstrap学习之如何使用缩略图组件和警示框组件”,给大家总结了以下内容,具有一定借鉴价值,内容详细步骤清晰,细...
    99+
    2023-06-06
  • Bootstrap中如何配置使用时间日历插件bootstrap-datetimepicker
    这篇文章将为大家详细讲解有关Bootstrap中如何配置使用时间日历插件bootstrap-datetimepicker,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1....
    99+
    2024-04-02
  • 如何使用JS组件Bootstrap实现弹出框效果
    本篇内容主要讲解“如何使用JS组件Bootstrap实现弹出框效果”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用JS组件Bootstrap实现弹出框效果”吧!插件依赖弹出框依赖工具提示插...
    99+
    2023-07-04
  • bootstrap分页插件如何使用
    要使用Bootstrap分页插件,您需要遵循以下步骤:1. 引入Bootstrap的CSS和JavaScript文件。在您的HTML...
    99+
    2023-08-24
    bootstrap
  • Bootstrap中Blazor组件有什么用
    这篇文章主要介绍Bootstrap中Blazor组件有什么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Bootstrap是什么Bootstrap是目前最受欢迎的前端框架,它是基于 HTML、CSS、JAVASCR...
    99+
    2023-06-09
  • 如何使用Bootstrap输入框、导航、分页等常用组件
    这篇文章主要介绍如何使用Bootstrap输入框、导航、分页等常用组件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Bootstrap输入框和导航组件一.下拉菜单下拉菜单,就是点击一...
    99+
    2024-04-02
  • 如何使用JS组件Bootstrap实现下拉菜单效果
    这篇“如何使用JS组件Bootstrap实现下拉菜单效果”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“如何使用JS组件Boo...
    99+
    2023-07-04
  • Bootstrap中如何使用Tree View
    这篇文章主要介绍Bootstrap中如何使用Tree View,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!A simple and elegant solution to disp...
    99+
    2024-04-02
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作