Dropdowns
Ganti overlay kontekstual kanggo nampilake dhaptar pranala lan liya-liyane nganggo plugin gulung mudhun Bootstrap.
Ringkesan
Dropdown bisa diganti, overlay kontekstual kanggo nampilake dhaptar pranala lan liya-liyane. Dheweke digawe interaktif karo plugin JavaScript dropdown Bootstrap sing kalebu. Padha diganti kanthi ngeklik, ora kanthi nglayang; iki minangka keputusan desain sing disengaja .
Dropdowns dibangun ing perpustakaan pihak katelu, Popper , sing nyedhiyakake posisi dinamis lan deteksi viewport. Priksa manawa sampeyan kalebu popper.min.js sadurunge Bootstrap JavaScript utawa gunakake bootstrap.bundle.min.js
/ bootstrap.bundle.js
sing ngemot Popper. Popper ora digunakake kanggo nyelehake dropdown ing navbar sanajan posisi dinamis ora dibutuhake.
Aksesibilitas
Standar WAI ARIArole="menu"
nemtokake widget sing nyata , nanging iki khusus kanggo menu kaya aplikasi sing micu tumindak utawa fungsi. Menu ARIA mung bisa ngemot item menu, item menu kothak centhang, item menu tombol radio, grup tombol radio, lan sub-menu.
Dropdown Bootstrap, ing sisih liya, dirancang kanggo umum lan bisa ditrapake kanggo macem-macem kahanan lan struktur markup. Contone, sampeyan bisa nggawe dropdown sing ngemot input tambahan lan kontrol formulir, kayata kolom telusuran utawa formulir login. Mulane, Bootstrap ora ngarep-arep (utawa kanthi otomatis nambah) samubarang role
lan aria-
atribut sing dibutuhake kanggo menu ARIA sing bener. Penulis kudu nyakup atribut sing luwih spesifik iki.
Nanging, Bootstrap nambahake dhukungan sing dibangun kanggo umume interaksi menu keyboard standar, kayata kemampuan kanggo mindhah .dropdown-item
unsur individu nggunakake tombol kursor lan nutup menu nganggo ESCtombol.
Tuladha
Bungkus pilihan gulung mudhun (tombol utawa link sampeyan) lan menu gulung mudhun ing .dropdown
, utawa unsur liyane sing nyatakake position: relative;
. Dropdown bisa dipicu saka <a>
utawa <button>
unsur supaya luwih cocog karo kabutuhan potensial sampeyan. Conto sing ditampilake ing kene nggunakake <ul>
unsur semantik yen cocog, nanging markup khusus didhukung.
Tombol tunggal
Sembarang .btn
siji bisa diowahi dadi pilihan gulung mudhun kanthi sawetara owah-owahan markup. Mangkene carane sampeyan bisa nggarap <button>
unsur kasebut:
<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>
Lan karo <a>
unsur:
<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>
Sisih paling apik yaiku sampeyan bisa nindakake iki karo varian tombol apa wae:
<!-- 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>
Tombol pamisah
Kajaba iku, nggawe dropdown tombol pamisah kanthi markup sing meh padha karo dropdown tombol siji, nanging kanthi tambahan .dropdown-toggle-split
kanggo jarak sing tepat ing sekitar caret molor.
Kita nggunakake kelas ekstra iki kanggo nyuda horisontal padding
ing salah siji sisih caret karo 25% lan mbusak margin-left
sing ditambahake kanggo dropdowns tombol biasa. Owah-owahan ekstra kasebut njaga karet pusat ing tombol pamisah lan nyedhiyakake area hit sing luwih cocog ing jejere tombol utama.
<!-- 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>
ukuran
Tombol dropdown bisa digunakake karo tombol kabeh ukuran, kalebu tombol gulung standar lan pamisah.
<!-- 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>
Dropdown peteng
Milih dropdown sing luwih peteng kanggo cocog karo navbar peteng utawa gaya khusus kanthi nambahake .dropdown-menu-dark
menyang .dropdown-menu
. Ora ana owah-owahan ing item gulung mudhun.
<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>
Lan nggunakake ing 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>
arah
RTL
Arah dicerminake nalika nggunakake Bootstrap ing RTL, tegese .dropstart
bakal katon ing sisih tengen.
Dropup
Micu menu gulung mudhun ing ndhuwur unsur kanthi nambahake .dropup
menyang unsur induk.
<!-- 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
Micu menu gulung mudhun ing sisih tengen unsur kanthi nambahake .dropend
menyang unsur induk.
<!-- 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
Micu menu gulung mudhun ing sisih kiwa unsur kanthi nambahake .dropstart
menyang unsur induk.
<!-- 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>
Item menu
Sampeyan bisa nggunakake <a>
utawa <button>
unsur minangka item gulung mudhun.
<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>
Sampeyan uga bisa nggawe item dropdown non-interaktif karo .dropdown-item-text
. Bebas bae kanggo gaya luwih karo CSS khusus utawa keperluan teks.
<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>
Aktif
Tambah .active
menyang item ing gulung mudhun kanggo gaya minangka aktif . Kanggo ngirim status aktif menyang teknologi assistive, gunakake aria-current
atribut - nggunakake page
nilai kanggo kaca saiki, utawa true
kanggo item saiki ing 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>
dipatèni
Tambah .disabled
menyang item ing gulung mudhun kanggo gaya minangka dipatèni .
<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>
Alignment menu
Kanthi gawan, menu gulung mudhun kanthi otomatis dipanggonke 100% saka ndhuwur lan ing sisih kiwa wong tuwa. Sampeyan bisa ngganti iki karo .drop*
kelas pituduh, nanging sampeyan uga bisa ngontrol karo kelas modifier tambahan.
Tambah .dropdown-menu-end
menyang .dropdown-menu
sisih tengen menu gulung mudhun. Arah dicerminake nalika nggunakake Bootstrap ing RTL, tegese .dropdown-menu-end
bakal katon ing sisih kiwa.
<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>
Alignment responsif
Yen sampeyan pengin nggunakake keselarasan responsif, mateni posisi dinamis kanthi nambah data-bs-display="static"
atribut lan gunakake kelas variasi responsif.
Kanggo nyelarasake tengen menu gulung mudhun karo breakpoint utawa luwih gedhe, tambahake .dropdown-menu{-sm|-md|-lg|-xl|-xxl}-end
.
<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>
Kanggo nyelarasake kiwa menu molor karo breakpoint diwenehi utawa luwih gedhe, nambah .dropdown-menu-end
lan .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>
Elinga yen sampeyan ora perlu nambah data-bs-display="static"
atribut kanggo tombol gulung mudhun ing navbar, amarga Popper ora digunakake ing navbar.
Pilihan Alignment
Njupuk akeh pilihan sing ditampilake ing ndhuwur, iki demo sink pawon cilik saka macem-macem pilihan alignment gulung mudhun ing sak panggonan.
<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>
Isi menu
Headers
Tambah header kanggo menehi label bagean tumindak ing menu gulung mudhun.
<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>
Pembagi
Pisah klompok item menu sing gegandhengan karo 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>
Teks
Selehake sembarang teks freeform ing menu gulung mudhun karo teks lan nggunakake spasi utilitas . Elinga yen sampeyan mbutuhake gaya ukuran tambahan kanggo mbatesi ambane menu.
<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>
Formulir
Sijine formulir ing menu gulung mudhun, utawa nggawe menyang menu gulung mudhun, lan nggunakake margin utawa padding keperluan kanggo menehi papan negatif sing dibutuhake.
<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>
Pilihan gulung mudhun
Gunakake data-bs-offset
utawa data-bs-reference
kanggo ngganti lokasi gulung mudhun.
<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 nutup prilaku
Kanthi gawan, menu gulung mudhun ditutup nalika ngeklik ing njero utawa njaba menu gulung mudhun. Sampeyan bisa nggunakake autoClose
pilihan kanggo ngganti prilaku gulung mudhun iki.
<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
Variabel
Variabel kanggo kabeh dropdown:
$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($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: $dropdown-padding-y $dropdown-item-padding-x;
Variabel kanggo dropdown peteng :
$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;
Variabel kanggo carets basis CSS sing nuduhake interaktivitas dropdown:
$caret-width: .3em;
$caret-vertical-align: $caret-width * .85;
$caret-spacing: $caret-width * .85;
Campuran
Mixin digunakake kanggo ngasilake carets basis CSS lan bisa ditemokake ing 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;
}
}
}
Panggunaan
Liwat atribut data utawa JavaScript, plugin gulung mudhun ngganti konten sing didhelikake (menu gulung mudhun) kanthi ngganti .show
kelas ing wong tuwa .dropdown-menu
. Atribut data-bs-toggle="dropdown"
kasebut diandelake kanggo nutup menu gulung mudhun ing tingkat aplikasi, mula luwih becik nggunakake aplikasi kasebut.
mouseover
panangan kosong kanggo anak langsung saka
<body>
unsur. Iki hack ngakoni elek perlu kanggo ngubengi
quirk ing iOS 'delegasi acara , kang digunakake bakal nyegah tunyuk ngendi wae njaba dropdown saka pemicu kode sing nutup dropdown. Sawise gulung mudhun ditutup, panangan kosong tambahan
mouseover
iki dibusak.
Liwat atribut data
Tambah data-bs-toggle="dropdown"
menyang link utawa tombol kanggo ngalih gulung mudhun.
<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>
Liwat JavaScript
Telpon dropdown liwat JavaScript:
var dropdownElementList = [].slice.call(document.querySelectorAll('.dropdown-toggle'))
var dropdownList = dropdownElementList.map(function (dropdownToggleEl) {
return new bootstrap.Dropdown(dropdownToggleEl)
})
data-bs-toggle="dropdown"
isih dibutuhake
Ora preduli apa sampeyan nelpon dropdown liwat JavaScript utawa nggunakake data-api, mesthine data-bs-toggle="dropdown"
kudu ana ing unsur pemicu dropdown.
Pilihan
Opsi bisa dilewati liwat atribut data utawa JavaScript. Kanggo atribut data, tambahake jeneng pilihan menyang data-bs-
, kaya ing data-bs-offset=""
. Priksa manawa kanggo ngganti jinis cilik saka jeneng pilihan saka camelCase kanggo kebab-kasus nalika liwat opsi liwat atribut data. Contone, tinimbang nggunakake data-bs-autoClose="false"
, gunakake data-bs-auto-close="false"
.
jeneng | Jinis | Default | Katrangan |
---|---|---|---|
boundary |
senar | unsur | 'clippingParents' |
Watesan watesan overflow saka menu gulung mudhun (mung ditrapake kanggo modifier preventOverflow Popper). Kanthi gawan, iku 'clippingParents' lan bisa nampa referensi HTMLElement (mung liwat JavaScript). Kanggo informasi luwih lengkap waca docs detectOverflow Popper . |
reference |
senar | unsur | obyek | 'toggle' |
Unsur referensi saka menu gulung mudhun. Nampa nilai 'toggle' , 'parent' , referensi HTMLElement utawa obyek sing nyedhiyakake getBoundingClientRect . Kanggo informasi luwih lengkap, deleng dokumen konstruktor Popper lan dokumen unsur virtual . |
display |
senar | 'dynamic' |
Kanthi gawan, kita nggunakake Popper kanggo posisi dinamis. Pateni iki nganggo static . |
offset |
susunan | senar | fungsi | [0, 2] |
Offset saka dropdown relatif kanggo target. Sampeyan bisa ngliwati senar ing atribut data kanthi nilai sing dipisahake koma kaya: Nalika fungsi digunakake kanggo nemtokake offset, diarani obyek sing ngemot panggonan popper, referensi, lan popper rects minangka argumen pisanan. Node DOM unsur pemicu dilewati minangka argumen kapindho. Fungsi kasebut kudu ngasilake array kanthi rong nomer: . Kanggo informasi luwih lengkap waca docs offset Popper . |
autoClose |
boolean | senar | true |
Konfigurasi prilaku nutup otomatis saka gulung mudhun:
|
popperConfig |
null | obyek | fungsi | null |
Kanggo ngganti konfigurasi Popper standar Bootstrap, ndeleng konfigurasi Popper . Nalika fungsi digunakake kanggo nggawe konfigurasi Popper, diarani karo obyek sing ngemot konfigurasi Popper standar Bootstrap. Iki mbantu sampeyan nggunakake lan nggabungake standar karo konfigurasi sampeyan dhewe. Fungsi kasebut kudu ngasilake obyek konfigurasi kanggo Popper. |
Nggunakake fungsi karopopperConfig
var dropdown = new bootstrap.Dropdown(element, {
popperConfig: function (defaultBsPopperConfig) {
// var newPopperConfig = {...}
// use defaultBsPopperConfig if needed...
// return newPopperConfig
}
})
Metode
Metode | Katrangan |
---|---|
toggle |
Ngalih menu gulung mudhun saka navbar utawa pandhu arah tab. |
show |
Nuduhake menu gulung mudhun navbar utawa pandhu arah tab. |
hide |
Ndhelikake menu gulung mudhun saka navbar utawa pandhu arah tab. |
update |
Nganyari posisi gulung mudhun unsur. |
dispose |
Ngancurake dropdown unsur. (Mbusak data sing disimpen ing unsur DOM) |
getInstance |
Cara statis sing ngidini sampeyan entuk conto dropdown sing ana gandhengane karo unsur DOM, sampeyan bisa nggunakake kaya iki:bootstrap.Dropdown.getInstance(element) |
getOrCreateInstance |
Cara statis sing ngasilake conto dropdown sing digandhengake karo unsur DOM utawa nggawe sing anyar yen ora diwiwiti. Sampeyan bisa nggunakake kaya iki:bootstrap.Dropdown.getOrCreateInstance(element) |
Acara
Kabeh acara gulung mudhun dipecat ing unsur toggling banjur gelembung munggah. Supaya sampeyan uga bisa nambah pamireng acara ing .dropdown-menu
unsur induk 's. hide.bs.dropdown
lan hidden.bs.dropdown
acara duwe clickEvent
properti (mung nalika jinis Acara asli click
) sing ngemot Obyek Acara kanggo acara klik.
Metode | Katrangan |
---|---|
show.bs.dropdown |
Langsung murub nalika metode conto acara diarani. |
shown.bs.dropdown |
Dipecat nalika dropdown wis katon kanggo pangguna lan transisi CSS wis rampung. |
hide.bs.dropdown |
Langsung murub nalika metode conto ndhelikake wis diarani. |
hidden.bs.dropdown |
Dipecat nalika dropdown wis rampung didhelikake saka pangguna lan transisi CSS wis rampung. |
var myDropdown = document.getElementById('myDropdown')
myDropdown.addEventListener('show.bs.dropdown', function () {
// do something...
})