Source

Dropdowns

Toggle contextual overlays rau tso saib cov npe ntawm cov 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 hovering; qhov no yog kev txiav txim siab tsim qauv.

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

Yog tias koj tab tom tsim peb JavaScript los ntawm qhov chaw, nws xav tauutil.js .

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 uas ua rau kev ua lossis kev ua haujlwm. ARIA cov ntawv qhia zaub mov tsuas yog muaj cov ntawv qhia zaub mov xwb, cov ntawv qhia zaub mov checkbox, xov tooj cua khawm cov ntawv qhia zaub mov, pab pawg xov tooj cua, 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 ntawm cov khoom 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 tshwm sim los ntawm <a>lossis <button>cov ntsiab lus kom haum rau koj cov kev xav tau.

Ib lub khawm dropdowns

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

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

Thiab nrog <a>cov ntsiab lus:

<div class="dropdown show">
  <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown link
  </a>

  <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Action
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
    <div class="dropdown-divider"></div>
    <a class="dropdown-item" href="#">Separated link</a>
  </div>
</div>

Split khawm dropdowns

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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span class="sr-only">Toggle Dropdown</span>
  </button>
  <div class="dropdown-menu">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
    <div class="dropdown-divider"></div>
    <a class="dropdown-item" href="#">Separated link</a>
  </div>
</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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Large button
  </button>
  <div class="dropdown-menu">
    ...
  </div>
</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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span class="sr-only">Toggle Dropdown</span>
  </button>
  <div class="dropdown-menu">
    ...
  </div>
</div>

<!-- Small button groups (default and split) -->
<div class="btn-group">
  <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Small button
  </button>
  <div class="dropdown-menu">
    ...
  </div>
</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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span class="sr-only">Toggle Dropdown</span>
  </button>
  <div class="dropdown-menu">
    ...
  </div>
</div>

Dropup variation

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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropup
  </button>
  <div class="dropdown-menu">
    <!-- Dropdown menu links -->
  </div>
</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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span class="sr-only">Toggle Dropdown</span>
  </button>
  <div class="dropdown-menu">
    <!-- Dropdown menu links -->
  </div>
</div>

Dropright variation

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

<!-- Default dropright button -->
<div class="btn-group dropright">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropright
  </button>
  <div class="dropdown-menu">
    <!-- Dropdown menu links -->
  </div>
</div>

<!-- Split dropright button -->
<div class="btn-group dropright">
  <button type="button" class="btn btn-secondary">
    Split dropright
  </button>
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span class="sr-only">Toggle Dropright</span>
  </button>
  <div class="dropdown-menu">
    <!-- Dropdown menu links -->
  </div>
</div>

Dropleft variation

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

<!-- Default dropleft button -->
<div class="btn-group dropleft">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropleft
  </button>
  <div class="dropdown-menu">
    <!-- Dropdown menu links -->
  </div>
</div>

<!-- Split dropleft button -->
<div class="btn-group">
  <div class="btn-group dropleft" role="group">
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <span class="sr-only">Toggle Dropleft</span>
    </button>
    <div class="dropdown-menu">
      <!-- Dropdown menu links -->
    </div>
  </div>
  <button type="button" class="btn btn-secondary">
    Split dropleft
  </button>
</div>

Keeb kwm dropdown cov ntawv qhia zaub mov yuav tsum tau txuas, tab sis qhov ntawd tsis yog qhov teeb meem nrog v4. Tam sim no koj tuaj yeem xaiv tau siv <button>cov ntsiab lus hauv koj cov dropdowns tsis yog <a>s.

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
    <button class="dropdown-item" type="button">Action</button>
    <button class="dropdown-item" type="button">Another action</button>
    <button class="dropdown-item" type="button">Something else here</button>
  </div>
</div>

Los ntawm lub neej ntawd, cov ntawv qhia zaub mov poob cia li tso 100% los ntawm sab saum toj thiab sab laug ntawm nws niam nws txiv. Ntxiv .dropdown-menu-rightrau .dropdown-menutxoj cai dlhos cov ntawv qhia zaub mov.

Tau taub hau! Dropdowns yog positioned ua tsaug rau Popper.js (tshwj tsis yog thaum lawv muaj nyob rau hauv ib tug navbar).

<div class="btn-group">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Right-aligned menu
  </button>
  <div class="dropdown-menu dropdown-menu-right">
    <button class="dropdown-item" type="button">Action</button>
    <button class="dropdown-item" type="button">Another action</button>
    <button class="dropdown-item" type="button">Something else here</button>
  </div>
</div>

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

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

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

<div class="dropdown-menu">
  <a class="dropdown-item" href="#">Action</a>
  <a class="dropdown-item" href="#">Another action</a>
  <a class="dropdown-item" href="#">Something else here</a>
  <div class="dropdown-divider"></div>
  <a class="dropdown-item" href="#">Separated link</a>
