Hla mus rau cov ntsiab lus tseem ceeb Hla mus rau docs navigation
Check
in English

Dropdowns

Toggle contextual overlays rau tso saib cov npe ntawm kev sib txuas thiab ntau ntxiv nrog Bootstrap dropdown plugin.

Txheej txheem cej luam

Dropdowns yog toggleable, contextual overlays rau tso saib cov npe ntawm kev sib txuas thiab ntau dua. Lawv tau ua kev sib tham nrog cov suav nrog Bootstrap dropdown JavaScript plugin. Lawv nyob nraum toggled los ntawm txhaj, tsis yog los ntawm hovering; qhov no yog kev txiav txim siab tsim qauv .

Dropdowns yog tsim los ntawm lub tsev qiv ntawv thib peb, Popper , uas muab qhov chaw nyob thiab pom qhov chaw pom. Nco ntsoov suav nrog popper.min.js ua ntej Bootstrap's JavaScript lossis siv bootstrap.bundle.min.js/ bootstrap.bundle.jsuas muaj Popper. Popper tsis yog siv los ua qhov kev poob qis hauv navbars txawm tias tsis tas yuav tsum muaj qhov chaw nyob.

Kev siv tau

Tus qauv WAI ARIA txhais cov role="menu"widget tiag tiag , tab sis qhov no yog qhov tshwj xeeb rau daim ntawv thov-zoo li cov ntawv qhia zaub mov uas ua rau ua haujlwm lossis ua haujlwm. ARIA cov ntawv qhia zaub mov tsuas yog muaj cov ntawv qhia zaub mov, cov ntawv qhia zaub mov checkbox, xov tooj cua khawm cov ntawv qhia zaub mov, pawg xov tooj cua khawm, thiab sub-menus.

Bootstrap's dropdowns, ntawm qhov tod tes, yog tsim los ua generic thiab siv tau rau ntau yam xwm txheej thiab cov qauv cim. Piv txwv li, nws muaj peev xwm tsim cov dropdowns uas muaj cov khoom siv ntxiv thiab cov ntawv tswj hwm, xws li kev tshawb nrhiav teb lossis cov ntawv nkag. Vim li no, Bootstrap tsis cia siab tias (lossis cia li ntxiv) ib qho rolethiab aria-cov yam ntxwv xav tau rau cov ntawv qhia ARIA tseeb . Cov kws sau ntawv yuav tsum suav nrog cov cwj pwm tshwj xeeb no lawv tus kheej.

Txawm li cas los xij, Bootstrap ntxiv kev txhawb nqa rau feem ntau cov txheej txheem cov ntawv qhia zaub mov sib cuam tshuam, xws li muaj peev xwm txav mus los ntawm tus kheej .dropdown-itemcov ntsiab lus siv tus cursor yuam sij thiab kaw cov ntawv qhia zaub mov nrog tus ESCyuam sij.

Piv txwv

Qhwv lub dropdown's toggle (koj lub pob lossis qhov txuas) thiab cov ntawv qhia zaub mov hauv qab .dropdown, lossis lwm lub ntsiab lus uas tshaj tawm position: relative;. Dropdowns tuaj yeem tsim los ntawm <a>lossis <button>cov ntsiab lus kom haum rau koj cov kev xav tau. Cov piv txwv qhia ntawm no siv cov ntsiab lus semantic <ul>qhov tsim nyog, tab sis kev cai cim tau txais kev txhawb nqa.

Ib lub pob

Ib qho .btntwg tuaj yeem hloov mus rau hauv qhov kev hloov pauv nrog qee qhov kev hloov pauv. Nov yog qhov koj tuaj yeem tso lawv mus ua haujlwm nrog ob lub <button>ntsiab lus:

html
<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown button
  </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>
  </ul>
</div>

Thiab nrog <a>cov ntsiab lus:

html
<div class="dropdown">
  <a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown link
  </a>

  <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>
  </ul>
</div>

Qhov zoo tshaj plaws yog koj tuaj yeem ua qhov no nrog txhua lub pob variant, ib yam nkaus:

<!-- Example single danger button -->
<div class="btn-group">
  <button type="button" class="btn btn-danger dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Action
  </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>

Split khawm

Ib yam li ntawd, tsim phua khawm dropdowns nrog zoo tib yam markup raws li ib tug khawm dropdowns, tab sis nrog rau qhov sib ntxiv ntawm .dropdown-toggle-splitrau kom qhov chaw nyob ib ncig ntawm lub dropdown caret.

