Source

Mbelata

Gbanwee ihe mkpuchi gburugburu maka igosipụta ndepụta njikọ yana ihe ndị ọzọ na ngwa mgbakwunye Bootstrap.

Nchịkọta

Mbudata bụ mgbanwe mgbanwe, machie gburugburu maka igosipụta ndepụta njikọ yana ndị ọzọ. Ejiri ngwa mgbakwunye Bootstrap dropdown Javascript mekọrịta ha. A na-atụgharị ha site na ịpị, ọ bụghị site na ịnyagharị; nke a bụ mkpebi imewe nke a kpachapụrụ anya.

A na-ewu mkpọda n'ọbá akwụkwọ nke ndị ọzọ, Popper.js , nke na-enye ọnọdụ dị ike na nchọpụta nlele. Jide n'aka na ị gụnyere popper.min.js tupu Bootstrap's JavaScript ma ọ bụ jiri bootstrap.bundle.min.js/ bootstrap.bundle.jsnke nwere Popper.js. A naghị eji Popper.js dobe dropdowns na navbars n'agbanyeghị na ọ dịghị mkpa n'ọkwa dị ike.

Ọ bụrụ na ị na-ewu Javascript anyị site na isi iyi, ọ chọrọutil.js .

Nnweta

Ọkọlọtọ WAI ARIA na -akọwapụta role="menu"wijetị n'ezie , mana nke a bụ kpọmkwem maka ngwa-dị ka ngwa nke na-akpalite omume ma ọ bụ ọrụ. Nchịkọta ARIA nwere ike ịnwe naanị ihe nchịkọta nhọrọ, ihe nchịkọta nhọrọ igbe igbe, ihe nchịkọta nhọrọ bọtịnụ redio, otu bọtịnụ redio, na obere menus.

N'aka nke ọzọ, emebere bootstrap's dropdowns ka ọ bụrụ ọnụ na ọdabara n'ọnọdụ dị iche iche yana nhazi akara. Dịka ọmụmaatụ, ọ ga-ekwe omume ịmepụta mkpọda nwere ntinye agbakwunyere yana njikwa ụdị, dị ka mpaghara ọchụchọ ma ọ bụ ụdị nbanye. N'ihi nke a, Bootstrap anaghị atụ anya (ma ọ bụ tinye ya ozugbo) nke ọ bụla rolena aria-njirimara achọrọ maka ezigbo menus ARIA . Ndị ode akwụkwọ ga-etinyerịrị njirimara ndị a n'onwe ha.

Agbanyeghị, Bootstrap na-agbakwunye nkwado arụnyere n'ime ọtụtụ mmekọrịta menu nke ahụigodo, dị ka ike iji .dropdown-itemigodo cursor mechie menu site na ihe ESCdị iche iche.

Ihe atụ

Kechie ngbanwe nke dropdown (bọtịnụ gị ma ọ bụ njikọ gị) yana menu ndọpụta n'ime .dropdown, ma ọ bụ ihe ọzọ na-ekwupụta position: relative;. Enwere ike ịkpalite mbelata site na <a>ma ọ bụ <button>ihe iji kwado mkpa gị nke ọma.

Mbudata bọtịnụ otu

Enwere .btnike ịtụgharị otu ọ bụla ka ọ bụrụ ntụgharị mkpọda nwere ụfọdụ mgbanwe akara. Nke a bụ otu ị ga-esi tinye ha ka ha jiri <button>ihe abụọ ọ bụla rụọ ọrụ:

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

Na <a>ihe ndị a:

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

Akụkụ kachasị mma bụ na ị nwere ike ime nke a site na iji ụdị bọtịnụ ọ bụla, kwa:

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

Mbelata bọtịnụ gbawara agbawa

N'otu aka ahụ, mepụta mkpọda bọtịnụ nkewa nwere ihe fọrọ nke nta ka ọ bụrụ otu akara dị ka otu ndetu bọtịnụ, mana yana mgbakwunye .dropdown-toggle-splitmaka oghere kwesịrị ekwesị n'akụkụ ebe nchekwa dropdown.

Anyị na-eji klaasị mgbakwunye a iji belata kehoraizin paddingn'akụkụ ọ bụla nke nlekọta ahụ site na 25% wee wepụ margin-leftnke ahụ agbakwunyere maka ndetu bọtịnụ oge niile. Mgbanwe ndị ahụ na-eme ka nlekọta ahụ dabere na bọtịnụ gbawara agbawa wee wepụta mpaghara kụrụ nke ọma karịa n'akụkụ bọtịnụ isi.

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

Nha nha

Mbelata bọtịnụ na-arụ ọrụ na bọtịnụ nha niile, gụnyere ndabara na bọtịnụ mwụda gbawara agbawa.

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

Mwepu mgbanwe

Kpalite menu ndọda n'elu ihe site na ịgbakwunye .dropupna mmewere nne na nna.

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

Ọdịiche dị ala

Kpalite menu ndọda n'aka nri nke ihe ndị ahụ site na ịgbakwunye .droprightna mmewere nne na nna.

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

Mgbanwe mgbanwe

Kpalite menu ndọda n'aka ekpe nke ihe ndị ahụ site na ịgbakwunye .dropleftna mmewere nne na nna.

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

Ọdịnaya menu ndetu nke akụkọ ihe mere eme ga-abụrịrị njikọ, mana nke ahụ abụghịzi ikpe na v4. Ugbu a ị nwere ike iji <button>ihe dị na dropdowns gị na nhọrọ nhọrọ kama naanị <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>

