Dropdowns
Skeakelje kontekstuele overlays foar it werjaan fan listen mei keppelings en mear mei de Bootstrap dropdown-plugin.
Oersicht
Dropdowns binne te wikseljen, kontekstuele overlays foar it werjaan fan listen mei keppelings en mear. Se wurde ynteraktyf makke mei it ynbegrepen Bootstrap dropdown JavaScript-plugin. Se wurde wiksele troch te klikken, net troch te sweven; dit is in opsetlike ûntwerpbeslút .
Dropdowns binne boud op in bibleteek fan tredden, Popper , dy't dynamyske posysjonearring en sichtportdeteksje leveret. Wês wis dat jo popper.min.js opnimme foardat Bootstrap's JavaScript of brûk bootstrap.bundle.min.js
/ bootstrap.bundle.js
dy't Popper befettet. Popper wurdt net brûkt om dropdowns yn navbars te pleatsen, om't dynamyske posisjonearring net fereaske is.
Tagonklikheid
De WAI ARIA - standert definiearret in wirklike role="menu"
widget , mar dit is spesifyk foar applikaasje-like menu's dy't aksjes of funksjes trigger. ARIA -menu's kinne allinich menu-items, karfakje-menu-items, radioknopmenu-items, radioknopgroepen en submenu's befetsje.
De dropdowns fan Bootstrap, oan 'e oare kant, binne ûntworpen om generysk te wêzen en fan tapassing op in ferskaat oan situaasjes en markupstruktueren. It is bygelyks mooglik om dropdowns oan te meitsjen dy't ekstra ynputs en formulierkontrôles befetsje, lykas sykfjilden of oanmeldformulieren. Om dizze reden ferwachtet Bootstrap gjin (net automatysk tafoegje) ien fan 'e role
en aria-
attributen dy't nedich binne foar wiere ARIA- menu's. Auteurs sille dizze mear spesifike attributen sels moatte opnimme.
Bootstrap foegje lykwols ynboude stipe ta foar de measte standert toetseboerdmenu-ynteraksjes, lykas de mooglikheid om troch yndividuele .dropdown-item
eleminten te bewegen mei de rinnerketoetsen en it menu te sluten mei de ESCkaai.
Foarbylden
Wrap de skeakel fan it dellûkmenu (jo knop of keppeling) en it dellûkmenu binnen .dropdown
, of in oar elemint dat ferklearret position: relative;
. Dropdowns kinne wurde aktivearre fan <a>
of <button>
eleminten om better te passen by jo potensjele behoeften. De hjir werjûn foarbylden brûke semantyske <ul>
eleminten wêr passend, mar oanpaste markearring wurdt stipe.
Single knop
Elke single .btn
kin wurde feroare yn in dropdown-skeakel mei wat markupwizigingen. Hjir is hoe't jo se mei beide <button>
eleminten oan it wurk kinne sette:
<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>
En mei <a>
eleminten:
<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown link
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<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>
It bêste diel is dat jo dit ek kinne dwaan mei elke knopfariant:
<!-- 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 knop
Meitsje op deselde manier split knop dropdowns mei praktysk deselde markup as single knop dropdowns, mar mei de tafoeging fan .dropdown-toggle-split
foar juste ôfstân om de dropdown caret.
Wy brûke dizze ekstra klasse te ferminderjen de horizontale padding
oan wjerskanten fan 'e caret mei 25% en fuortsmite de margin-left
dat is tafoege foar reguliere knop dropdowns. Dy ekstra feroarings hâlden de caret sintraal yn de split knop en soargje foar in mear passend grutte hit gebiet neist de wichtichste knop.
<!-- 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>
Sizing
Button dropdowns wurkje mei knoppen fan alle grutte, ynklusyf standert en split dropdown knoppen.
<!-- 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>
Donkere dropdowns
Kies foar donkere dropdowns om te passen by in donkere navbar of oanpaste styl troch ta te foegjen .dropdown-menu-dark
oan in besteande .dropdown-menu
. Gjin wizigingen binne nedich foar de dropdown-items.
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton2" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<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>
En it te brûken yn in navbar:
<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>
Rjochtings
RTL
Rjochtingen wurde spegele by it brûken fan Bootstrap yn RTL, betsjutting .dropstart
sil oan 'e rjochterkant ferskine.
Dropup
Trigger dropdown-menu's boppe eleminten troch ta te foegjen .dropup
oan it âlderelemint.
<!-- 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>
Dropright
Trigger dellûkmenu's rjochts fan 'e eleminten troch ta te foegjen .dropend
oan it âlderelemint.
<!-- Default dropend button -->
<div class="btn-group dropend">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropright
</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 Dropright</span>
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
Dropleft
Trigger dropdown-menu's links fan 'e eleminten troch ta te foegjen .dropstart
oan it âlderelemint.
<!-- 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">
<div class="btn-group dropstart" role="group">
<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>
</div>
<button type="button" class="btn btn-secondary">
Split dropstart
</button>
</div>
Menu items
Jo kinne <a>
of <button>
eleminten brûke as dropdown-items.
<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" aria-labelledby="dropdownMenu2">
<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>
Jo kinne ek net-ynteraktive dropdown-items meitsje mei .dropdown-item-text
. Fiel jo frij om fierder te stylen mei oanpaste CSS- of teksthulpprogramma's.
<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>
Aktyf
Taheakje .active
oan items yn it útklapmenu om se as aktyf te stylearjen . Om de aktive steat oer te bringen oan assistive technologyen, brûk it aria-current
attribút - mei de page
wearde foar de aktuele side, of true
foar it aktuele item yn in set.
<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>
Ynvalide
Taheakje .disabled
oan items yn it útklapmenu om se te stylearjen as útskeakele .
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Regular link</a></li>
<li><a class="dropdown-item disabled" href="#" tabindex="-1" aria-disabled="true">Disabled link</a></li>
<li><a class="dropdown-item" href="#">Another link</a></li>
</ul>
Menu alignment
Standert wurdt in útklapmenu automatysk 100% fan boppen en lâns de linkerkant fan syn âlder pleatst. Jo kinne dit feroarje mei de rjochtingsklassen .drop*
, mar jo kinne se ek kontrolearje mei ekstra modifikaasjeklassen.
Taheakje .dropdown-menu-end
oan in .dropdown-menu
rjochts rjochtsje it dellûkmenu. Rjochtingen wurde spegele by it brûken fan Bootstrap yn RTL, betsjutting .dropdown-menu-end
sil ferskine oan 'e linkerkant.
<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
As jo responsive ôfstimming wolle brûke, skeakelje dynamyske posysjonearring út troch it data-bs-display="static"
attribút ta te foegjen en brûk de responsive fariaasjeklassen.
Om it útklapmenu rjochts.dropdown-menu{-sm|-md|-lg|-xl|-xxl}-end
út te rjochtsjen mei it opjûne brekpunt of grutter, foegje jo ta .
<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>
Om it útklapmenu linksôf te rjochtsjen mei it opjûne brekpunt of grutter, foegje .dropdown-menu-end
en ta .dropdown-menu{-sm|-md|-lg|-xl|-xxl}-start
.
<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>
Tink derom dat jo gjin data-bs-display="static"
attribút taheakje moatte oan dropdown-knoppen yn navbars, om't Popper net brûkt wurdt yn navbars.
Alignment opsjes
Troch de measte fan 'e hjirboppe werjûn opsjes te nimmen, is hjir in lytse demo fan keukenwasken fan ferskate opsjes foar dropdown-ôfstimming op ien plak.
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<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>
Menu ynhâld
Kopteksten
Foegje in koptekst ta om seksjes fan aksjes te markearjen yn elk útklapmenu.
<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>
Dividers
Skieden groepen fan relatearre menu-items mei in divider.
<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>
Tekst
Pleats elke tekst yn 'e frije foarm yn in útklapmenu mei tekst en brûk spaasje-hulpprogramma's . Tink derom dat jo wierskynlik ekstra maatstilen nedich binne om de menubreedte te beheinen.
<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>
Formulieren
Set in formulier yn in útklapmenu, of meitsje it yn in útklapmenu, en brûk marzje- of padding -helpprogramma's om it de negative romte te jaan dy't jo nedich binne.
<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>
<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>
Dropdown opsjes
Brûk data-bs-offset
of data-bs-reference
om de lokaasje fan it dellûkmenu te feroarjen.
<div class="d-flex">
<div class="dropdown me-1">
<button type="button" class="btn btn-secondary dropdown-toggle" id="dropdownMenuOffset" data-bs-toggle="dropdown" aria-expanded="false" data-bs-offset="10,20">
Offset
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuOffset">
<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" id="dropdownMenuReference" data-bs-toggle="dropdown" aria-expanded="false" data-bs-reference="parent">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuReference">
<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>
Auto slute gedrach
Standert is it útklapmenu sluten as jo binnen of bûten it útklapmenu klikke. Jo kinne de autoClose
opsje brûke om dit gedrach fan it útklapmenu te feroarjen.
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="defaultDropdown" data-bs-toggle="dropdown" data-bs-auto-close="true" aria-expanded="false">
Default dropdown
</button>
<ul class="dropdown-menu" aria-labelledby="defaultDropdown">
<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" id="dropdownMenuClickableOutside" data-bs-toggle="dropdown" data-bs-auto-close="inside" aria-expanded="false">
Clickable outside
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableOutside">
<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" id="dropdownMenuClickableInside" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
Clickable inside
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableInside">
<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" id="dropdownMenuClickable" data-bs-toggle="dropdown" data-bs-auto-close="false" aria-expanded="false">
Manual close
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuClickable">
<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>
Sass
Fariabelen
Fariabelen foar alle 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: rgba($black, .15);
$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($gray-900, 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: $dropdown-padding-y $dropdown-item-padding-x;
Fariabelen foar de tsjustere 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;
Fariabelen foar de CSS-basearre carets dy't de ynteraktiviteit fan in dropdown oanjaan:
$caret-width: .3em;
$caret-vertical-align: $caret-width * .85;
$caret-spacing: $caret-width * .85;
Mixins
Mixins wurde brûkt om de CSS-basearre carets te generearjen en kinne fûn wurde yn 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;
}
}
}
Gebrûk
Fia gegevensattributen of JavaSkript skeakelt de dropdown-plugin ferburgen ynhâld (útklapmenu's) troch de .show
klasse op 'e âlder te wikseljen .dropdown-menu
. It data-bs-toggle="dropdown"
attribút wurdt fertroud foar it sluten fan dellûkmenu's op in applikaasjenivo, dus it is in goed idee om it altyd te brûken.
mouseover
handlers ta oan de direkte bern fan it
<body>
elemint. Dizze ûnsjogge hack is nedich om in
eigensinnigens te wurkjen yn 'e delegaasje fan iOS-eveneminten , dy't oars soe foarkomme dat in tap oeral bûten it dropdown-menu de koade trigger dy't de dropdown slút. Sadree't it útklapmenu is sletten, wurde dizze ekstra lege
mouseover
handlers fuortsmiten.
Fia data attributen
Taheakje data-bs-toggle="dropdown"
oan in keppeling of knop om in dropdown te wikseljen.
<div class="dropdown">
<button id="dLabel" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown trigger
</button>
<ul class="dropdown-menu" aria-labelledby="dLabel">
...
</ul>
</div>
Fia JavaScript
Rop de dropdowns fia JavaScript:
var dropdownElementList = [].slice.call(document.querySelectorAll('.dropdown-toggle'))
var dropdownList = dropdownElementList.map(function (dropdownToggleEl) {
return new bootstrap.Dropdown(dropdownToggleEl)
})
data-bs-toggle="dropdown"
noch nedich
Nettsjinsteande of jo jo dropdown fia JavaScript neame of ynstee de data-api brûke, data-bs-toggle="dropdown"
is altyd fereaske om oanwêzich te wêzen op it trigger-elemint fan 'e dropdown.
Opsjes
Opsjes kinne wurde trochjûn fia gegevensattributen of JavaScript. Foar gegevensattributen foegje de opsjenamme ta oan data-bs-
, lykas yn data-bs-offset=""
. Soargje derfoar dat jo it gefaltype fan 'e opsjenamme feroarje fan camelCase nei kebab-case as jo de opsjes trochjaan fia gegevensattributen. Bygelyks, ynstee fan te brûken data-bs-autoClose="false"
, brûk dan data-bs-auto-close="false"
.
Namme | Type | Standert | Beskriuwing |
---|---|---|---|
boundary |
string | elemint | 'clippingParents' |
Overflow beheining grins fan it dellûk menu (jildt allinnich foar Popper syn preventOverflow modifier). Standert is it 'clippingParents' en kin it in HTMLElement-referinsje akseptearje (allinich fia JavaScript). Foar mear ynformaasje ferwize nei Popper's detectOverflow-dokuminten . |
reference |
string | elemint | objekt | 'toggle' |
Referinsje elemint fan it dellûk menu. Akseptearret de wearden fan 'toggle' , 'parent' , in HTMLElement-referinsje as in objekt dat getBoundingClientRect . Foar mear ynformaasje ferwize nei Popper syn constructor docs en firtuele elemint docs . |
display |
string | 'dynamic' |
Standert brûke wy Popper foar dynamyske posisjonearring. Skeakelje dit út mei static . |
offset |
rige | string | funksje | [0, 2] |
Offset fan it dropdown relatyf oan syn doel. Jo kinne in tekenrige trochjaan yn gegevensattributen mei komma-skieden wearden lykas: As in funksje brûkt wurdt om de offset te bepalen, wurdt it neamd mei in objekt dat de popper pleatsing, de referinsje en popperrects as syn earste argumint befettet. It triggerelemint DOM-knooppunt wurdt trochjûn as it twadde argumint. De funksje moat in array weromjaan mei twa nûmers: . Foar mear ynformaasje ferwize nei Popper's offset -dokuminten . |
autoClose |
boolean | string | true |
Konfigurearje it auto-slutgedrach fan 'e dropdown:
|
popperConfig |
null | objekt | funksje | null |
Om de standert Popper-konfiguraasje fan Bootstrap te feroarjen, sjoch Popper's konfiguraasje . As in funksje wurdt brûkt om de Popper-konfiguraasje te meitsjen, wurdt it neamd mei in objekt dat de standert Popper-konfiguraasje fan de Bootstrap befettet. It helpt jo de standert te brûken en te fusearjen mei jo eigen konfiguraasje. De funksje moat werom in konfiguraasje foarwerp foar Popper. |
Mei help fan funksje meipopperConfig
var dropdown = new bootstrap.Dropdown(element, {
popperConfig: function (defaultBsPopperConfig) {
// var newPopperConfig = {...}
// use defaultBsPopperConfig if needed...
// return newPopperConfig
}
})
Metoaden
Metoade | Beskriuwing |
---|---|
toggle |
Skeakelt it dellûkmenu fan in opjûne navigaasjebalke of navigaasje mei ljeppers. |
show |
Toant it dellûkmenu fan in opjûne navigaasjebalke of navigaasje mei ljeppers. |
hide |
Ferberget it dellûkmenu fan in opjûne navigaasjebalke of navigaasje mei ljeppers. |
update |
Updatet de posysje fan in dellûkmenu fan in elemint. |
dispose |
Ferneatiget de dropdown fan in elemint. (Ferwidert bewarre gegevens op it DOM-elemint) |
getInstance |
Statyske metoade wêrmei jo de dropdown-eksimplaar kinne krije ferbûn oan in DOM-elemint, jo kinne it sa brûke:bootstrap.Dropdown.getInstance(element) |
getOrCreateInstance |
Statyske metoade dy't in dropdown-eksimplaar werombringt dy't assosjeare is mei in DOM-elemint of in nij oanmeitsje yn gefal it net inisjalisearre is. Jo kinne it sa brûke:bootstrap.Dropdown.getOrCreateInstance(element) |
Eveneminten
Alle dropdown-eveneminten wurde op it wikseljende elemint ûntslein en dan opborreld. Sa kinne jo ek barren-harkers tafoegje oan it âlderelemint fan .dropdown-menu
'e. hide.bs.dropdown
en hidden.bs.dropdown
eveneminten hawwe in clickEvent
eigenskip (allinne as de oarspronklike Event type is click
) dat befettet in Event Object foar de klik evenemint.
Metoade | Beskriuwing |
---|---|
show.bs.dropdown |
Fires fuortendaliks doe't de show eksimplaar metoade wurdt neamd. |
shown.bs.dropdown |
Untslein as de dropdown sichtber makke is foar de brûker en CSS-oergongen binne foltôge. |
hide.bs.dropdown |
Fires fuortendaliks as de hide eksimplaar metoade is oanroppen. |
hidden.bs.dropdown |
Untslein as de dropdown klear is ferburgen foar de brûker en CSS-oergongen binne foltôge. |
var myDropdown = document.getElementById('myDropdown')
myDropdown.addEventListener('show.bs.dropdown', function () {
// do something...
})