Peb siv cov chav kawm ntxiv no kom txo tau cov kab rov tav paddingntawm ob sab ntawm cov cart los ntawm 25% thiab tshem tawm cov margin-leftuas tau ntxiv rau cov pob tsis tu ncua. Cov kev hloov pauv ntxiv no ua rau qhov chaw zov me nyuam nyob rau hauv qhov sib cais khawm thiab muab qhov chaw sib tsoo ntau dua qhov tsim nyog ntawm ib sab ntawm lub pob tseem ceeb.

<!-- Example split danger button -->
<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>

Qhov loj me

Khawm dropdowns ua haujlwm nrog cov nyees khawm ntawm txhua qhov ntau thiab tsawg, suav nrog lub neej ntawd thiab faib cov nyees khawm poob.

<!-- Large button groups (default and split) -->
<div class="btn-group">
  <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>
</div>
<div class="btn-group">
  <button class="btn btn-secondary btn-lg" type="button">
    Large split button
  </button>
  <button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
    <span class="visually-hidden">Toggle Dropdown</span>
  </button>
  <ul class="dropdown-menu">
    ...
  </ul>
</div>
<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>
<div class="btn-group">
  <button class="btn btn-secondary btn-sm" type="button">
    Small split button
  </button>
  <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
    <span class="visually-hidden">Toggle Dropdown</span>
  </button>
  <ul class="dropdown-menu">
    ...
  </ul>
</div>

Tsaus dropdowns

Xaiv rau hauv qhov tsaus nti dropdowns kom phim qhov tsaus navbar lossis kev cai style los ntawm kev ntxiv .dropdown-menu-darkrau qhov uas twb muaj lawm .dropdown-menu. Tsis tas yuav hloov pauv rau cov khoom poob.

html
<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown button
  </button>
  <ul class="dropdown-menu dropdown-menu-dark">
    <li><a class="dropdown-item active" 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>

Thiab muab tso rau siv nyob rau hauv ib tug navbar:

html
<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="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown
          </a>
          <ul class="dropdown-menu dropdown-menu-dark">
            <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>

Cov lus qhia

RTL

Cov lus qhia yog mirrored thaum siv Bootstrap hauv RTL, lub ntsiab lus .dropstartyuav tshwm sim ntawm sab xis.

Nruab nrab

Ua kom cov ntawv qhia dropdown nyob nruab nrab hauv qab lub toggle nrog .dropdown-centerrau ntawm niam txiv lub caij.

html
<div class="dropdown-center">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Centered dropdown
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Action two</a></li>
    <li><a class="dropdown-item" href="#">Action three</a></li>
  </ul>
</div>

Dropup

Trigger dropdown menus saum cov ntsiab lus los ntawm kev ntxiv .dropuprau niam txiv lub caij.

<!-- Default dropup button -->
<div class="btn-group dropup">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Dropup
  </button>
  <ul class="dropdown-menu">
    <!-- Dropdown menu links -->
  </ul>
</div>

<!-- Split dropup button -->
<div class="btn-group dropup">
  <button type="button" class="btn btn-secondary">
    Split dropup
  </button>
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
    <span class="visually-hidden">Toggle Dropdown</span>
  </button>
  <ul class="dropdown-menu">
    <!-- Dropdown menu links -->
  </ul>
</div>

Dropup centered

Ua kom cov ntawv qhia dropup nyob nruab nrab ntawm lub toggle nrog .dropup-centerrau ntawm niam txiv lub caij.

html
<div class="dropup-center dropup">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Centered dropup
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Action two</a></li>
    <li><a class="dropdown-item" href="#">Action three</a></li>
  </ul>
</div>

Droppend

Trigger dropdown menus ntawm sab xis ntawm cov ntsiab lus los ntawm kev ntxiv .dropendrau cov niam txiv caij.

<!-- Default dropend button -->
<div class="btn-group dropend">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Dropend
  </button>
  <ul class="dropdown-menu">
    <!-- Dropdown menu links -->
  </ul>
</div>

<!-- Split dropend button -->
<div class="btn-group dropend">
  <button type="button" class="btn btn-secondary">
    Split dropend
  </button>
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
    <span class="visually-hidden">Toggle Dropend</span>
  </button>
  <ul class="dropdown-menu">
    <!-- Dropdown menu links -->
  </ul>
</div>

Dropstart

