Source

Ba dropdowns ya kokita

Bobongola ba superpositions contextuelles mpo na kolakisa ba liste ya ba liens mpe mingi na plugin ya kokita ya Bootstrap.

Botali ya mozindo

Ba dropdowns ezali toggleable, ba overlays contextuels mpo na kolakisa ba liste ya ba liens mpe mingi mosusu. Bazali kosala interactive na plugin ya JavaScript oyo ezali kokita ya Bootstrap oyo ezali na kati. Bazali ko toggled na ko cliquer, na ko hover te; oyo ezali ekateli ya kosala na mokano.

Ba dropdowns etongami na bibliothèque ya troisième partie, Popper.js , oyo epesaka positionnement dynamique mpe détection ya port de vue. Sala makasi otia popper.min.js liboso ya JavaScript ya Bootstrap to salela bootstrap.bundle.min.js/ bootstrap.bundle.jsoyo ezali na Popper.js. Popper.js esalelamaka te mpo na ko positionner ba dropdowns na ba navbars atako lokola positionnement dynamique esengeli te.

Soki ozali kotonga JavaScript na biso uta na source, esengiutil.js .

Kozala na makoki ya kozwa yango

Norme WAI ARIA elimboli role="menu"widget ya solo , kasi oyo ezali ya sikisiki na ba menu lokola ya bosaleli oyo ebandisaka misala to misala. Ba menu ya ARIA ekoki kozala kaka na biloko ya menu, biloko ya menu ya case ya kotiya elembo, biloko ya menu ya bouton ya radio, bituluku ya bouton ya radio, mpe ba sous-menu.

Ba dropdowns ya Bootstrap, na ngambo mosusu, esalemi mpo na kozala générique mpe ekoki kosalelama na ba situations ndenge na ndenge mpe na ba structures ya marquage. Na ndakisa, ezali na nzela ya kosala ba dropdowns oyo ezali na ba entrées ya kobakisa mpe ba contrôles ya formulaire, lokola ba champs ya recherche to ba formulaires ya login. Mpo na ntina oyo, Bootstrap ezali kozela te (to kobakisa na ndenge ya automatique) moko ya rolempe aria-bizaleli oyo esengeli mpo na ba menu ya solosolo ya ARIA . Bakomi bakosengela kokɔtisa bango moko bizaleli wana ya sikisiki mingi.

Kasi, Bootstrap ebakisi lisungi oyo etongami na kati mpo na mingi ya boyokani ya menu ya klaviatware ya momesano, lokola makoki ya kotambola na kati ya .dropdown-itembiloko mokomoko na kosaleláká bafungola ya kursɛrɛ mpe kokanga menu na ESCfungola.

Bandakisa

Envelopper toggle ya dropdown (bouton to lien na yo) mpe menu ya dropdown na kati ya .dropdown, to élément mosusu oyo esakoli position: relative;. Ba dropdowns ekoki kozala déclenché à partir ya <a>to <button>ba éléments mpo na ko correspondre malamu na ba besoins na yo potentiels.

Ba dropdowns ya bouton moko

Single nyonso .btnekoki kobongwana na toggle ya kokita na mwa mbongwana ya bilembo. Tala ndenge okoki kotia bango na mosala na ba <button>éléments moko to mosusu:

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

Mpe na <a>biloko oyo:

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

Eteni ya malamu ezali ete okoki kosala yango na variante nyonso ya bouton, mpe:

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

Ba déroulants ya bouton ya kokabola

Ndenge moko mpe, sala ba dropdowns ya bouton split na marque presque moko na ba dropdowns ya bouton moko, kasi na kobakisa ya .dropdown-toggle-splitmpo na espacement malamu zingazinga ya caret ya dropdown.

Tosalelaka kelasi oyo ya likolo mpo na kokitisa horizontal paddingna ngambo nyonso mibale ya caret na 25% mpe kolongola margin-leftoyo ebakisami mpo na ba dropdowns ya bouton ya mbala na mbala. Mbongwana wana ya kobakisa ebatelaka caret na katikati na bouton ya kokabola mpe epesaka esika ya kobɛta oyo ezali na bonene oyo ebongi mpenza pembeni ya bouton monene.

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

Kosala bonene ya biloko

Ba boutons oyo ezali kokita esalaka na ba boutons ya bonene nyonso, bakisa mpe ba boutons oyo ezali kokita mpe oyo ekabwani.

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

Variation ya dropup

Déclenchez ba menu déroulant likolo ya ba éléments na kobakisa .dropupna élément parent.

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

Bokeseni ya ba dropright

Déclenchez ba menu déroulant na droite ya ba éléments na kobakisa .droprightna élément parent.

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

Bokeseni ya dropleft

Déclenchez ba menu déroulant na gauche ya ba éléments na kobakisa .dropleftna élément parent.

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

Historiquement ba contenus ya menu déroulant esengelaki ezala ba liens, mais ezali lisusu bongo te na v4. Sikoyo okoki kosalela optionnellement ba <button>éléments na ba dropdowns na yo au lieu ya kaka <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>

