Dropdowns
Toggle contextual overlays rau tso saib cov npe ntawm cov kev sib txuas thiab ntau ntxiv nrog Bootstrap dropdown plugin.
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.js
uas 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
.
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 role
thiab 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-item
cov ntsiab lus siv tus cursor yuam sij thiab kaw cov ntawv qhia zaub mov nrog tus ESCyuam sij.
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 qho .btn
twg 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:
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-split
rau kom qhov chaw nyob ib ncig ntawm lub dropdown caret.
Peb siv cov chav kawm ntxiv no kom txo tau cov kab rov tav padding
ntawm ob sab ntawm cov cart los ntawm 25% thiab tshem tawm cov margin-left
uas 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.
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.
Trigger dropdown menus saum cov ntsiab lus los ntawm kev ntxiv .dropup
rau niam txiv lub caij.
Trigger dropdown menus ntawm sab xis ntawm cov ntsiab lus los ntawm kev ntxiv .dropright
rau cov niam txiv caij.
Trigger dropdown menus nyob rau sab laug ntawm cov ntsiab lus los ntawm kev ntxiv .dropleft
rau cov niam txiv caij.
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-right
rau .dropdown-menu
txoj 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>
Ntxiv .active
rau 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>
Ntxiv .disabled
rau 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>
Los ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript, lub dropdown plugin toggles zais cov ntsiab lus (dropdown menus) los ntawm toggling .show
chav 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
) mouseover
handlers 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 mouseover
no raug tshem tawm.
Ntxiv data-toggle="dropdown"
rau qhov txuas lossis khawm kom toggle ib qho kev poob.
Hu rau dropdowns ntawm JavaScript:
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.
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 boundary
teem rau ib qho nqi uas tsis yog 'scrollParent'
, cov style position: static
yog siv rau lub .dropdown
thawv.
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. |
Tag nrho cov txheej xwm dropdown raug rho tawm haujlwm ntawm .dropdown-menu
's niam txiv lub caij thiab muaj cov relatedTarget
cuab 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). |