iT邦幫忙

2023 iThome 鐵人賽

DAY 26
0

今天開始講元件了,bootstrap提供很多實用的元件,基本上是複製官方文件的原始碼,搭配通用類別與helpers就行調整,我們可以輕鬆快速客製化它們變成自己想要的樣子~
所以,為了能夠輕鬆修改元件,我們必須要去了解它們的運作方式,所以接下來就來逐個講解元件吧!

手風琴 (Accordion)

基本使用

bootstrap的手風琴可以透過點擊達到垂直收合展開的效果,這個元件會用到bootstrap的javascript,所以記得要載入。先看此範例程式碼:

<div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        Accordion Item #1
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingTwo">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        Accordion Item #2
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingThree">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        Accordion Item #3
      </button>
    </h2>
    <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>
  1. 首先手風琴的最外層是.accordion,然後它有一個id,這是給裡面item設定data-bs-target="父層id"用的。
  2. 每個.accordion-item都有.accordion-header,那是用來放子項目的標題的。
  3. .accordion-header裡面有按鈕 .accordion-button,它使用data-bs-toggle="collapse" data-bs-target="子層accordion-collapse的id"來控制accordion的每個子項目的收合。
  4. 再來是那些被展開收合的內容,.accordion-collapse,它們也會有自己的id用來控制收合(對應第3. 的 data-bs-target)。此外對應剛剛第1.,它會添加data-bs-target="父層id",以確保一次只有一項是展開的。
  5. .accordion-collapse上面添加.show會使初始時是展開的;如果是添加.collapse則該項初始時是收合的。

效果如下圖:
https://ithelp.ithome.com.tw/upload/images/20231003/20135414ipCxzP8aAD.png

flush (外框和圓角)

.accordion加上.accordion-flush 來移除預設 background-color、 外框和圓角使該手風琴能和父容器緊鄰。如圖:
https://ithelp.ithome.com.tw/upload/images/20231003/20135414euGK9HLPz3.png

可以同時打開多項(always open)

如果在每個.accordion-collapse移除掉(不放)data-bs-parent屬性的話,那它就不再一次只能有一個項目展開了。

小練習: 此CodePen有兩個小地方需要填空,可以試著完成它


上一篇
Bootstrap 表單驗證(Validation)
下一篇
Bootstrap 元件| 警報 (Alerts)
系列文
前端超實用框架: Bootstrap 入門到實戰教學 31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言