Site na ndabara, a na-edobe menu ndetu ozugbo 100% site na elu yana n'akụkụ aka ekpe nke nne na nna ya. Tinye .dropdown-menu-rightna a .dropdown-menun'aka nri kwado menu ndọpụta.

Welie isi elu! A na-edobe ihe ndọghachi azụ n'ihi Popper.js (belụsọ mgbe ọ dị na 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>

Tinye nkụnye eji isi mee ka aha ngalaba omume na nchịkọta nhọrọ ọ bụla.

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

Kewapụ iche nke ihe nchịkọta nhọrọ nwere nkesa.

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

Tinye mpempe akwụkwọ n'ime menu ndọpụta, ma ọ bụ mee ya ka ọ bụrụ menu ndọpụta, ma jiri oke ma ọ bụ ihe nkpuchi iji nye ya ohere na-adịghị mma ịchọrọ.

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

Ihe nchịkọta nhọrọ na-arụ ọrụ

Tinye .activena ihe ndị dị na ndọpụta iji mee ka ha dị ka ndị na-arụ ọrụ .

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

Ihe nchịkọta nhọrọ agbanyụrụ

Tinye .disabledna ihe ndị dị na ndọpụta iji mee ka ha nwee nkwarụ .

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

Ojiji

Site na njirimara data ma ọ bụ Javascript, ngwa mgbakwunye na-atụgharị ọdịnaya zoro ezo (nchịkọta mkpọda) site na ịtụgharị .showklaasị na ihe ndepụta nne na nna. A data-toggle="dropdown"na-adabere na njirimara a maka imechi menus dropdown na ọkwa ngwa, yabụ ọ dị mma iji ya mgbe niile.

Na ngwaọrụ enyere aka, imepe mkpọda na-agbakwunye ndị na-ahụ maka ihe efu ( $.noop) mouseoverna ụmụaka nke <body>mmemme ahụ. Nke a na-ekweta jọrọ njọ mbanye anataghị ikike dị mkpa na-arụ ọrụ gburugburu a quirk na iOS' event nnọchiteanya , nke ga-ma ọ bụghị gbochie a mgbata n'ebe ọ bụla n'èzí dropdown si ịkpalite koodu na-emechi dropdown. Ozugbo emechiri ndọpụta ahụ, a mouseoverga-ewepụ ndị njikwa efu ndị ọzọ.

Site na njirimara data

Tinye data-toggle="dropdown"na njikọ ma ọ bụ bọtịnụ ka ịgbanwee mkpọda.

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

Site na Javascript

Kpọọ dropdowns site na Javascript:

$('.dropdown-toggle').dropdown()
data-toggle="dropdown"ka achọrọ

N'agbanyeghị ma ị na-akpọ dropdown gị site na Javascript ma ọ bụ kama iji data-api, data-toggle="dropdown"a na-achọrọ mgbe niile ịnọ na ihe mkpalite dropdown.

Nhọrọ

Enwere ike ịfefe nhọrọ site na njirimara data ma ọ bụ Javascript. Maka njirimara data, tinye aha nhọrọ na data-, dị ka ọ dị na data-offset="".

Aha Ụdị Ọdabara Nkọwa
nkwụsị nọmba | eriri | ọrụ 0 Nkwụsị nke ndapụta n'akụkụ ebumnuche ya. Maka ozi ndị ọzọ rụtụ aka na Docs Offset Popper.js .
tụgharịa boolean eziokwu Kwe ka ọ daa ka ọ tụgharịa ma ọ bụrụ na ọ dakọtara na mmewere ntụaka. Maka ozi ndị ọzọ rụtụ aka na docs tụgharịa nke Popper.js .
oke eriri | mmewere 'mpịakọta nne na nna' Oke oke mmachi nke menu ndọpụta. Nabata ụkpụrụ nke 'viewport', 'window', 'scrollParent', ma ọ bụ HTMLElement ntụaka (naanị JavaScript). Maka ozi ndị ọzọ rụtụ aka na Docs preventoverflow nke Popper.js .

Rịba ama mgbe boundaryatọrọ na uru ọ bụla na-abụghị 'scrollParent', a na position: static-etinye ụdị ahụ na .dropdownakpa ahụ.

Ụzọ

Usoro Nkọwa
$().dropdown('toggle') Na-atụgharị menu mwụda nke navbar enyere ma ọ bụ igodo taabụ.
$().dropdown('update') Na-emelite ọnọdụ nke dropdown element.
$().dropdown('dispose') Na-emebi mwepu nke mmewere.

Ihe omume

A na-achụpụ mmemme ndakpọ niile .dropdown-menuna mmewere nne na nna ma nwee relatedTargetakụrụngwa, nke uru ya bụ mmewere arịlịka na-atụgharị.

Ihe omume Nkọwa
show.bs.dropdown Ihe omume a na-agba ọkụ ozugbo a na-akpọ usoro ihe ngosi.
shown.bs.dropdown A na-agbanyụ mmemme a mgbe onye ọrụ mere ka mwepu ahụ hụ ya (ga-echere mgbanwe CSS, ka ọ mechaa).
hide.bs.dropdown A na-agbapụ mmemme a ozugbo a kpọrọ usoro ihe nleba anya.
hidden.bs.dropdown A na-agbapụ mmemme a mgbe ezoro ezoro ya n'aka onye ọrụ dropdown (ga-echere mgbanwe CSS, iji wuchaa).
$('#myDropdown').on('show.bs.dropdown', function () {
  // do something…
})