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:
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.
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.
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.
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).
Alaynɛshɔn we de ansa
If yu want fɔ yuz rispɔnsiv alaynɛshɔn, disable dinamik pozishɔn bay we yu ad di data-display="static"atribyut ɛn yuz di rispɔnsiv vɛryushɔn klas dɛn.
Fɔ alaynɛt rayt di drɔpdɔwn mɛnyu wit di brekpɔynt we dɛn gi ɔ big wan, ad .dropdown-menu{-sm|-md|-lg|-xl}-right.
Fɔ alaynɛt di lɛft di drɔpdɔwn mɛnyu wit di brekpɔynt we dɛn gi ɔ big wan, ad .dropdown-menu-rightɛn .dropdown-menu{-sm|-md|-lg|-xl}-left.
Notis se yu nɔ nid fɔ ad data-display="static"atribyut to drɔpdɔwn bɔtin dɛn na navbar, bikɔs dɛn nɔ de yuz Popper.js na navbar.
Di tin dɛn we de insay di menyu
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.
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.
Yu kin yuz JavaSkript fɔ yuz am
Kɔl di drɔpdɔwn dɛn bay JavaSkript:
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('show')
Sho di drɔpdɔwn mɛnyu fɔ wan givɛn navbar ɔ tab nevigishɔn.
$().dropdown('hide')
Ayd 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).