Trigger dropdown menus nyob rau sab laug ntawm cov ntsiab lus los ntawm kev ntxiv .dropstartrau cov niam txiv caij.

<!-- Default dropstart button -->
<div class="btn-group dropstart">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Dropstart
  </button>
  <ul class="dropdown-menu">
    <!-- Dropdown menu links -->
  </ul>
</div>

<!-- Split dropstart button -->
<div class="btn-group dropstart">
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
    <span class="visually-hidden">Toggle Dropstart</span>
  </button>
  <ul class="dropdown-menu">
    <!-- Dropdown menu links -->
  </ul>
  <button type="button" class="btn btn-secondary">
    Split dropstart
  </button>
</div>

Koj tuaj yeem siv <a>lossis <button>cov khoom ua cov khoom poob.

html
<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown
  </button>
  <ul class="dropdown-menu">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
</div>

Koj tseem tuaj yeem tsim cov khoom tsis sib tham sib cuam tshuam nrog .dropdown-item-text. Xav tias dawb rau style ntxiv nrog kev cai CSS lossis cov ntawv siv hluav taws xob.

html
<ul class="dropdown-menu">
  <li><span class="dropdown-item-text">Dropdown item text</span></li>
  <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>

Active

Ntxiv .activerau cov khoom hauv dropdown kom style lawv li nquag . Txhawm rau nthuav tawm lub xeev nquag mus rau kev pabcuam thev naus laus zis, siv tus aria-currentcwj pwm - siv tus pagenqi rau nplooj ntawv tam sim no, lossis truerau cov khoom tam sim no hauv ib txheej.

html
<ul class="dropdown-menu">
  <li><a class="dropdown-item" href="#">Regular link</a></li>
  <li><a class="dropdown-item active" href="#" aria-current="true">Active link</a></li>
  <li><a class="dropdown-item" href="#">Another link</a></li>
</ul>

Neeg tsis taus

Ntxiv .disabledrau cov khoom hauv dropdown kom style lawv li neeg tsis taus .

html
<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>

Los ntawm lub neej ntawd, cov ntawv qhia zaub mov poob yog cia li tso 100% los ntawm sab saum toj thiab sab laug ntawm nws niam nws txiv. Koj tuaj yeem hloov qhov no nrog cov chav qhia .drop*qhia, tab sis koj tuaj yeem tswj tau lawv nrog cov chav kawm hloov kho ntxiv.

Ntxiv .dropdown-menu-endrau .dropdown-menutxoj cai dlhos cov ntawv qhia zaub mov. Cov lus qhia yog mirrored thaum siv Bootstrap hauv RTL, lub ntsiab lus .dropdown-menu-endyuav tshwm nyob rau sab laug.

Tau taub hau! Dropdowns yog positioned ua tsaug rau Popper tshwj tsis yog thaum lawv muaj nyob rau hauv ib tug navbar.
html
<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Right-aligned menu example
  </button>
  <ul class="dropdown-menu dropdown-menu-end">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
</div>

Responsive alignment

Yog tias koj xav siv cov kev sib raug zoo, lov tes taw dynamic positioning los ntawm kev ntxiv tus data-bs-display="static"cwj pwm thiab siv cov chav kawm hloov pauv hloov.

Txhawm rau kho txoj cai ntawm cov ntawv qhia zaub mov nrog rau qhov muab faib lossis loj dua, ntxiv .dropdown-menu{-sm|-md|-lg|-xl|-xxl}-end.

html
<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
    Left-aligned but right aligned when large screen
  </button>
  <ul class="dropdown-menu dropdown-menu-lg-end">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
</div>

Txhawm rau ua kom sab laug ntawm cov ntawv qhia zaub mov nrog rau qhov muab faib lossis loj dua, ntxiv .dropdown-menu-endthiab .dropdown-menu{-sm|-md|-lg|-xl|-xxl}-start.

html
<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
    Right-aligned but left aligned when large screen
  </button>
  <ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
</div>

Nco ntsoov tias koj tsis tas yuav ntxiv tus data-bs-display="static"cwj pwm rau cov nyees khawm dropdown hauv navbars, vim Popper tsis siv hauv navbars.

Alignment xaiv

Noj feem ntau ntawm cov kev xaiv uas tau qhia saum toj no, ntawm no yog lub chav ua noj me me dab dej demo ntawm ntau yam kev xaiv kev sib dhos hauv ib qho chaw.

