搜尋
首頁web前端Bootstrap教程Bootstrap中怎麼使用Toasts元件? (例講解)

Bootstrap中怎麼使用Toasts元件?以下這篇文章跟大家介紹一下Bootstrap5中吐司訊息Toasts元件的用法,希望對大家有幫助!

Bootstrap中怎麼使用Toasts元件? (例講解)

1 吐司訊息(Toasts)範例

#吐司(Toasts)是一種輕量級通知,旨在模仿行動和桌面作業系統已經普及的推播通知。它們是用flexbox構建的,所以它們很容易對齊和定位。 【相關推薦:《bootstrap教學》】

和彈出提示一樣,吐司訊息也需要自己初始化,不知為何官網的初始化方法無效,我在國外網站找到一個可行的方法。

<!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(&#39;.toast&#39;)).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  ></script>
     <script>
      document.querySelector("#liveToastBtn").onclick = function() {
        new bootstrap.Toast(document.querySelector(&#39;.toast&#39;)).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(&#39;#toast1&#39;)).show();
      }
      document.querySelector("#liveToastBtn2").onclick = function() {
        new bootstrap.Toast(document.querySelector(&#39;#toast2&#39;)).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  ></script>
     <script>
      document.querySelector("#liveToastBtn").onclick = function() {
        new bootstrap.Toast(document.querySelector(&#39;.toast&#39;)).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(&#39;.toast&#39;)).show();
      }
   </script>
  </body>
</html>

Bootstrap中怎麼使用Toasts元件? (例講解)

7 設定顯示位置

預設吐司訊息顯示在瀏覽器右下角,根據需求,使用自訂的CSS指定吐司位置。右上角通常用於通知,頂部的中間也是如此。如果您一次只要展示一個吐司,請將定位樣式放在toast上。


  Bootstrap中怎麼使用Toasts元件? (例講解)   Bootstrap   11 mins ago
  Hello, world! This is a toast message.

上面是官方例子,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(&#39;.toast&#39;)).show();
      }
   </script>
  </body>
</html>

Bootstrap中怎麼使用Toasts元件? (例講解)

更多關於bootstrap的相關知識,可在:bootstrap基礎教學! !

以上是Bootstrap中怎麼使用Toasts元件? (例講解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:掘金社区。如有侵權,請聯絡admin@php.cn刪除
React的引導:優勢和最佳實踐React的引導:優勢和最佳實踐Apr 16, 2025 am 12:17 AM

將Bootstrap集成到React項目中的優勢包括:1)快速開發,2)一致性和可維護性,3)響應式設計。通過直接引入CSS文件或使用React-Bootstrap庫,可以在React項目中高效使用Bootstrap的組件和样式。

Bootstrap:網絡框架的快速指南Bootstrap:網絡框架的快速指南Apr 15, 2025 am 12:10 AM

Bootstrap是由Twitter開發的框架,幫助快速搭建響應式、移動優先的網站和應用。 1.易用性和豐富組件庫使開發更快。 2.龐大社區提供支持和解決方案。 3.通過CDN引入並使用類名控製樣式,如創建響應式網格。 4.可自定義樣式和擴展組件。 5.優點包括快速開發和響應式設計,缺點是樣式一致性和學習曲線。

打破bootstrap:是什麼以及為什麼重要打破bootstrap:是什麼以及為什麼重要Apr 14, 2025 am 12:05 AM

Bootstrapisafree,開放式frameworkthatsimplifiesRessiveandMobile-firstwebsitedEvelvelopment.itofferspre-styledComponentsAndAgridSystem,流化inthiningthecreationofaesthethetshethetshetshetshetshetshetshetshetshetshethetshethet interpleaseansing和Runctinctionalwebdesigns。

Bootstrap:使網頁設計更容易Bootstrap:使網頁設計更容易Apr 13, 2025 am 12:10 AM

Bootstrap讓網頁設計更容易的原因是其預設組件、響應式設計和豐富的社區支持。 1)預設組件庫和样式讓開發者無需編寫複雜的CSS代碼;2)內置網格系統簡化了響應式佈局的創建;3)社區支持提供了豐富的資源和解決方案。

Bootstrap的影響:加速網絡開發Bootstrap的影響:加速網絡開發Apr 12, 2025 am 12:05 AM

Bootstrap加速了Web開發,通過提供預定義的樣式和組件,開發者可以快速搭建響應式網站。 1)它縮短了開發時間,例如在項目中幾天內完成基本佈局。 2)通過Sass變量和mixins,Bootstrap允許定製樣式以滿足特定需求。 3)使用CDN版本可以優化性能,提高加載速度。

理解引導:核心概念和功能理解引導:核心概念和功能Apr 11, 2025 am 12:01 AM

Bootstrap是一個開源的前端框架,主要作用是幫助開發者快速構建響應式網站。 1)它提供了預定義的CSS類和JavaScript插件,方便實現複雜的UI效果。 2)Bootstrap的工作原理依賴於其CSS和JavaScript組件,通過媒體查詢實現響應式設計。 3)使用示例包括基本用法,如創建按鈕,以及高級用法,如自定義樣式。 4)常見錯誤包括類名拼寫錯誤和未正確引入文件,建議使用瀏覽器開發者工具調試。 5)性能優化可通過自定義構建工具實現,最佳實踐包括使用語義化HTML和Bootstrap的預定義

Bootstrap Deep Dive:響應式設計和高級佈局技術Bootstrap Deep Dive:響應式設計和高級佈局技術Apr 10, 2025 am 09:35 AM

Bootstrap通過網格系統和媒體查詢實現響應式設計,使網站適應不同設備。 1.使用預定義類(如col-sm-6)定義列寬。 2.網格系統基於12列,需注意總和不超12。3.使用斷點(如sm、md、lg)定義不同屏幕尺寸下的佈局。

Bootstrap面試問題:降落您夢想的前端工作Bootstrap面試問題:降落您夢想的前端工作Apr 09, 2025 am 12:14 AM

Bootstrap是一套開源的前端框架,用於快速開發響應式網站和應用。 1.它提供了響應式設計、一致的UI組件和快速開發的優勢。 2.網格系統使用flexbox佈局,基於12列結構,通過.container、.row和.col-sm-6等類實現。 3.自定義樣式可以通過修改SASS變量或覆蓋CSS實現。 4.常用JavaScript組件包括模態框、輪播圖和折疊。 5.優化性能可以通過只加載必要組件、使用CDN和壓縮合併文件來實現。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具