Source

Drɔpdɔwn dɛn

Tɔgl kɔntɛkstual ɔvlay fɔ sho list dɛn fɔ link dɛn ɛn mɔ wit di Bootstrap drɔpdɔwn plɔgin.

Ɔvaviu fɔ di Baybul

Drɔpdɔwn dɛn kin chenj, kɔntɛkstual ɔvlay fɔ sho list dɛn fɔ link dɛn ɛn ɔda tin dɛn. Dɛn mek dɛn intaraktiv wit di Bootstrap drɔpdɔwn JavaSkript plɔgin we de insay. Dɛn kin toggl dɛn bay we dɛn de klik, nɔto bay we dɛn de hov; dis na wan intenshɔnal dizayn disizhɔn.

Dɛn bil drɔpdɔwn dɛn pan wan tɔd pati laybri, Popper.js , we de gi dinamik pozishɔn ɛn viupɔt ditekshɔn. Mek shɔ se yu put popper.min.js bifo Bootstrap in JavaSkript ɔ yuz bootstrap.bundle.min.js/ bootstrap.bundle.jswe gɛt Popper.js. Popper.js nɔ de yuz fɔ posishun drɔpdɔwn dɛn na navbar dɛn pan ɔl we as dinamik pozishɔn nɔ nid.

If yu de bil wi JavaSkript frɔm sɔs, i nid fɔutil.js .

Di we aw pɔsin kin ebul fɔ go de

Di WAI ARIA standad de difayn wan aktual role="menu"widget , bɔt dis na spɛshal to aplikeshɔn-layk mɛnyu dɛn we de trig akshɔn ɔ fɛnshɔn dɛn. ARIA mɛnyu dɛn kin jɔs gɛt mɛnyu aytem dɛn, chɛkbɔks mɛnyu aytem dɛn, redio bɔtin mɛnyu aytem dɛn, redio bɔtin grup dɛn, ɛn sab-mɛnyu dɛn.

Bootstrap in drɔpdɔwn dɛn, na di ɔda say, dɛn mek am fɔ bi jenɛrik ɛn fɔ yuz fɔ difrɛn sityueshɔn ɛn mak strɔkchɔ dɛn. Fɔ ɛgzampul, i pɔsibul fɔ mek drɔpdɔwn dɛn we gɛt ɔda tin dɛn fɔ put ɛn fɔm kɔntrol dɛn, lɛk say dɛn fɔ fɛn ɔ fɔm dɛn fɔ login. Fɔ dis rizin, Bootstrap nɔ de ɛkspɛkt (nɔ ɔtomɛtik ad) ɛni wan pan di roleɛn aria-atribyut dɛn we nid fɔ tru ARIA mɛnyu dɛn. Di wan dɛn we rayt di buk go gɛt fɔ put dɛn mɔ spɛshal atribyut dɛn ya dɛnsɛf.

Bɔt, Bootstrap de ad bilt-in sɔpɔt fɔ mɔs standad kibɔd mɛnyu intarakshɔn dɛn, lɛk di ebul fɔ muv tru wan wan .dropdown-itemɛlimɛnt dɛn yuz di kɔsa ki dɛn ɛn lɔk di mɛnyu wit di ESCki.

Ɛgzampul dɛn

Rap di drɔpdɔwn in tɔgl (yu bɔtin ɔ link) ɛn di drɔpdɔwn mɛnyu insay .dropdown, ɔ ɔda ɛlimɛnt we de diklar position: relative;. Drɔpdɔwn kin trig frɔm <a>ɔ <button>ɛlimɛnt fɔ bɛtɛ fit yu pɔtɛnɛshɛl nid dɛm.

Wan bɔtin

Ɛni singl .btnkin tɔn to drɔpdɔwn tɔgl wit sɔm mak chenj dɛn. Na dis na aw yu kin put dɛn fɔ wok wit ɛni wan pan di <button>ɛlimɛnt dɛn:

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

Ɛn wit <a>ɛlimɛnt dɛn:

<div class="dropdown">
  <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>

Di bes pat na yu kin du dis wit eni button variant, tu:

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

Di bɔtin fɔ split

Semweso, mek split bɔtin drɔpdɔwn dɛn wit klos to di sem mak lɛk wan bɔtin drɔpdɔwn dɛn, bɔt wit di addɛshɔn fɔ .dropdown-toggle-splitfɔ mek dɛn spes fayn fayn wan rawnd di drɔpdɔwn karet.