html
<div class="btn-group">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Right-aligned menu
  </button>
  <ul class="dropdown-menu dropdown-menu-end">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
    Left-aligned, right-aligned lg
  </button>
  <ul class="dropdown-menu dropdown-menu-lg-end">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
    Right-aligned, left-aligned lg
  </button>
  <ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group dropstart">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Dropstart
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group dropend">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Dropend
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group dropup">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
    Dropup
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

Headers

Ntxiv ib lub header rau daim ntawv lo rau ntu ntawm kev ua hauv ib qho ntawv qhia zaub mov.

html
<ul class="dropdown-menu">
  <li><h6 class="dropdown-header">Dropdown header</h6></li>
  <li><a class="dropdown-item" href="#">Action</a></li>
  <li><a class="dropdown-item" href="#">Another action</a></li>
</ul>

Sib faib

Cais cov pab pawg ntawm cov khoom noj uas muaj feem xyuam nrog lub divider.

html
<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>

Ntawv nyeem

Muab cov ntawv sau ntawv pub dawb rau hauv cov ntawv qhia zaub mov nrog cov ntawv thiab siv cov khoom siv sib nrug . Nco ntsoov tias koj yuav xav tau cov qauv sizing ntxiv kom txwv cov ntawv qhia zaub mov dav.

html
<div class="dropdown-menu p-4 text-muted" style="max-width: 200px;">
  <p>
    Some example text that's free-flowing within the dropdown menu.
  </p>
  <p class="mb-0">
    And this is more example text.
  </p>
</div>

Cov ntawv

Muab ib daim ntawv tso rau hauv cov ntawv qhia zaub mov, los yog ua rau hauv cov ntawv qhia zaub mov, thiab siv cov paj los yog cov khoom siv padding kom muab qhov chaw tsis zoo uas koj xav tau.

html
<div class="dropdown-menu">
  <form class="px-4 py-3">
    <div class="mb-3">
      <label for="exampleDropdownFormEmail1" class="form-label">Email address</label>
      <input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="[email protected]">
    </div>
    <div class="mb-3">
      <label for="exampleDropdownFormPassword1" class="form-label">Password</label>
      <input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
    </div>
    <div class="mb-3">
      <div class="form-check">
        <input type="checkbox" class="form-check-input" id="dropdownCheck">
        <label class="form-check-label" for="dropdownCheck">
          Remember me
        </label>
      </div>
    </div>
    <button type="submit" class="btn btn-primary">Sign in</button>
  </form>
  <div class="dropdown-divider"></div>
  <a class="dropdown-item" href="#">New around here? Sign up</a>
  <a class="dropdown-item" href="#">Forgot password?</a>
</div>
html
<div class="dropdown">
  <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">
    Dropdown form
  </button>
  <form class="dropdown-menu p-4">
    <div class="mb-3">
      <label for="exampleDropdownFormEmail2" class="form-label">Email address</label>
      <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="[email protected]">
    </div>
    <div class="mb-3">
      <label for="exampleDropdownFormPassword2" class="form-label">Password</label>
      <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
    </div>
    <div class="mb-3">
      <div class="form-check">
        <input type="checkbox" class="form-check-input" id="dropdownCheck2">
        <label class="form-check-label" for="dropdownCheck2">
          Remember me
        </label>
      </div>
    </div>
    <button type="submit" class="btn btn-primary">Sign in</button>
  </form>
</div>

Siv data-bs-offsetlos yog data-bs-referencehloov qhov chaw ntawm lub dropdown.

html
<div class="d-flex">
  <div class="dropdown me-1">
    <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" data-bs-offset="10,20">
      Offset
    </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>
    </ul>
  </div>
  <div class="btn-group">
    <button type="button" class="btn btn-secondary">Reference</button>
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false" data-bs-reference="parent">
      <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>
</div>

Nws pib kaw tus cwj pwm

Los ntawm lub neej ntawd, cov ntawv qhia zaub mov raug kaw thaum nias sab hauv lossis sab nraud ntawm cov ntawv qhia zaub mov. Koj tuaj yeem siv qhov autoClosekev xaiv los hloov tus cwj pwm ntawm qhov kev poob qis.

html
<div class="btn-group">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="true" aria-expanded="false">
    Default dropdown
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="inside" aria-expanded="false">
    Clickable outside
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
    Clickable inside
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

