Bootstrap中怎麼使用Toasts元件?以下這篇文章跟大家介紹一下Bootstrap5中吐司訊息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('.toast')).show(); } </script> </body> </html>
選項可以透過資料屬性或是JavaScript傳遞。對於資料屬性,將選項名稱附加到data-bs-,如:data-bs-animation=""。
以上值為預設值,如果你對磨人的效果滿意,根本不需要寫那個,27.3.1範例中,我設定了data-bs-autohide="false"設定不自動將吐司隱藏,這樣好方便截圖,否則滑鼠只要在任何地方一點,訊息框就消失了。
吐司也可以是半透明的,因此能混合在它們可能出現的任何東西上。在支援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>
可以透過將吐司包裝於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>
透過移除子元件、調整通用類別或增加標記以自訂吐司。
<!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>
基於以上的範例,您也可以透過我們的顏色通用類別建立不同的吐司配色方案。以下我們將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>
預設吐司訊息顯示在瀏覽器右下角,根據需求,使用自訂的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的相關知識,可在:bootstrap基礎教學! !
以上是Bootstrap中怎麼使用Toasts元件? (例講解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!