Na ndenge ya libela, menu oyo ezali kokita ezali kotyama automatiquement 100% kobanda na likoló mpe na ngámbo ya lobɔkɔ ya mwasi ya moboti na yango. Bakisa .dropdown-menu-rightna a .dropdown-menuna droite aligner menu oyo ezali kokita.

Mitó likoló! Ba dropdowns e positionné grâce na Popper.js (sauf soki ezali na kati ya 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>

Bakisa motó mpo na kotya bilembo na biteni ya misala na menu nyonso oyo ezali kokita.

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

Kabola bituluku ya biloko ya menu oyo ezali na boyokani na yango na nzela ya kokabola.

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

Tia formulaire moko na kati ya menu oyo ezali kokita, to sala yango na menu oyo ezali kokita, mpe salelá ba utilitaires ya marge to ya padding mpo na kopesa yango esika ya mabe oyo osengeli na yango.

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

Biloko ya menu oyo ezali kosala

Bakisa .activena biloko oyo ezali na dropdown mpo na kosala style na yango lokola active .

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

Biloko ya menu oyo ekangami

Bakisa .disabledna biloko na dropdown mpo na kosala style na yango lokola désactivé .

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

Kosalela yango

Na nzela ya ba attributs ya ba données to JavaScript, plugin oyo ezali kokita ebalusaka makambo oyo ebombami (ba menu oyo ezali kokita) na kobongola .showkelasi na eloko ya liste ya baboti. Attribut data-toggle="dropdown"etie motema mpo na kokanga ba menu ya kokita na niveau ya application, yango wana ezali likanisi malamu kosalela yango ntango nyonso.

Na ba appareils oyo ekoki ko toucher, kofungola dropdown ebakisi ba handlers ya pamba ( $.noop) na bana ya mbala moko ya élément. This admittedly ugly hack is necessary to work around a quirk in iOS’ event delegation , oyo soki te elingaki kopekisa robinet esika nyonso libanda ya dropdown e déclencher code oyo ekanga dropdown. Soki kaka dropdown ekangami, ba handlers oyo ya pamba ya kobakisa elongolamaka.mouseover<body>mouseover

Na nzela ya ba attributs ya ba données

Bakisa data-toggle="dropdown"na lien to na bouton mpo na kobongola esika oyo ezali kokita.

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

Na nzela ya JavaScript

Benga ba dropdowns na nzela ya JavaScript:

$('.dropdown-toggle').dropdown()
data-toggle="dropdown"esɛngami naino

Indépendamment soki obengi dropdown na yo na nzela ya JavaScript to na esika na yango kosalela data-api, data-toggle="dropdown"esengeli ntango nyonso kozala présent na élément déclencheur ya dropdown.

Makambo oyo okoki kopona

Ba options ekoki koleka na nzela ya ba attributs ya ba données to JavaScript. Mpo na ba attributs ya ba données, bakisa kombo ya option na data-, lokola na data-offset="".

Nkombo Lolenge Mbeba Ndimbola
offset ya kosala motango | molongo ya molongo | mosala 0. Offset ya dropdown par rapport na cible na yango. Mpo na koyeba makambo mingi tala Docs ya offset ya Popper.js .
kobalusa boolean ya solo Tika Dropdown ebalusa na cas ya superposition na élément ya référence. Mpo na koyeba makambo mingi tala na flip docs ya Popper.js .
ndelo ya mboka molongo ya molongo | eloko 'moboti ya défilement'. Frontière ya contrainte ya débordement ya menu oyo ezali kokita. Endimi motuya ya 'viewport', 'window', 'scrollParent', to référence ya HTMLElement (JavaScript kaka). Mpo na koyeba makambo mingi tala ba docs ya preventOverflow ya Popper.js .

Yeba ntango boundaryetiamaki na motuya mosusu longola se 'scrollParent', lolenge position: staticesalelami na eloko ya .dropdownkobomba.

Ba méthodes ya kosala

Metode Ndimbola
$().dropdown('toggle') Ezali kobongola menu oyo ezali kokita ya navbar to navigation oyo ezali na onglet oyo epesami.
$().dropdown('update') Ezali kosala mikolo oyo position ya dropdown ya élément moko.
$().dropdown('dispose') Ebebisaka dropdown ya élément moko.

Makambo oyo esalemaki

Ba événements nionso ya dropdown ezo betama na .dropdown-menuélément parent ya 's pe eza na relatedTargetpropriété, oyo valeur na yango eza élément anchor toggling.

Likambo Ndimbola
show.bs.dropdown Evenement oyo ezo beta mbala moko tango méthode ya instance ya kolakisa ebengami.
shown.bs.dropdown Evenement oyo ezo beta tango dropdown esalemi komonana na mosaleli (ekozela ba transitions ya CSS, mpo esila).
hide.bs.dropdown Evenement oyo ezo beta mbala moko tango méthode ya instance ya kobomba ebengami.
hidden.bs.dropdown Evenement oyo ezo beta tango dropdown esili kobombama na mosaleli (ekozela ba transitions ya CSS, mpo esila).
$('#myDropdown').on('show.bs.dropdown', function () {
  // do something…
})