</div>

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.

<div class="dropdown-menu">
  <form class="px-4 py-3">
    <div class="form-group">
      <label for="exampleDropdownFormEmail1">Email address</label>
      <input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="[email protected]">
    </div>
    <div class="form-group">
      <label for="exampleDropdownFormPassword1">Password</label>
      <input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
    </div>
    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="dropdownCheck">
      <label class="form-check-label" for="dropdownCheck">
        Remember me
      </label>
    </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>
<form class="dropdown-menu p-4">
  <div class="form-group">
    <label for="exampleDropdownFormEmail2">Email address</label>
    <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="[email protected]">
  </div>
  <div class="form-group">
    <label for="exampleDropdownFormPassword2">Password</label>
    <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
  </div>
  <div class="form-check">
    <input type="checkbox" class="form-check-input" id="dropdownCheck2">
    <label class="form-check-label" for="dropdownCheck2">
      Remember me
    </label>
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>

Active zaub mov khoom

Ntxiv .activerau cov khoom hauv dropdown kom style lawv li nquag .

<div class="dropdown-menu">
  <a class="dropdown-item" href="#">Regular link</a>
  <a class="dropdown-item active" href="#">Active link</a>
  <a class="dropdown-item" href="#">Another link</a>
</div>

Disabled khoom noj khoom haus

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

<div class="dropdown-menu">
  <a class="dropdown-item" href="#">Regular link</a>
  <a class="dropdown-item disabled" href="#">Disabled link</a>
  <a class="dropdown-item" href="#">Another link</a>
</div>

Kev siv

Los 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 cov npe khoom. Tus data-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-enabled, qhib lub dropdown ntxiv khoob ( $.noop) mouseoverhandlers 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-toggle="dropdown"rau qhov txuas lossis khawm kom toggle ib qho kev poob.

<div class="dropdown">
  <button id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown trigger
  </button>
  <div class="dropdown-menu" aria-labelledby="dLabel">
    ...
  </div>
</div>

Ntawm JavaScript

Hu rau dropdowns ntawm JavaScript:

$('.dropdown-toggle').dropdown()
data-toggle="dropdown"tseem xav tau

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

Kev xaiv

Cov kev xaiv tuaj yeem dhau los ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript. Rau cov ntaub ntawv tus cwj pwm, ntxiv rau qhov kev xaiv npe rau data-, xws li hauv data-offset="".

Lub npe Hom Default Kev piav qhia
offset tus lej | hlua | muaj nuj nqi 0 Offset ntawm dropdown txheeb ze rau nws lub hom phiaj. Yog xav paub ntxiv mus saib Popper.js's offset docs .
tig boolean muaj tseeb Cia Dropdown tig rau hauv rooj plaub ntawm qhov sib tshooj ntawm cov khoom siv. Yog xav paub ntxiv mus saib Popper.js's flip docs .
ciam teb hlua | keeb 'scrollParent' Overflow txwv ciam teb ntawm dropdown zaub mov. Txais cov txiaj ntsig ntawm 'viewport', 'window', 'scrollParent', lossis HTMLElement siv (JavaScript nkaus xwb). Yog xav paub ntxiv mus saib Popper.js's PreventOverflow docs .

Nco ntsoov thaum boundaryteem rau ib qho nqi uas tsis yog 'scrollParent', cov style position: staticyog siv rau lub .dropdownthawv.

Cov txheej txheem

Txoj kev Kev piav qhia
$().dropdown('toggle') Toggles lub dropdown ntawv qhia zaub mov ntawm ib tug muab navbar los yog tabbed navigation.
$().dropdown('update') Hloov tshiab txoj hauj lwm ntawm ib qho kev poob qis.
$().dropdown('dispose') Ua kom ib qho kev poob qis.

Cov xwm txheej

Tag nrho cov txheej xwm dropdown raug rho tawm haujlwm ntawm .dropdown-menu's niam txiv lub caij thiab muaj cov relatedTargetcuab yeej, uas nws tus nqi yog qhov toggling thauj tog rau nkoj caij.

Kev tshwm sim Kev piav qhia
show.bs.dropdown Qhov xwm txheej no tua hluav taws tam sim ntawd thaum hu ua yeeb yam piv txwv.
shown.bs.dropdown Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub dropdown tau pom rau tus neeg siv (yuav tos rau CSS kev hloov pauv, kom tiav).
hide.bs.dropdown Qhov xwm txheej no raug rho tawm haujlwm tam sim ntawd thaum txoj kev zais piv txwv tau raug hu.
hidden.bs.dropdown Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub dropdown tiav lawm muab zais los ntawm tus neeg siv (yuav tos rau CSS hloov, kom tiav).
$('#myDropdown').on('show.bs.dropdown', function () {
  // do something…
})