Wi de yuz dis ekstra klas fɔ ridyus di ɔrizɔntal paddingna ɛni say na di karet bay 25% ɛn pul di margin-leftdat we dɛn ad fɔ rɛgyula bɔtin drɔpdɔwn dɛn. Dɛn ɛkstra chenj dɛn de kin mek di karet de na di sɛnt na di split bɔtin ɛn dɛn kin gi yu wan say fɔ hit we gɛt mɔ saiz nia di men bɔtin.

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

Sayz fɔ di sayz

Bɔtin drɔpdɔwn dɛn de wok wit bɔtin dɛn we gɛt ɔl kayn saiz, ivin di difɔlt ɛn split drɔpdɔwn bɔtin dɛn.

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

Aw fɔ sho aw fɔ du am

Drop-ap fɔ de

Trig drɔpdɔwn mɛnyu dɛn ɔp di ɛlimɛnt dɛn bay we yu ad .dropupto di mama ɛn papa ɛlimɛnt.

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

Drɔprayt

Trig drɔpdɔwn mɛnyu dɛn na di rayt say na di ɛlimɛnt dɛn bay we yu ad .droprightto di mama ɛn papa ɛlimɛnt.

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

Drɔplɛft fɔ di wan dɛn we de drɔp

Trig drɔpdɔwn mɛnyu dɛn na di lɛft say na di ɛlimɛnt dɛn bay we yu ad .dropleftto di mama ɛn papa ɛlimɛnt.

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

Historically dropdown menu contents bin gɛt fɔ bi link, bɔt dat nɔ de igen wit v4. Naw yu kin opshɔnali yuz <button>ɛlimɛnt dɛn na yu drɔpdɔwn dɛn instead fɔ jɔs <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>

Yu kin mek bak tin dɛn we nɔ de intaraktiv drɔpdɔwn wit .dropdown-item-text. Fil fri fɔ stayl mɔ wit kɔstɔm CSS ɔ tɛks yutiliti dɛn.

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

Wok

Ad .activeto tin dɛn we de na di drɔpdɔwn fɔ mek dɛn stayl dɛn as aktif .

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

Disebul

Ad .disabledto tin dɛn na di drɔpdɔwn fɔ stayl dɛn as disabled .

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

Bay difɔlt, wan drɔp-dɔwn mɛnyu de ɔtomɛtik wan 100% frɔm di ɔp ɛn along di lɛft say na in mama ɔ papa. Ad .dropdown-menu-rightto a .dropdown-menuto rayt alaynɛt di drɔpdɔwn mɛnyu.

