添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

JavaScript 的摺疊插件被用於顯示和隱藏內容。按鈕、錨點被用作為觸發器,對應到需要被切換的元素上。折疊一個元素會將它的 height 從當前的值轉換為 0 。基於 CSS 處理動畫的方式,你不能在帶有 .collapse 的元素上使用 padding ; 相反的,應該把它作為獨立的包裝元素。

該元件的動畫效果取決於 prefers-reduced-motion media queries。請參考 reduced motion section of our accessibility documentation

點擊以下按鈕,透過改變 Class 來顯示及隱藏另一個元素:

  • .collapse 隱藏內容。
  • .collapsing 會在轉換的過程中被套用。
  • .collapse.show 顯示內容。
  • 通常,我們建議使用帶有 data-bs-target 屬性的按鈕。 從語義的角度來看,雖然不建議這樣做,但您也可以使用帶有 href 屬性(和 role ="button" )的連結。 在這兩種情況下,都需要 data-bs-toggle="collapse"

    Link with href Button with data-bs-target Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger. < a class = "btn btn-primary" data-bs-toggle = "collapse" href = "#collapseExample" role = "button" aria-expanded = "false" aria-controls = "collapseExample" > Link with href < button class = "btn btn-primary" type = "button" data-bs-toggle = "collapse" data-bs-target = "#collapseExample" aria-expanded = "false" aria-controls = "collapseExample" > Button with data-bs-target </ button > < div class = "collapse" id = "collapseExample" > < div class = "card card-body" > Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger. </ div > </ div >

    <button> <a> 標籤可以用來顯示與隱藏元素,需要在它們的 data-bs-target 或是 href 屬性內部引用選擇器。同一個元素可以被多個 <button> <a> 控制以顯示或隱藏,只要它們都有透過 data-bs-target 或是 href 對其進行引用。

    < a class = "btn btn-primary" data-bs-toggle = "collapse" href = "#multiCollapseExample1" role = "button" aria-expanded = "false" aria-controls = "multiCollapseExample1" > Toggle first element </ a > < button class = "btn btn-primary" type = "button" data-bs-toggle = "collapse" data-bs-target = "#multiCollapseExample2" aria-expanded = "false" aria-controls = "multiCollapseExample2" > Toggle second element </ button > < button class = "btn btn-primary" type = "button" data-bs-toggle = "collapse" data-bs-target = ".multi-collapse" aria-expanded = "false" aria-controls = "multiCollapseExample1 multiCollapseExample2" > Toggle both elements </ button > < div class = "row" > < div class = "col" > < div class = "collapse multi-collapse" id = "multiCollapseExample1" > < div class = "card card-body" > Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger. </ div > </ div > </ div > < div class = "col" > < div class = "collapse multi-collapse" id = "multiCollapseExample2" > < div class = "card card-body" > Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger. </ div > </ div > </ div > </ div >

    確保有在控制元素中添加 aria-expanded 。該屬性明確地向螢幕閱讀器、類似的輔助技術之控制元件傳達可折疊元素的當前狀態。如果可折疊元素預設為關閉,則控制項元素上的屬性值應為 aria-expanded="false" ; 如果您使用 show 將可折疊元素設置為預設打開,則應在控制項上設置 aria-expanded="true" 。插件將根據可折疊元素目前的狀態 (開啟與否) 自動切換控制項上的這個屬性 (透過 JavaScript,或是當使用者觸發了綁定到相同折疊元素的另一個控制項元素)。如果控制的 HTML 元素不是按鈕 (例如: <a> <div> ),那麼 role="button" 屬性應該被加到元素中。

    如果你的控制元素鎖定了一個單一的可折疊元素 (即 data-bs-target 屬性指向的是 id 選擇器),則應該在控制元素上添加 aria-controls 屬性,且內部包含了可折疊元素之 id 。現代的螢幕閱讀器、類似的輔助技術都利用此屬性為用戶提供額外的快捷方式以直接導航到可折疊元素。

    請注意,Bootstrap 當前的折疊應用未涵蓋 WAI-ARIA Authoring Practices 1.1 accordion pattern 所描述的多種 選擇性的 鍵盤操作,你需要自訂 JavaScript 來實現這些內容。

    Variables

    $transition-collapse:         height .35s ease;
    

    Classes

    Collapse transition classes can be found in scss/_transitions.scss as these are shared across multiple components (collapse and accordion).

    .collapse {
      &:not(.show) {
        display: none;
    .collapsing {
      height: 0;
      overflow: hidden;
      @include transition($transition-collapse);
    

    用法 {usage}

    折疊插件使用一些 Class 來處理這些繁雜工作。

  • .collapse 隱藏內容。
  • .collapse.show 顯示內容。
  • .collapsing 會在轉換開始時被加入,並在結束的時候被移除。
  • 這些類別可以在 _transitions.scss 中被找到.

    透過資料屬性

    只需將 data-bs-toggle="collapse" 以及 data-bs-target 加入元素即可自動指定控制一個或多個可折疊元素。data-bs-target 屬性接受 CSS 選擇器以套用摺疊。請確保有在可折疊元素上添加 collapse 類別。如果希望它預設是打開的,加上額外的類別 .show

    如果要在一個可折疊區域加入可折疊群組,加入 data-bs-parent="#selector" 資料屬性。可以參考範例中的效果。

    透過 JavaScript

    透過以下語法手動啟用:

    var collapseElementList = [].slice.call(document.querySelectorAll('.collapse'))
    var collapseList = collapseElementList.map(function (collapseEl) {
      return new bootstrap.Collapse(collapseEl)
    

    選項可以透過資料屬性或是 JavaScript 加入。對於資料屬性,將選項名稱加入 data-bs-, 如 data-bs-parent=""

    Default Description selector | jQuery object | DOM element false 如果加入了 parent,則當可折疊物件為顯示時,指定父項下的所有可折疊元素將被關閉。(類似于傳統的折疊控制項行為 - 這取決於 card)。該屬性必須在目標可折疊區域上設置。 toggle boolean 切換可折疊元素

    Asynchronous methods and transitions

    All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.

    See our JavaScript documentation for more information.

    將內容啟用為可摺疊元素。接受一個選擇性的選項 object

    可以透過建構函式建立一個摺疊範例,舉例來說:

    var myCollapse = document.getElementById('myCollapse')
    var bsCollapse = new bootstrap.Collapse(myCollapse, {
      toggle: false
          Method
          Description
          toggle
          將一個可折疊元素切換為顯示或隱藏。在可折疊元素實際顯示或隱藏之前返回給調用者。 (即在 shown.bs.collapsehidden.bs.collapse 事件發生之前)。
          顯示一個可折疊元素。在可折疊元素實際顯示之前返回給調用者。 (例如, shown.bs.collapse 事件發生前). 
          隱藏一個可折疊元素。在可折疊元素實際被隱藏之前返回給調用者。 (例如, 在 hidden.bs.collapse 事件發生之前)。
          dispose
          銷毀一個元素的折疊。 (移除 DOM 元素上儲存的資料)
          getInstance
          允許你獲取與 DOM 元素關聯之折疊範例的靜態方法。
    

    Bootstrap 提供一些事件給予折疊使用。

    Event type Description
    var myCollapsible = document.getElementById('myCollapsible')
    myCollapsible.addEventListener('hidden.bs.collapse', function () {
      // do something...
              
  • Designed and built with all the love in the world by the Bootstrap team with the help of our contributors.
  • Code licensed MIT, docs CC BY 3.0.
  • Currently v5.0.0.
  • Links
  • Examples
  • Themes
  • Guides
  • Getting started
  • Starter template
  • Webpack
  • Parcel
  • Projects
  • Bootstrap 5
  • Bootstrap 4
  • Icons
  • npm starter
  • Community
  • Issues
  • Discussions
  • Corporate sponsors
  • Open Collective
  • Slack
  • Stack Overflow
  •