Source

Zotsitsa

Sinthani zokulirapo kuti muwonetse mndandanda wamaulalo ndi zina zambiri ndi pulogalamu yowonjezera ya Bootstrap.

Mwachidule

Madontho otsika amatha kusinthika, ndikuwunjikana pazowonetsa mndandanda wamaulalo ndi zina zambiri. Amapangidwa kuti azilumikizana ndi pulogalamu yowonjezera ya JavaScript yophatikizidwa ya Bootstrap. Amasinthidwa ndikudina, osati kungoyang'ana; ichi ndi chisankho chopanga mwadala.

Zotsitsa zimamangidwa palaibulale ya anthu ena, Popper.js , yomwe imapereka mawonekedwe osinthika komanso kuzindikira malo owonera. Onetsetsani kuti mwaphatikiza popper.min.js pamaso pa Bootstrap's JavaScript kapena gwiritsani ntchito bootstrap.bundle.min.js/ bootstrap.bundle.jsyomwe ili ndi Popper.js. Popper.js sichigwiritsidwa ntchito poyika zotsikira mu navbars ngakhale kuti kuyimitsidwa kosunthika sikofunikira.

Ngati mukupanga JavaScript yathu kuchokera kugwero, pamafunikautil.js .

Kufikika

Muyezo wa WAI ARIA umatanthawuza role="menu"widget yeniyeni , koma iyi ndi mindandanda yazantchito yomwe imayambitsa zochita kapena ntchito. Mindandanda ya ARIA imatha kukhala ndi menyu, zinthu zamabokosi, zinthu za batani la wailesi, magulu a batani la wailesi, ndi ma menyu ang'onoang'ono.

Zotsitsa za Bootstrap, kumbali ina, zidapangidwa kuti zikhale zanthawi zonse komanso zogwiritsidwa ntchito pazosiyanasiyana komanso mawonekedwe. Mwachitsanzo, ndizotheka kupanga zotsitsa zomwe zimakhala ndi zowonjezera ndi zowongolera mawonekedwe, monga malo osakira kapena mafomu olowera. Pazifukwa izi, Bootstrap sayembekezera (kapena kungowonjezera) chilichonse rolemwazofunikira pamamenyu aria-enieni a ARIA . Olemba akuyenera kuphatikizirapo izi mwapadera iwo eni.

Komabe, Bootstrap imawonjezera chithandizo chokhazikika pazolumikizana zambiri zamakiyibodi, monga kutha kusuntha pazinthu .dropdown-itemzilizonse pogwiritsa ntchito makiyi a cholozera ndikutseka menyu ndi ESCkiyi.

Zitsanzo

Manga chosinthira chotsitsa (batani kapena ulalo wanu) ndi menyu yotsikira mkati .dropdown, kapena chinthu china chomwe chimalengeza position: relative;. Zotsitsa zitha kuyambika kuchokera <a>kapena <button>zinthu kuti zigwirizane ndi zosowa zanu.

Batani limodzi

Chilichonse .btnchikhoza kusinthidwa kukhala chotsitsa chotsitsa ndi zosintha zina. Umu ndi momwe mungawagwiritsire ntchito ndi <button>zinthu zilizonse:

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

Ndipo ndi <a>zinthu:

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

Gawo labwino kwambiri ndikuti mutha kuchita izi ndi batani lililonse, nanunso:

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

Kugawa batani

Momwemonso, pangani zotsitsa za batani logawanika zokhala ndi chizindikiro chofanana ndi mabatani amodzi, koma ndikuwonjezera .dropdown-toggle-splitmalo oyenera kuzungulira malo otsikira.

Timagwiritsa ntchito kalasi yowonjezerayi kuti tichepetse zopingasa paddingmbali zonse za chisamaliro ndi 25% ndikuchotsa margin-leftzomwe zimawonjezedwa pamakina otsika. Kusintha kowonjezerako kumapangitsa kuti chisamaliro chikhale chokhazikika mu batani logawanika ndikupereka malo ogunda moyenerera pafupi ndi batani lalikulu.

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

Kukula

Mabatani otsikirapo amagwira ntchito ndi mabatani amitundu yonse, kuphatikiza mabatani otsikirapo ndi ogawanika.

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

Mayendedwe

Kusiya

Yambitsani mindandanda yotsikira pamwamba pa zinthu powonjezera .dropupku chinthu cha makolo.

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

Dropright

Yambitsani mindandanda yotsikira kumanja kwa zinthu powonjezera .droprightku chinthu cha makolo.

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

Dropleft

Yambitsani mindandanda yotsikira kumanzere kwa zinthu powonjezera .dropleftku chinthu cha makolo.

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

Zakale zomwe zatsikira pansi zidayenera kukhala maulalo, koma sizili choncho ndi v4. Tsopano mutha kugwiritsa ntchito <button>zinthu pazotsitsa zanu m'malo mwa <a>s basi.

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

Mutha kupanganso zinthu zosagwiritsa ntchito potsitsa ndi .dropdown-item-text. Khalani omasuka kuti muwonjezere mawonekedwe ndi CSS kapena zolemba.

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

Yogwira

Onjezani .activekuzinthu zomwe zili m'munsi kuti ziwoneke ngati zikugwira ntchito .

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

Wolumala

Onjezani .disabledkuzinthu zomwe zili m'munsi kuti muzisanjire ngati zolephereka .

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

Mwachikhazikitso, menyu yotsitsa imayikidwa 100% kuchokera pamwamba ndi kumanzere kwa kholo lake. Onjezani .dropdown-menu-rightku a .dropdown-menukulumikiza kumanja menyu yotsitsa.

