iT邦幫忙

2023 iThome 鐵人賽

DAY 22
0
  • 這篇主要是把Bootstrap裡面的Dropdwons(下拉式選單)樣式做個簡易的整理。下拉式選單式需要用到js的所以記得要把bootstrap的javaScript cdn也一起引用近來。
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ"
        crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
  • 單一按鈕

    • 任何單個 .btn 可以變成帶有一些標記的下拉式切換。以下是您如何使用 <button> 元素:
    <div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
        Dropdown button
      </button>
      <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
      </ul>
    </div>
    
    • 可以加上.btn-primart、.btn-warning、.btn-danger已更改顏色,如下圖。

https://ithelp.ithome.com.tw/upload/images/20230914/201621701kwa49973w.png

  • 分割按鈕
    • 透過與單個按鈕下拉選單幾乎相同的標記,創建分割按鈕下拉選單,但是添加了 .dropdown-toggle-split 以在下拉插入符號做適當間隔,以便達成只有右邊的按鈕可以顯示下拉式選單。
<div class="btn-group">
        <button type="button" class="btn btn-danger">Action</button>
        <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown"
            aria-expanded="false">
            <span class="visually-hidden">Toggle Dropdown</span>
        </button>
        <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Action</a></li>
            <li><a class="dropdown-item" href="#">Another action</a></li>
            <li><a class="dropdown-item" href="#">Something else here</a></li>
            <li>
                <hr class="dropdown-divider">
            </li>
            <li><a class="dropdown-item" href="#">Separated link</a></li>
        </ul>
    </div>

https://ithelp.ithome.com.tw/upload/images/20230914/20162170dRYwffE8cU.png

  • 尺寸
    <div class="btn-group">
        <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown"
            aria-expanded="false">
            Small button
        </button>
        <ul class="dropdown-menu">
            ...
        </ul>
    </div>
    <button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-bs-toggle="dropdown"
        aria-expanded="false">
        Large button
    </button>
    <ul class="dropdown-menu">
        ...
    </ul>

https://ithelp.ithome.com.tw/upload/images/20230914/20162170YkIvIIm0kA.png

  • 深色背景
<ul class="dropdown-menu dropdown-menu-dark">
        ...
    </ul>

https://ithelp.ithome.com.tw/upload/images/20230914/20162170FGcCLFXrXK.png

  • 把以上的功能做個整合:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDarkDropdown" aria-controls="navbarNavDarkDropdown" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavDarkDropdown">
      <ul class="navbar-nav">
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown
          </a>
          <ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink">
            <li><a class="dropdown-item" href="#">Action</a></li>
            <li><a class="dropdown-item" href="#">Another action</a></li>
            <li><a class="dropdown-item" href="#">Something else here</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>

https://ithelp.ithome.com.tw/upload/images/20230914/20162170A5H7PIpmlg.png

  • 想讓選單變成往上延伸的話,再btn-group的class那邊加上dropup,但須注意必須要有空間才會往上。右邊是.dropend、左邊是.dropstart、
    https://ithelp.ithome.com.tw/upload/images/20230914/20162170Vo6Kg1ctQA.png
<div class="btn-group dropup" style="top:500px";>
        <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown"
            aria-expanded="false">
            Small button
        </button>
        <ul class="dropdown-menu">
            ...
        </ul>
    </div>
  • 有一個蠻常用的功能disable,li 的class 輸入disable即可。實際應用上可以與JS做搭配,點選過後就關閉按鈕。
<div class="dropdown">
        <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-bs-toggle="dropdown"
            aria-expanded="false">
            Dropdown
        </button>
        <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Regular link</a></li>
            <li><a class="dropdown-item disabled">Disabled link</a></li>
            <li><a class="dropdown-item" href="#">Another link</a></li>
        </ul>
    </div>

https://ithelp.ithome.com.tw/upload/images/20230914/20162170AJO7MbQKbV.png

  • 對齊,.dropdown-menu-end:
  • https://ithelp.ithome.com.tw/upload/images/20230914/20162170TW1CieZz8S.png
  • 標頭,選單的head:
<li><h6 class="dropdown-header">Dropdown header</h6></li>

https://ithelp.ithome.com.tw/upload/images/20230914/201621704ZqbaxTaqP.png

以上即是bootstrap裡面的下拉式選單,我認為比較常用的幾個功能。


上一篇
Day_21 用JavaScript做一個小遊戲
下一篇
Day_23 篡改猴 Tampermonkey 基礎用法教學
系列文
三十天持續努力挑戰py30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言