<div class="btn-group">
  <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" data-bs-auto-close="false" aria-expanded="false">
    Manual close
  </button>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
    <li><a class="dropdown-item" href="#">Menu item</a></li>
  </ul>
</div>

CSS

Hloov pauv

Ntxiv hauv v5.2.0

Raws li ib feem ntawm Bootstrap qhov kev hloov pauv CSS hloov pauv mus kom ze, cov kev poob qis tam sim no siv cov CSS hauv zos .dropdown-menurau kev hloov kho lub sijhawm tiag tiag. Cov txiaj ntsig rau CSS qhov sib txawv tau teeb tsa ntawm Sass, yog li Sass customization tseem txhawb nqa, ib yam nkaus.

  --#{$prefix}dropdown-zindex: #{$zindex-dropdown};
  --#{$prefix}dropdown-min-width: #{$dropdown-min-width};
  --#{$prefix}dropdown-padding-x: #{$dropdown-padding-x};
  --#{$prefix}dropdown-padding-y: #{$dropdown-padding-y};
  --#{$prefix}dropdown-spacer: #{$dropdown-spacer};
  @include rfs($dropdown-font-size, --#{$prefix}dropdown-font-size);
  --#{$prefix}dropdown-color: #{$dropdown-color};
  --#{$prefix}dropdown-bg: #{$dropdown-bg};
  --#{$prefix}dropdown-border-color: #{$dropdown-border-color};
  --#{$prefix}dropdown-border-radius: #{$dropdown-border-radius};
  --#{$prefix}dropdown-border-width: #{$dropdown-border-width};
  --#{$prefix}dropdown-inner-border-radius: #{$dropdown-inner-border-radius};
  --#{$prefix}dropdown-divider-bg: #{$dropdown-divider-bg};
  --#{$prefix}dropdown-divider-margin-y: #{$dropdown-divider-margin-y};
  --#{$prefix}dropdown-box-shadow: #{$dropdown-box-shadow};
  --#{$prefix}dropdown-link-color: #{$dropdown-link-color};
  --#{$prefix}dropdown-link-hover-color: #{$dropdown-link-hover-color};
  --#{$prefix}dropdown-link-hover-bg: #{$dropdown-link-hover-bg};
  --#{$prefix}dropdown-link-active-color: #{$dropdown-link-active-color};
  --#{$prefix}dropdown-link-active-bg: #{$dropdown-link-active-bg};
  --#{$prefix}dropdown-link-disabled-color: #{$dropdown-link-disabled-color};
  --#{$prefix}dropdown-item-padding-x: #{$dropdown-item-padding-x};
  --#{$prefix}dropdown-item-padding-y: #{$dropdown-item-padding-y};
  --#{$prefix}dropdown-header-color: #{$dropdown-header-color};
  --#{$prefix}dropdown-header-padding-x: #{$dropdown-header-padding-x};
  --#{$prefix}dropdown-header-padding-y: #{$dropdown-header-padding-y};
  

Customization los ntawm CSS variables tuaj yeem pom nyob rau hauv .dropdown-menu-darkchav kawm uas peb hla cov txiaj ntsig tshwj xeeb yam tsis tau ntxiv cov khoom sib tw CSS.

  --#{$prefix}dropdown-color: #{$dropdown-dark-color};
  --#{$prefix}dropdown-bg: #{$dropdown-dark-bg};
  --#{$prefix}dropdown-border-color: #{$dropdown-dark-border-color};
  --#{$prefix}dropdown-box-shadow: #{$dropdown-dark-box-shadow};
  --#{$prefix}dropdown-link-color: #{$dropdown-dark-link-color};
  --#{$prefix}dropdown-link-hover-color: #{$dropdown-dark-link-hover-color};
  --#{$prefix}dropdown-divider-bg: #{$dropdown-dark-divider-bg};
  --#{$prefix}dropdown-link-hover-bg: #{$dropdown-dark-link-hover-bg};
  --#{$prefix}dropdown-link-active-color: #{$dropdown-dark-link-active-color};
  --#{$prefix}dropdown-link-active-bg: #{$dropdown-dark-link-active-bg};
  --#{$prefix}dropdown-link-disabled-color: #{$dropdown-dark-link-disabled-color};
  --#{$prefix}dropdown-header-color: #{$dropdown-dark-header-color};
  

Sass variables

Variables rau tag nrho cov dropdowns:

$dropdown-min-width:                10rem;
$dropdown-padding-x:                0;
$dropdown-padding-y:                .5rem;
$dropdown-spacer:                   .125rem;
$dropdown-font-size:                $font-size-base;
$dropdown-color:                    $body-color;
$dropdown-bg:                       $white;
$dropdown-border-color:             var(--#{$prefix}border-color-translucent);
$dropdown-border-radius:            $border-radius;
$dropdown-border-width:             $border-width;
$dropdown-inner-border-radius:      subtract($dropdown-border-radius, $dropdown-border-width);
$dropdown-divider-bg:               $dropdown-border-color;
$dropdown-divider-margin-y:         $spacer * .5;
$dropdown-box-shadow:               $box-shadow;

$dropdown-link-color:               $gray-900;
$dropdown-link-hover-color:         shade-color($dropdown-link-color, 10%);
$dropdown-link-hover-bg:            $gray-200;

$dropdown-link-active-color:        $component-active-color;
$dropdown-link-active-bg:           $component-active-bg;

$dropdown-link-disabled-color:      $gray-500;

$dropdown-item-padding-y:           $spacer * .25;
$dropdown-item-padding-x:           $spacer;

$dropdown-header-color:             $gray-600;
$dropdown-header-padding-x:         $dropdown-item-padding-x;
$dropdown-header-padding-y:         $dropdown-padding-y;
// fusv-disable
$dropdown-header-padding:           $dropdown-header-padding-y $dropdown-header-padding-x; // Deprecated in v5.2.0
// fusv-enable

Variables rau qhov tsaus nti dropdown :

$dropdown-dark-color:               $gray-300;
$dropdown-dark-bg:                  $gray-800;
$dropdown-dark-border-color:        $dropdown-border-color;
$dropdown-dark-divider-bg:          $dropdown-divider-bg;
$dropdown-dark-box-shadow:          null;
$dropdown-dark-link-color:          $dropdown-dark-color;
$dropdown-dark-link-hover-color:    $white;
$dropdown-dark-link-hover-bg:       rgba($white, .15);
$dropdown-dark-link-active-color:   $dropdown-link-active-color;
$dropdown-dark-link-active-bg:      $dropdown-link-active-bg;
$dropdown-dark-link-disabled-color: $gray-500;
$dropdown-dark-header-color:        $gray-500;

Cov kev hloov pauv rau CSS-based carets uas qhia txog kev sib cuam tshuam ntawm kev poob qis:

$caret-width:                 .3em;
$caret-vertical-align:        $caret-width * .85;
$caret-spacing:               $caret-width * .85;

Mixins

Mixins yog siv los tsim CSS-based carets thiab tuaj yeem pom hauv scss/mixins/_caret.scss.

@mixin caret-down {
  border-top: $caret-width solid;
  border-right: $caret-width solid transparent;
  border-bottom: 0;
  border-left: $caret-width solid transparent;
}

@mixin caret-up {
  border-top: 0;
  border-right: $caret-width solid transparent;
  border-bottom: $caret-width solid;
  border-left: $caret-width solid transparent;
}

@mixin caret-end {
  border-top: $caret-width solid transparent;
  border-right: 0;
  border-bottom: $caret-width solid transparent;
  border-left: $caret-width solid;
}

@mixin caret-start {
  border-top: $caret-width solid transparent;
  border-right: $caret-width solid;
  border-bottom: $caret-width solid transparent;
}

@mixin caret($direction: down) {
  @if $enable-caret {
    &::after {
      display: inline-block;
      margin-left: $caret-spacing;
      vertical-align: $caret-vertical-align;
      content: "";
      @if $direction == down {
        @include caret-down();
      } @else if $direction == up {
        @include caret-up();
      } @else if $direction == end {
        @include caret-end();
      }
    }

    @if $direction == start {
      &::after {
        display: none;
      }

      &::before {
        display: inline-block;
        margin-right: $caret-spacing;
        vertical-align: $caret-vertical-align;
        content: "";
        @include caret-start();
      }
    }

    &:empty::after {
      margin-left: 0;
    }
  }
}

Kev siv

Ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript, lub dropdown plugin toggles zais cov ntsiab lus (dropdown menus) los ntawm toggling .showchav kawm ntawm niam txiv .dropdown-menu. Tus data-bs-toggle="dropdown"cwj pwm yog tso siab rau kev kaw cov ntawv qhia zaub mov ntawm qib kev thov, yog li nws yog ib lub tswv yim zoo rau ib txwm siv nws.

Ntawm cov khoom siv kov, qhib lub dropdown ntxiv cov mouseoverneeg ua haujlwm khoob rau cov me nyuam tam sim ntawd ntawm lub <body>caij. Qhov no lees paub dab tuag hack yog tsim nyog los ua hauj lwm nyob ib ncig ntawm ib tug quirk nyob rau hauv iOS no 'kev tshwm sim delegation , uas yuav txwv tsis pub ib tug kais nyob qhov twg los ntawm lub dropdown los ntawm triggering cov cai uas kaw lub dropdown. Thaum lub dropdown kaw lawm, cov khoom siv khoob khoob mouseoverno raug tshem tawm.

Los ntawm cov ntaub ntawv attributes

Ntxiv data-bs-toggle="dropdown"rau qhov txuas lossis khawm kom toggle ib qho kev poob.

<div class="dropdown">
  <button type="button" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown trigger
  </button>
  <ul class="dropdown-menu">
    ...
  </ul>
</div>

Ntawm JavaScript

Hu rau dropdowns ntawm JavaScript:

const dropdownElementList = document.querySelectorAll('.dropdown-toggle')
const dropdownList = [...dropdownElementList].map(dropdownToggleEl => new bootstrap.Dropdown(dropdownToggleEl))
data-bs-toggle="dropdown"tseem xav tau

Txawm hais tias koj hu koj lub dropdown ntawm JavaScript los yog siv cov ntaub ntawv-api, data-bs-toggle="dropdown"ib txwm yuav tsum muaj nyob rau hauv lub dropdown lub ntsiab lus.

Kev xaiv

Raws li kev xaiv tuaj yeem dhau los ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript, koj tuaj yeem ntxiv ib qho kev xaiv npe rau data-bs-, xws li hauv data-bs-animation="{value}". Nco ntsoov hloov cov ntaub ntawv ntawm lub npe xaiv los ntawm " camelCase " mus rau " kebab-case " thaum dhau cov kev xaiv ntawm cov ntaub ntawv cwj pwm. Piv txwv li, siv data-bs-custom-class="beautifier"es tsis txhob data-bs-customClass="beautifier".

Raws li ntawm Bootstrap 5.2.0, tag nrho cov khoom txhawb nqa qhov kev sim khaws tseg cov ntaub ntawv tus cwj pwm data-bs-configuas tuaj yeem ua tsev yooj yim kev teeb tsa raws li JSON txoj hlua. Thaum ib qho khoom muaj data-bs-config='{"delay":0, "title":123}'thiab data-bs-title="456"tus cwj pwm, qhov kawg titletus nqi yuav yog 456thiab cov ntaub ntawv cais cov cwj pwm yuav override tus nqi muab rau data-bs-config. Ntxiv mus, cov ntaub ntawv uas twb muaj lawm muaj peev xwm ua tsev JSON qhov tseem ceeb xws li data-bs-delay='{"show":0,"hide":150}'.

Lub npe Hom Default Kev piav qhia
autoClose boolean, hlua true Configure qhov pib kaw tus cwj pwm ntawm dropdown:
  • true- lub dropdown yuav raug kaw los ntawm txhaj rau sab nraud lossis sab hauv cov ntawv qhia zaub mov.
  • false- lub dropdown yuav raug kaw los ntawm nias lub khawm toggle thiab manually hu hidelos yog toggletxoj kev. (Kuj yuav tsis raug kaw los ntawm nias lub pob esc)
  • 'inside'- lub dropdown yuav raug kaw (tsuas yog) los ntawm txhaj rau hauv cov ntawv qhia zaub mov.
  • 'outside'- lub dropdown yuav raug kaw (tsuas yog) los ntawm txhaj rau sab nraum lub dropdown zaub mov.
Nco tseg: lub dropdown yeej ib txwm raug kaw nrog tus ESCyuam sij.
boundary hlua, element 'clippingParents' Overflow txwv ciam teb ntawm cov ntawv qhia dropdown (tsuas yog siv rau Popper's PreventOverflow modifier). Los ntawm lub neej ntawd nws yog clippingParentsthiab tuaj yeem lees txais HTMLElement siv (ntawm JavaScript nkaus xwb). Yog xav paub ntxiv mus saib Popper's detectOverflow docs .
display txoj hlua 'dynamic' Los ntawm lub neej ntawd, peb siv Popper rau dynamic positioning. Disable qhov no nrog static.
offset array, hlua, function [0, 2] Offset ntawm dropdown txheeb ze rau nws lub hom phiaj. Koj tuaj yeem hla ib txoj hlua hauv cov ntaub ntawv tus cwj pwm nrog comma cais qhov tseem ceeb xws li: data-bs-offset="10,20". Thaum muaj nuj nqi yog siv los txiav txim qhov offset, nws raug hu nrog ib yam khoom uas muaj cov popper qhov chaw, siv, thiab popper rects raws li nws thawj qhov kev sib cav. Lub triggering element DOM node tau dhau los ua qhov kev sib cav thib ob. Txoj haujlwm yuav tsum rov qab ib qho array nrog ob tus lej: skidding , nrug . Yog xav paub ntxiv mus saib Popper's offset docs .
popperConfig null, object, function null Txhawm rau hloov Bootstrap's default Popper config, saib Popper's configuration . Thaum muaj nuj nqi siv los tsim Popper teeb tsa, nws tau hu nrog ib qho khoom uas muaj Bootstrap's default Popper configuration. Nws pab koj siv thiab ua ke lub neej ntawd nrog koj tus kheej configuration. Cov haujlwm yuav tsum xa rov qab cov khoom teeb tsa rau Popper.
reference hlua, element, object 'toggle' Reference element ntawm dropdown menu. Txais cov txiaj ntsig ntawm 'toggle', 'parent', HTMLElement siv los yog ib qho khoom muab getBoundingClientRect. Yog xav paub ntxiv mus saib Popper's constructor docs thiab virtual element docs .

Siv ua haujlwm nrogpopperConfig

const dropdown = new bootstrap.Dropdown(element, {
  popperConfig(defaultBsPopperConfig) {
    // const newPopperConfig = {...}
    // use defaultBsPopperConfig if needed...
    // return newPopperConfig
  }
})

Cov txheej txheem

Txoj kev Kev piav qhia
dispose Ua kom ib qho kev poob qis. (Tshem cov ntaub ntawv khaws cia ntawm DOM lub caij)
getInstance Txoj kev zoo li qub uas tso cai rau koj kom tau txais cov piv txwv poob cuam tshuam rau DOM lub caij, koj tuaj yeem siv nws zoo li no: bootstrap.Dropdown.getInstance(element).
getOrCreateInstance Txoj kev zoo li qub uas xa rov qab qhov piv txwv poob cuam tshuam rau DOM lub caij lossis tsim ib qho tshiab yog tias nws tsis tau pib. Koj tuaj yeem siv nws li no: bootstrap.Dropdown.getOrCreateInstance(element).
hide Nias cov ntawv qhia zaub mov ntawm ib qho navbar muab lossis tabbed navigation.
show Qhia cov ntawv qhia zaub mov dropdown ntawm ib tug muab navbar lossis tabbed navigation.
toggle Toggles lub dropdown ntawv qhia zaub mov ntawm ib tug muab navbar los yog tabbed navigation.
update Hloov tshiab txoj hauj lwm ntawm ib qho kev poob qis.

Cov xwm txheej

Tag nrho cov txheej xwm dropdown raug rho tawm haujlwm ntawm lub cim toggling thiab tom qab ntawd bubbled. Yog li koj tuaj yeem ntxiv cov neeg mloog cov xwm txheej ntawm tus .dropdown-menuniam txiv lub ntsiab. hide.bs.dropdownthiab hidden.bs.dropdowncov xwm txheej muaj cov clickEventcuab yeej (tsuas yog thaum thawj hom Txheej Txheem yog click) uas muaj Cov Txheej Txheem Txheej Txheem rau qhov kev tshwm sim nyem.

Hom xwm txheej Kev piav qhia
hide.bs.dropdown Hluav taws kub tam sim ntawd thaum hidehu ua piv txwv txoj kev.
hidden.bs.dropdown Raug rho tawm haujlwm thaum lub dropdown tiav lawm muab zais los ntawm tus neeg siv thiab CSS kev hloov pauv tau ua tiav.
show.bs.dropdown Hluav taws kub tam sim ntawd thaum showhu ua piv txwv.
shown.bs.dropdown Raug rho tawm haujlwm thaum lub dropdown tau pom rau tus neeg siv thiab CSS kev hloov pauv tau ua tiav.
const myDropdown = document.getElementById('myDropdown')
myDropdown.addEventListener('show.bs.dropdown', event => {
  // do something...
})