Mungodziwiratu! Zotsitsa zimayikidwa chifukwa cha Popper.js (kupatula ngati zili mu 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>

Mitu

Onjezani mutu kuti mulembe magawo a zochita mu menyu yotsikira.

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

Ogawanitsa

Olekanitsa magulu azinthu zokhudzana ndi menyu ndi chogawa.

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

Mawu

Ikani mawu aliwonse aulere mkati mwa menyu otsika omwe ali ndi mawu ndikugwiritsa ntchito masitayilo . Dziwani kuti mudzafunika masitayelo owonjezera kuti muchepetse kukula kwa menyu.

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

Mafomu

Ikani fomu mkati mwa menyu yotsitsa, kapena ipangitseni kukhala menyu yotsitsa, ndipo gwiritsani ntchito maginito kapena ma padding kuti mupatse malo omwe mukufuna.

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

Gwiritsani ntchito data-offsetkapena data-referencekusintha malo otsitsa.

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

Kugwiritsa ntchito

Pogwiritsa ntchito mawonekedwe a data kapena JavaScript, pulogalamu yowonjezera yotsitsa imasintha zobisika (mindandanda yotsikira) posintha .showkalasiyo pamndandanda wa makolo. Lingaliroli data-toggle="dropdown"limadaliridwa potseka mindandanda yazakudya pamlingo wogwiritsa ntchito, ndiye ndibwino kuti muzigwiritsa ntchito nthawi zonse.

Pazida zomwe zimagwira, kutsegula kutsika kumawonjezera zopanda kanthu ( $.noop) zogwirira mouseoverntchito kwa ana omwe ali nawo <body>. Kuthyolako kovomerezeka kumeneku ndikofunikira kuti mugwiritse ntchito movutikira muutumiki wa zochitika za iOS , zomwe zingalepheretse kugunda kulikonse kunja kwa dontho kuti zisayambitse khodi yomwe imatseka kutsitsa. Kutsitsa kukatsekedwa, zowongolera zopanda kanthu mouseoverizi zimachotsedwa.

Kudzera muzochita za data

Onjezani data-toggle="dropdown"ku ulalo kapena batani kuti musinthe kutsitsa.

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

Kudzera pa JavaScript

Imbani zotsitsa kudzera pa JavaScript:

$('.dropdown-toggle').dropdown()
data-toggle="dropdown"zofunikabe

Mosasamala kanthu kuti mumayitanira kutsika kwanu kudzera pa JavaScript kapena m'malo mwake mugwiritse ntchito data-api, data-toggle="dropdown"nthawi zonse imafunika kuti mukhalepo pazoyambitsa zotsitsa.

Zosankha

Zosankha zitha kuperekedwa kudzera pa data kapena JavaScript. Pamawonekedwe a data, yonjezerani dzina lachisankho ku data-, monga mu data-offset="".

Dzina Mtundu Zosasintha Kufotokozera
kuchepetsa nambala | chingwe | ntchito 0 Kuchotsera kwa dontho lotsika poyerekeza ndi chandamale chake. Kuti mumve zambiri onani za Popper.js's offset docs .
tembenuza boolean zoona Lolani Dropdown itembenuke ngati pakhala palimodzi pagawo lolozera. Kuti mumve zambiri onani za Popper.js's flip docs .
malire chingwe | chinthu 'scrollParent' Malire oletsa kusefukira a menyu otsika. Imavomereza mfundo za 'viewport', 'window', 'scrollParent', kapena HTMLElement reference (JavaScript yokha). Kuti mudziwe zambiri onani zolemba za preventOverflow za Popper.js .
umboni chingwe | chinthu 'kusintha' Cholozera cha menyu otsika. Imavomereza mfundo za 'toggle', 'parent', kapena HTMLElement reference. Kuti mudziwe zambiri onani zolemba za Popper.js's referenceObject docs .
chiwonetsero chingwe 'dynamic' Mwachikhazikitso, timagwiritsa ntchito Popper.js poyika ma dynamic positioning. Letsani izi ndi static.

Zindikirani pamene boundaryakhazikitsidwa ku mtengo wina uliwonse kupatulapo 'scrollParent', kalembedwe kameneka position: statickamagwiritsidwa ntchito pa .dropdownchidebecho.

Njira

Njira Kufotokozera
$().dropdown('toggle') Imatembenuza menyu yotsikira ya navbar yopatsidwa kapena kusakatula kwa tabu.
$().dropdown('update') Imawonjezera pomwe pali kutsika kwa chinthu.
$().dropdown('dispose') Imawononga kutsika kwa chinthu.

Zochitika

Zochitika zonse zotsikira zimathamangitsidwa ku .dropdown-menu's kholo element ndipo zimakhala ndi relatedTargetkatundu, zomwe mtengo wake ndi toggling nangula element. hide.bs.dropdownndipo hidden.bs.dropdownzochitika zimakhala ndi clickEventkatundu (pokhapo pamene mtundu wa chochitika choyambirira uli click) chomwe chili ndi Chochitika Chochitika cha chochitikacho.

Chochitika Kufotokozera
show.bs.dropdown Chochitika ichi chimayaka nthawi yomweyo njira yachiwonetsero imatchedwa.
shown.bs.dropdown Chochitikachi chimachotsedwa pamene kutsika kwawonekera kwa wogwiritsa ntchito (kudikira kusintha kwa CSS, kuti kumalize).
hide.bs.dropdown Chochitikachi chimachotsedwa nthawi yomweyo njira yobisala itayitanidwa.
hidden.bs.dropdown Chochitikachi chimachotsedwa pamene dontho latha kubisidwa kwa wogwiritsa ntchito (kudikira kusintha kwa CSS, kuti kumalize).
$('#myDropdown').on('show.bs.dropdown', function () {
  // do something…
})