
- Bootstrap 教程
- Bootstrap - 首頁
- Bootstrap - 概述
- Bootstrap - 環境設定
- Bootstrap - 右到左 (RTL)
- Bootstrap - CSS 變數
- Bootstrap - 顏色模式
- Bootstrap 佈局
- Bootstrap - 斷點
- Bootstrap - 容器
- Bootstrap - 網格系統
- Bootstrap - 列
- Bootstrap - 間距
- Bootstrap - 實用工具類
- Bootstrap - CSS 網格
- Bootstrap 元件
- Bootstrap - 手風琴
- Bootstrap - 警示框
- Bootstrap - 徽章
- Bootstrap - 麵包屑
- Bootstrap - 按鈕
- Bootstrap - 按鈕組
- Bootstrap - 卡片
- Bootstrap - 走馬燈
- Bootstrap - 關閉按鈕
- Bootstrap - 摺疊
- Bootstrap - 下拉選單
- Bootstrap - 列表組
- Bootstrap - 模態框
- Bootstrap - 導航欄
- Bootstrap - 導航 & 標籤頁
- Bootstrap - 側邊欄
- Bootstrap - 分頁
- Bootstrap - 佔位符
- Bootstrap - 氣泡提示
- Bootstrap - 進度條
- Bootstrap - 滾動監聽
- Bootstrap - 載入動畫
- Bootstrap - 提示框
- Bootstrap - 工具提示
- Bootstrap 表單
- Bootstrap - 表單
- Bootstrap - 表單控制元件
- Bootstrap - 選擇框
- Bootstrap - 複選框 & 單選按鈕
- Bootstrap - 範圍選擇器
- Bootstrap - 輸入組
- Bootstrap - 浮動標籤
- Bootstrap - 佈局
- Bootstrap - 表單驗證
- Bootstrap 輔助類
- Bootstrap - 清除浮動
- Bootstrap - 顏色 & 背景
- Bootstrap - 彩色連結
- Bootstrap - 焦點環
- Bootstrap - 圖示連結
- Bootstrap - 定位
- Bootstrap - 比例
- Bootstrap - 堆疊
- Bootstrap - 拉伸連結
- Bootstrap - 文字截斷
- Bootstrap - 垂直線
- Bootstrap - 視覺隱藏
- Bootstrap 實用工具類
- Bootstrap - 背景
- Bootstrap - 邊框
- Bootstrap - 顏色
- Bootstrap - 顯示
- Bootstrap - Flex 佈局
- Bootstrap - 浮動
- Bootstrap - 互動
- Bootstrap - 連結
- Bootstrap - 物件適應
- Bootstrap - 透明度
- Bootstrap - 溢位
- Bootstrap - 定位
- Bootstrap - 陰影
- Bootstrap - 尺寸
- Bootstrap - 間距
- Bootstrap - 文字
- Bootstrap - 垂直對齊
- Bootstrap - 可見性
- Bootstrap 演示
- Bootstrap - 網格演示
- Bootstrap - 按鈕演示
- Bootstrap - 導航演示
- Bootstrap - 部落格演示
- Bootstrap - 滑塊演示
- Bootstrap - 走馬燈演示
- Bootstrap - 頁首演示
- Bootstrap - 頁尾演示
- Bootstrap - 英雄圖演示
- Bootstrap - 特色演示
- Bootstrap - 側邊欄演示
- Bootstrap - 下拉選單演示
- Bootstrap - 列表組演示
- Bootstrap - 模態框演示
- Bootstrap - 徽章演示
- Bootstrap - 麵包屑演示
- Bootstrap - Jumbotrons 演示
- Bootstrap-粘性頁尾演示
- Bootstrap-相簿演示
- Bootstrap-登入演示
- Bootstrap-定價演示
- Bootstrap-結賬演示
- Bootstrap-產品演示
- Bootstrap-封面演示
- Bootstrap-儀表盤演示
- Bootstrap-粘性頁尾導航欄演示
- Bootstrap-砌體佈局演示
- Bootstrap-入門模板演示
- Bootstrap-RTL 相簿演示
- Bootstrap-RTL 結賬演示
- Bootstrap-RTL 走馬燈演示
- Bootstrap-RTL 部落格演示
- Bootstrap-RTL 儀表盤演示
- Bootstrap 有用資源
- Bootstrap - 問答
- Bootstrap - 快速指南
- Bootstrap - 有用資源
- Bootstrap - 討論
Bootstrap - 側邊欄
本章討論側邊欄元件。Bootstrap 中的側邊欄元件是一個功能,允許您建立一個滑動面板或側邊欄,可以在視口的側面顯示或隱藏。
它通常用於建立導航選單、內容面板或其他可以暫時顯示而不會佔用整個螢幕空間的附加資訊。
概述
以下是 Bootstrap 側邊欄元件的關鍵特性
啟用:側邊欄元件通常由一個切換按鈕或連結啟用,該按鈕或連結觸發側邊欄面板的開啟和關閉。這可以使用 JavaScript 或資料屬性來實現。
位置:根據您的設計要求,側邊欄面板可以放置在視口的左側、右側、頂部或底部。
內容:您可以在側邊欄面板中放置任何 HTML 內容,包括導航選單、文字、影像、表單或其他元件。
輔助功能:Bootstrap 確保側邊欄元件可供螢幕閱讀器和鍵盤導航訪問,使其符合輔助功能標準。
響應式行為:側邊欄元件預設情況下是響應式的,可以適應不同的螢幕尺寸。在較小的螢幕上,可以將其設定為全屏或允許在側邊欄面板內滾動。
事件:Bootstrap 提供 JavaScript 事件,可用於自定義側邊欄元件的行為,例如在面板開啟或關閉時執行程式碼。
它簡化了滑動面板的建立,並有助於改善移動裝置上的使用者體驗,提供了一種簡潔高效的方式來呈現附加內容或導航選項,而不會使主螢幕混亂。
注意:一次只能顯示一個側邊欄,類似於模態框。
側邊欄元件
以下元件共同作用以在 Bootstrap 中建立側邊欄功能,允許您建立滑動面板,當被切換按鈕或連結觸發時,顯示附加內容或導航選項
切換按鈕或觸發元素
側邊欄面板容器
側邊欄面板
側邊欄位置
關閉按鈕
Javascript 和資料屬性
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <h4 class="text-success text-start">Offcanvas component</h4> <div class="container"> <button class="btn btn-success" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvas"> Open offcanvas </button> </div> <div class="offcanvas offcanvas-end" id="offcanvas"> <div class="offcanvas-header"> <h5 class="offcanvas-title"> Offcanvas title </h5> <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body"> <p>This is an offcanvas component of Bootstrap, which is similar to a modal in functionality.</p> </div> </div> </div> </body> </html>
線上演示
側邊欄元件可以根據以下條件顯示或隱藏
.offcanvas(預設) - 隱藏內容
.offcanvas.show - 顯示內容
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <h4 class="text-success text-start">Offcanvas component live demo</h4> <button class="btn btn-primary" type="button" id="trigger-btn" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample" aria-controls="offcanvasExample"> Button click </button> <div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas Title</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body"> <div> The Offcanvas component provides a convenient way to create responsive and mobile-friendly layouts. When triggered, the offcanvas panel slides into view from either the left or right side of the screen, depending on the placement chosen. It overlays the main content, pushing it aside, and provides a smooth transition effect. </div> </div> </div> </div> <script> //New instance of the collapse element is created var offcanvasElement = document.getElementById("offcanvasExample"); var offcanvas = new bootstrap.Offcanvas(offcanvasElement); let button = document.getElementById("trigger-btn"); button.addEventListener("click", () => { return offcanvas.show(); ; }); </script> </body> </html>
主體滾動
當側邊欄及其背景可見時,將禁止<body>元素的滾動。要為<body>元素啟用滾動,您可以使用data-bs-scroll屬性。
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="offcanvas offcanvas-start" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="sidebar" aria-labelledby="offcanvasStartLabel"> <div class="offcanvas-header"> <h1 id="offcanvasStartLabel">Offcanvas</h1> <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <hr> <div class="offcanvas-body"> <h5>Enable body Scrolling</h5> <h5><p>Bootstrap provides features to scroll the page when offcanvas and the backdrop are visible.</p></h5> </div> </div> <div class="container mt-3"> <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#sidebar" aria-controls="offcanvasStart">Offcanvas Scrolling Enable</button> <center> <img src="/bootstrap/images/profile.jpg" alt="GFG" width="600" height="400"> <h3>Example for body scrolling in offcanvas component</h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <img src="/bootstrap/images/profile.jpg" alt="GFG" width="600" height="400"> </center> </div> </body> </html>
主體滾動和背景
您可以啟用具有可見背景的<body>滾動。
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="offcanvasWithBothOptions" aria-labelledby="offcanvasWithBothOptionsLabel"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="offcanvasWithBothOptionsLabel">Enable backdrop with scrolling</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <hr> <div class="offcanvas-body"> <h5><p>Bootstrap provides features to scroll the page and the backdrop are visible.</p></h5> <img src="/bootstrap/images/profile.jpg" alt="GFG" width="200" height="200"> <h3>Example for body scrolling in offcanvas component</h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <img src="/bootstrap/images/profile.jpg" alt="GFG" width="200" height="200"> </div> </div> <div class="container mt-3"> <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasWithBothOptions" aria-controls="offcanvasStart">Enabled scrolling with backdrop</button> <center> <img src="/bootstrap/images/tutimg.png" alt="GFG" width="600" height="400"> <h3>Example for body scrolling in offcanvas component</h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <img src="/bootstrap/images/tutimg.png" alt="GFG" width="600" height="400"> </center> </div> </body> </html>
靜態背景
當背景設定為靜態時,點選側邊欄外部不會關閉側邊欄元件。
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#staticBackdrop" aria-controls="staticBackdrop"> Static Offcanvas </button> <div class="offcanvas offcanvas-start" data-bs-backdrop="static" tabindex="-1" id="staticBackdrop" aria-labelledby="staticBackdropLabel"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="staticBackdropLabel">Offcanvas</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body"> <div> The offcanvas component will not get closed when you click outside it. </div> </div> </div> <center> <img src="/bootstrap/images/tutimg.png" alt="GFG" width="600" height="400"> <h3>Example for body scrolling in offcanvas component</h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <h4> A website for all the tech-savvy people. </h4> <h3> Welcome to tutorials point Welcome to tutorials point </h3> <img src="/bootstrap/images/tutimg.png" alt="GFG" width="600" height="400"> </center> </div> </body> </html>
深色側邊欄
由於顏色模式的實現,在 v5.3.0 中已棄用使用元件的深色變體。建議不要手動包含上述類,而是將data-bs-theme="dark"屬性應用於根元素、父包裝器或元件本身。
響應式
響應式的側邊欄類會在指定的斷點及以下隱藏視口之外的內容。另一方面,在該斷點以上,內容行為正常。例如,使用.offcanvas-lg類,在lg斷點以下的內容在側邊欄中隱藏,而在該斷點以上則可見。
注意:您需要調整瀏覽器大小才能檢視側邊欄的響應式行為。
讓我們來看一個例子
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <h4 class="text-success text-start">Responsive offcanvas</h4> <div class="container"> <button class="btn btn-success d-lg-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasResponsive" aria-controls="offcanvasResponsive">Show offcanvas</button> <div class="alert alert-warning d-none d-lg-block">Resize your screen / viewport to show the responsive offcanvas.</div> <div class="offcanvas-lg offcanvas-end" tabindex="-1" id="offcanvasResponsive" aria-labelledby="offcanvasResponsiveLabel"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="offcanvasResponsiveLabel">Responsive offcanvas</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#offcanvasResponsive" aria-label="Close"></button> </div> <div class="offcanvas-body"> <p class="mb-0">This content can be verified when the screen size is below the breakpoint lg (.offcanvas-lg).</p> </div> </div> </div> </div> </body> </html>
每個斷點的響應式側邊欄類都是可訪問的。
.offcanvas
.offcanvas-sm
.offcanvas-md
.offcanvas-lg
.offcanvas-xl
.offcanvas-xxl
位置
您必須新增一個修飾符類來為側邊欄元件新增位置,因為它沒有預設位置。
以下是可用的位置選項
.offcanvas-start - 將側邊欄放置在視口的左側。
.offcanvas-end - 將側邊欄放置在視口的右側。
.offcanvas-top - 將側邊欄放置在視口的頂部。
.offcanvas-bottom - 將側邊欄放置在視口的底部。
讓我們來看一個頂部位置的示例:
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <h4 class="text-success text-start">Offcanvas placement - Top</h4> <p>Resize the viewport size to see the offcanvas</p> <div class="container"> <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasTop" aria-controls="offcanvasTop">Top offcanvas</button> <div class="offcanvas offcanvas-top" tabindex="-1" id="offcanvasTop" aria-labelledby="offcanvasTopLabel"> <div class="offcanvas-header text-bg-primary"> <h5 class="offcanvas-title" id="offcanvasTopLabel">Top offcanvas</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body bg-primary-subtle"> <p>This is an example for an offcanvas whose placement is at the top of the viewport.</p> </div> </div> </div> </div> </body> </html>
讓我們來看一個右側位置的示例
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <h4 class="text-success text-start">Offcanvas placement - Right</h4> <div class="container"> <button class="btn btn-success" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight" aria-controls="offcanvasRight">Right offcanvas</button> <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight" aria-labelledby="offcanvasRightLabel"> <div class="offcanvas-header text-bg-success"> <h5 class="offcanvas-title" id="offcanvasRightLabel">Right offcanvas</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body bg-success-subtle"> <p>This is an example for an offcanvas whose placement is at the right of the viewport.</p> </div> </div> </div> </div> </body> </html>
讓我們來看一個底部位置的示例
示例
您可以使用編輯和執行選項編輯並嘗試執行此程式碼。
<!DOCTYPE html> <html> <head> <title>Bootstrap - Offcanvas</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <h5 class="text-dark text-start">Offcanvas placement - Bottom</h5> <p>Resize the viewport size to see the offcanvas</p> <div class="container"> <button class="btn btn-danger" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasBottom" aria-controls="offcanvasBottom">Bottom offcanvas</button> <div class="offcanvas offcanvas-bottom" data-bs-scroll="true" tabindex="-1" id="offcanvasBottom" aria-labelledby="offcanvasBottomLabel"> <div class="offcanvas-header text-bg-danger"> <h5 class="offcanvas-title" id="offcanvasBottomLabel">Bottom offcanvas</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body bg-danger-subtle"> <p>This is an example for an offcanvas whose placement is at the bottom of the viewport.</p> </div> </div> </div> </div> </body> </html>
輔助功能
確保在.offcanvas中包含aria-labelledby="...",引用側邊欄標題,因為側邊欄面板在概念上類似於模態對話方塊。值得注意的是,您不必新增role="dialog",因為它會透過 JavaScript 自動新增。