Hed dɛn de ɔp! Drɔpdɔwn dɛn de pozishɔn tank to Popper.js (eksept we dɛn de insay wan 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>

Di ed dɛn we de na di ed

Ad wan ɛda fɔ lɛbul sɛkshɔn dɛn fɔ di akshɔn dɛn na ɛni drɔpdɔwn mɛnyu.

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

Divayda dɛn

Separet grup dɛn fɔ di tin dɛn we gɛt fɔ du wit di mɛnyu wit wan divayda.

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

Tɛks

Put ɛni frifɔm tɛks insay wan drɔpdɔwn mɛnyu wit tɛks ɛn yuz spɛshal yutiliti dɛn . Notis se yu go mɔs nid ɔda stayl dɛn fɔ mek yu nɔ gɛt sayz fɔ mek di meny nɔ big.

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

Fɔm dɛn

Put wan fɔm insay wan drɔpdɔwn mɛnyu, ɔ mek am insay wan drɔpdɔwn mɛnyu, ɛn yuz margin ɔ padding utilities fɔ gi am di negatif spes we yu nid.

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

Yuz data-offsetɔ data-referencefɔ chenj di say we di drɔp-dɔwn de.

<div class="d-flex">
  <div class="dropdown mr-1">
    <button type="button" class="btn btn-secondary dropdown-toggle" id="dropdownMenuOffset" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-offset="10,20">
      Offset
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuOffset">
      <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>
  <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-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-reference="parent">
      <span class="sr-only">Toggle Dropdown</span>
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuReference">
      <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>
</div>

Aw fɔ yuz am

Via data atribyut ɔ JavaSkript, di drɔpdɔwn plɔgin de chenj di tin dɛn we dɛn ayd (drɔpdɔwn mɛnyu dɛn) bay we i de chenj di .showklas na di mama ɛn papa list aytem. Dɛn data-toggle="dropdown"kin abop pan di atribyut fɔ lɔk drɔpdɔwn mɛnyu dɛn na wan aplikeshɔn lɛvɛl, so i fayn fɔ yuz am ɔltɛm.

Na divays dɛn we gɛt tɔch, we yu opin wan drɔpdɔwn, i de ad ɛmti ( $.noop) mouseoverhandla dɛn to di pikin dɛn we de nia di <body>ɛlimɛnt. Dis admittedly ugly hack na nid fɔ wok arawnd wan quirk in iOS’ event delegation , we ɔda we go mek wan tap ɛnisay ausayd di drɔpdɔwn nɔ trig di kɔd we de lɔk di drɔpdɔwn. We dɛn dɔn lɔk di drɔp-dɔwn, dɛn kin pul dɛn ɔda ɛmti mouseoverhandla dɛn ya.

Via data atribyut dɛn

Ad data-toggle="dropdown"to link ɔ bɔtin fɔ chenj wan drɔp-dɔwn.

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

Yu kin yuz JavaSkript fɔ yuz am

Kɔl di drɔpdɔwn dɛn bay JavaSkript:

$('.dropdown-toggle').dropdown()
data-toggle="dropdown"stil nid fɔ de

I nɔ mata if yu kɔl yu drɔpdɔwn bay JavaSkript ɔ insted yu yuz di data-api, data-toggle="dropdown"i nid fɔ de ɔltɛm na di drɔpdɔwn in trig ɛlimɛnt.

Di tin dɛn we yu kin pik fɔ du

Yu kin pas opshɔn dɛn bay we yu yuz data atribyut ɔ JavaSkript. Fɔ data atribyut dɛn, ad di opshɔn nem to data-, lɛk insay data-offset="".

Nem Kayn Balans Tɔk bɔt
ɔfset nɔmba | string fɔ di wan dɛn we de | wok 0. Di wan dɛn we de Ɔfset fɔ di drɔpdɔwn rilitiv to in target. Fɔ no mɔ, luk na Popper.js in ɔfset dɔkyumɛnt dɛn .
tɔn oba boolean we dɛn kɔl tru Alaw Drɔpdɔwn fɔ flip if wan ɔvalap pan di rɛfrɛns ɛlimɛnt. Fɔ no mɔ, luk na Popper.js in flip docs .
bɔda string fɔ di wan dɛn we de | tin 'skrolParent'. Ovaflɔ kɔnstrakshɔn bɔda fɔ di drɔpdɔwn mɛnyu. Aksept di valyu dɛn fɔ 'viewport', 'window', 'scrollParent', ɔ wan HTMLElement rɛfrɛns (JavaSkript nɔmɔ). Fɔ no mɔ, luk na Popper.js in preventOverflow docs .
tɔk bɔt string fɔ di wan dɛn we de | tin 'tɔgl'. Rifrɛns ɛlimɛnt na di drɔpdɔwn mɛnyu. Aksept di valyu dɛn fɔ 'toggle', 'parent', ɔ wan HTMLElement rɛfrɛns. Fɔ no mɔ, luk na Popper.js in referenceObject docs .
sho rop 'daynamik'. Bay difɔlt, wi de yuz Popper.js fɔ dinamik pozishɔn. Disable dis wit static.

Notis we boundarydɛn sɛt am to ɛni valyu ɔda pas 'scrollParent', di stayl position: staticde aplay to di .dropdownkɔntena.

Di we aw dɛn de du am

We Tɔk bɔt
$().dropdown('toggle') Tɔgl di drɔpdɔwn mɛnyu fɔ wan givɛn navbar ɔ tab nevigishɔn.
$().dropdown('update') Ɔpdet di pozishɔn fɔ wan ɛlimɛnt in drɔpdɔwn.
$().dropdown('dispose') Destroy wan element in drɔpdɔwn.

Di tin dɛn we kin apin

Ɔl di drɔpdɔwn ivin dɛn de faya pan di .dropdown-menu's mama ɛn papa ɛlimɛnt ɛn dɛn gɛt relatedTargetprɔpati, we in valyu na di tɔgl ankɔ ɛlimɛnt. hide.bs.dropdownɛn hidden.bs.dropdownivin dɛn gɛt clickEventprɔpati (onli we di ɔrijinal ivin tayp na click) we gɛt wan Ivint Ɔbjɛkt fɔ di klik ivin.

Program Tɔk bɔt
show.bs.dropdown Dis ivent de faya wantɛm wantɛm we dɛn kɔl di sho instans mɛtɔd.
shown.bs.dropdown Dis ivin de faya we dɛn dɔn mek di drɔpdɔwn si di pɔsin we de yuz am (go wet fɔ CSS transishɔn dɛn, fɔ dɔn).
hide.bs.dropdown Dis ivent de faya wantɛm wantɛm we dɛn dɔn kɔl di ayd instans mɛtɔd.
hidden.bs.dropdown Dis ivent de faya we di drɔpdɔwn dɔn dɔn fɔ ayd frɔm di yuza (go wet fɔ CSS transishɔn dɛn, fɔ dɔn).
$('#myDropdown').on('show.bs.dropdown', function () {
  // do something…
})