Navbar
Amaxwebhu kunye nemizekelo ye-Bootstrap enamandla, i-header yokukhangela ephendulayo, i-navbar. Iquka inkxaso yokubeka uphawu, ukukhangela, kunye nokunye, kuquka nenkxaso yeplagin yethu yokuwa.
Ingaba isebenza kanjani
Nantsi into ekufuneka uyazi ngaphambi kokuba uqalise nge-navbar:
- Iibhari zeNavbar zifuna ukusonga
.navbar
kunye neeklasi zesikimu sombala esiphendulayo..navbar-expand{-sm|-md|-lg|-xl|-xxl}
- Ii-Navbar kunye nemixholo yazo ilulwelo ngokungagqibekanga. Guqula isikhongozeli sinciphise ububanzi bazo obuthe tye ngeendlela ezahlukeneyo.
- Sebenzisa izithuba zethu kunye neeklasi eziluncedo eziguquguqukayo zokulawula izithuba kunye nolungelelwaniso ngaphakathi kwee-navbar .
- Ii-Navbars ziphendula ngokungagqibekanga, kodwa unokuziguqula ngokulula ukuyitshintsha loo nto. Indlela yokuziphatha ephendulayo ixhomekeke kwiplagi yethu yokuLahla yeJavaScript.
- Qinisekisa ukufikeleleka ngokusebenzisa
<nav>
isiqalelo okanye, ukuba usebenzisa into eqhelekileyo efana ne<div>
, yongeza irole="navigation"
navbar nganye ukuyichonga ngokucacileyo njengommandla obalulekileyo wabasebenzisi boncedo lobuchwephesha. - Bonisa into yangoku ngokusebenzisa
aria-current="page"
iphepha langoku okanyearia-current="true"
into yangoku kwiseti.
prefers-reduced-motion
kumbuzo wemidiya. Jonga icandelo
leentshukumo ezincitshisiweyo kuxwebhu lwethu lofikelelo .
Umxholo oxhaswayo
I-Navbars iza nenkxaso eyakhelwe-ngaphakathi kwiinxalenye ezincinci. Khetha kwezi zilandelayo njengoko kufuneka:
.navbar-brand
yenkampani yakho, imveliso, okanye igama leprojekthi..navbar-nav
kubude obugcweleyo kunye nokukhanya okulula (kubandakanya inkxaso yokwehla)..navbar-toggler
ukuze sisetyenziswe kunye neplagin yethu yokuwa kunye nezinye iindlela zokuziphatha zokuguqula .- Izixhobo zeFlex kunye nesithuba kulo naluphi na uhlobo lolawulo kunye nezenzo.
.navbar-text
yokongeza imitya ethe nkqo embindini wokubhaliweyo..collapse.navbar-collapse
ngokwamaqela kunye nokufihla imixholo ye-navbar nge-breakpoint yomzali.- Yongeza into oyikhethayo
.navbar-scroll
ukusetamax-height
kwaye uskrole umxholo owandisiweyo webar ye navbar .
Nanku umzekelo wawo onke amacandelwana aqukiweyo kwi-navbar enemixholo yokukhanya ephendulayo ethi yonakale ngokuzenzekelayo kwindawo lg
(enkulu) yokwahlukana.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
Lo mzekelo usebenzisa imvelaphi ( bg-light
) kunye nezithuba ( my-2
, my-lg-0
, me-sm-0
, my-sm-0
) iiklasi eziluncedo.
Uphawu
Inokusetyenziswa .navbar-brand
kwizinto ezininzi, kodwa i-ankile isebenza ngcono, njengoko ezinye izinto zinokufuna iiklasi eziluncedo okanye izimbo zesiko.
Isicatshulwa
Yongeza isicatshulwa sakho phakathi kwento ethile .navbar-brand
neklasi.
<!-- As a link -->
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
<!-- As a heading -->
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">Navbar</span>
</div>
</nav>
Umfanekiso
Ungabuyisela okubhaliweyo ngaphakathi .navbar-brand
kwifayile ye <img>
.
<nav class="navbar navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">
<img src="/docs/5.1/assets/brand/bootstrap-logo.svg" alt="" width="30" height="24">
</a>
</div>
</nav>
Umfanekiso kunye nesicatshulwa
Unokusebenzisa ezinye izinto ezongezelelweyo ukongeza umfanekiso kunye nesicatshulwa ngaxeshanye. Qaphela ukongezwa .d-inline-block
kunye .align-text-top
nakwi <img>
.
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="/docs/5.1/assets/brand/bootstrap-logo.svg" alt="" width="30" height="24" class="d-inline-block align-text-top">
Bootstrap
</a>
</div>
</nav>
Nav
Ikhonkco lokujonga kwiNavbar yakhela .nav
kwiinketho zethu ngodidi lwazo lwesilungisi kwaye zifuna ukusetyenziswa kweeklasi ze-toggler kwisitayile esiphendulayo esifanelekileyo. Ukukhangela kwi-navbar kuya kukhula kuthathe indawo ethe tye kangangoko kunokwenzeka ukugcina imixholo yakho ye-navbar ilungelelaniswe ngokukhuselekileyo.
Yongeza .active
iklasi .nav-link
ukubonisa iphepha langoku.
Nceda qaphela ukuba kufuneka udibanise aria-current
uphawu lwesiphumo kokusebenzayo .nav-link
.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
</div>
</div>
</nav>
Kwaye ngenxa yokuba sisebenzisa iiklasi kwii-navs zethu, unokunqanda indlela esekwe kuluhlu ngokupheleleyo ukuba uyathanda.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link active" aria-current="page" href="#">Home</a>
<a class="nav-link" href="#">Features</a>
<a class="nav-link" href="#">Pricing</a>
<a class="nav-link disabled">Disabled</a>
</div>
</div>
</div>
</nav>
Ungasebenzisa kwakhona ukwehla kwi-navbar yakho. Iimenu ezithotyiweyo zifuna into yokusonga yokubeka, ke qiniseka ukuba usebenzisa izinto ezahlukileyo kunye nezifakwe kwindlwane .nav-item
kwaye .nav-link
njengoko kubonisiwe ngezantsi.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown link
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
Iifom
Beka ulawulo lweefomu ezahlukeneyo kunye namalungu ngaphakathi kwe-navbar:
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
Izinto zomntwana zangoku .navbar
zokusetyenziswa uyilo oluguquguqukayo kwaye luya kuhlala lukhona justify-content: space-between
. Sebenzisa izixhobo ezongezelelweyo eziguquguqukayo njengoko kufuneka ukulungisa le ndlela yokuziphatha.
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand">Navbar</a>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
Amaqela ongeniso ayasebenza, nawo. Ukuba i-navbar yakho iyifomu epheleleyo, okanye ubukhulu becala ifom, ungasebenzisa <form>
into njengesikhongozeli kwaye ugcine iHTML ethile.
<nav class="navbar navbar-light bg-light">
<form class="container-fluid">
<div class="input-group">
<span class="input-group-text" id="basic-addon1">@</span>
<input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
</div>
</form>
</nav>
Amaqhosha ahlukeneyo axhaswa njengenxalenye yezi fomu ze-navbar, nazo. Oku kukwasisikhumbuzo esihle sokuba izixhobo zolungelelwaniso ezithe nkqo zinokusetyenziselwa ukulungelelanisa izinto ezinobungakanani obahlukeneyo.
<nav class="navbar navbar-light bg-light">
<form class="container-fluid justify-content-start">
<button class="btn btn-outline-success me-2" type="button">Main button</button>
<button class="btn btn-sm btn-outline-secondary" type="button">Smaller button</button>
</form>
</nav>
Isicatshulwa
Iibar zeNavbar zinokuqulatha amasuntswana okubhaliweyo ngoncedo lwe .navbar-text
. Olu didi luhlengahlengisa ulungelelwaniso oluthe nkqo kunye nezithuba ezithe tye kwimitya yokubhaliweyo.
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<span class="navbar-text">
Navbar text with an inline element
</span>
</div>
</nav>
Hlanganisa kwaye udibanise nezinye izixhobo kunye nezinto eziluncedo njengoko kufuneka.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar w/ text</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
<span class="navbar-text">
Navbar text with an inline element
</span>
</div>
</div>
</nav>
Izikimu zemibala
Ukwenza umxholo kwi-navbar akukaze kube lula ukubulela kwindibaniso yeeklasi ze-theming kunye background-color
nezinto eziluncedo. Khetha phakathi .navbar-light
kokusetyenziswa ngemibala ekhanyayo yangasemva, okanye .navbar-dark
imibala engasemva emnyama. Emva koko, yenza ngokwezifiso kunye .bg-*
nezixhobo.
<nav class="navbar navbar-dark bg-dark">
<!-- Navbar content -->
</nav>
<nav class="navbar navbar-dark bg-primary">
<!-- Navbar content -->
</nav>
<nav class="navbar navbar-light" style="background-color: #e3f2fd;">
<!-- Navbar content -->
</nav>
Izikhongozeli
Nangona kungafunekiyo, ungayisonga i-navbar kwindawo yokuyibeka .container
embindini kwiphepha-nangona qaphela ukuba isikhongozeli sangaphakathi sisafuneka. Okanye unokongeza isikhongozeli ngaphakathi kwindawo .navbar
kuphela imixholo ye navbar emileyo okanye engatshintshiyo .
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
</div>
Sebenzisa nasiphi na isikhongozeli esiphendulayo ukuguqula ukuba banzi umxholo kwi-navbar yakho obonisiweyo.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-md">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
Ukubekwa
Sebenzisa indawo yethu eluncedo ukubeka ii-navbar kwindawo ezingezizo-static. Khetha ukusuka kwisigxina ukuya phezulu, ugxininise ukuya ezantsi, okanye unamathele phezulu (umqulu kunye nephepha uze ufike phezulu, uze uhlale apho). Sebenzisa iinavbar ezizinzileyo position: fixed
, okuthetha ukuba zitsalwa kumsinga oqhelekileyo weDOM kwaye zinokufuna iCSS yesiko (umzekelo, padding-top
kwi <body>
) ukunqanda ukugqithelana nezinye izinto.
Kwakhona qaphela ukuba .sticky-top
isebenzisa position: sticky
, engaxhaswanga ngokupheleleyo kumkhangeli zincwadi ngamnye .
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Default</a>
</div>
</nav>
<nav class="navbar fixed-top navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Fixed top</a>
</div>
</nav>
<nav class="navbar fixed-bottom navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Fixed bottom</a>
</div>
</nav>
<nav class="navbar sticky-top navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Sticky top</a>
</div>
</nav>
Ukuskrola
Yongeza .navbar-nav-scroll
kwi .navbar-nav
(okanye elinye icandelo elisezantsi le-navbar) ukwenza ukuskrola ngokuthe nkqo ngaphakathi kwemixholo eshukunyiswa kwinavbar edilikayo. Ngokungagqibekanga, ukuskrola kukhaba ngaphakathi 75vh
(okanye i-75% yobude bendawo yokujonga), kodwa ungayibhala ngaphezulu loo nto ngepropati yesiko leCSS yasekuhlaleni --bs-navbar-height
okanye izimbo zesiko. Kwiindawo zokujonga ezinkulu xa i-navbar yandisiwe, umxholo uya kuvela njengoko usenza kwi-navbar engagqibekanga.
Nceda uqaphele ukuba olu kuziphatha luza ne-drawback enokubakho ye-- overflow
xa kumiselwa overflow-y: auto
(ifuneka ukuskrola umxholo apha), overflow-x
ilingana auto
, eya kutyala umxholo othe tye.
Nanku umzekelo we navbar usebenzisa .navbar-nav-scroll
nge style="--bs-scroll-height: 100px;"
, kunye nezinto eziluncedo zomda ezongezelelweyo zesithuba esisiso.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar scroll</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarScroll" aria-controls="navbarScroll" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarScroll">
<ul class="navbar-nav me-auto my-2 my-lg-0 navbar-nav-scroll" style="--bs-scroll-height: 100px;">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarScrollingDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Link
</a>
<ul class="dropdown-menu" aria-labelledby="navbarScrollingDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled">Link</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
Ukuziphatha okuphendulayo
Iibar zeNavbar zinokusebenzisa .navbar-toggler
, .navbar-collapse
, kunye .navbar-expand{-sm|-md|-lg|-xl|-xxl}
neeklasi ukumisela xa umxholo wabo uwela emva kweqhosha. Ngokudityaniswa nezinye izinto eziluncedo, unokukhetha ngokulula ixesha lokubonisa okanye ukufihla izinto ezithile.
Kwiinavbar ezingasoze zidilike, yongeza .navbar-expand
iklasi kwi navbar. Kwiinavbar ezihlala zisihla, musa ukongeza naluphi na .navbar-expand
uhlobo.
Toggler
Iziguquli zeNavbar zilungelelaniswe ngokushiyekileyo, kodwa ukuba zilandela into yakowenu njenge .navbar-brand
, ziya kulungelelaniswa ngokuzenzekelayo ekunene. Ukubuyisela umva uphawu lwakho kuya kubuyisela umva ukubekwa kwesiguquli. Ngezantsi yimizekelo yeendlela ezahlukeneyo zokuguqula.
Ngaphandle .navbar-brand
kokuboniswa kweyona ndawo incinci yokuphumla:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<a class="navbar-brand" href="#">Hidden brand</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
Ngegama lebrendi eliboniswe ekhohlo kunye ne-toggler ekunene:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
Ngokuguqula ngasekhohlo kunye negama lebhrendi ekunene:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
Umxholo wangaphandle
Ngamanye amaxesha ufuna ukusebenzisa iplagin yokudilika ukuqalisa into yesikhongozeli somxholo ohleli ngaphandle kwe- .navbar
. Kuba iplagin yethu isebenza id
kwaye data-bs-target
ihambelana, oko kwenziwa ngokulula!
<div class="collapse" id="navbarToggleExternalContent">
<div class="bg-dark p-4">
<h5 class="text-white h4">Collapsed content</h5>
<span class="text-muted">Toggleable via the navbar brand.</span>
</div>
</div>
<nav class="navbar navbar-dark bg-dark">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
Xa usenza oku, sicebisa ukuquka iJavaScript eyongezelelweyo ukuhambisa ugxininiso ngokwenkqubo kwisikhongozeli xa sivulwa. Kungenjalo, abasebenzisi bebhodi yezitshixo kunye nabasebenzisi beetekhnoloji ezincedisayo baya kuba nobunzima bokufumana umxholo osanda kutyhilwa - ngakumbi ukuba isikhongozeli esivuliweyo siza phambi komguquli kwisakhiwo soxwebhu. Sikwacebisa ukuba uqinisekise ukuba umguquli uneempawu, ezikhomba kwisikhongozeli aria-controls
somxholo id
. Ngokwethiyori, oku kuvumela abasebenzisi betekhnoloji abancedisayo ukuba batsibe ngokuthe ngqo ukusuka kwi-toggler ukuya kwisingxobo esisilawulayo-kodwa inkxaso yale nto okwangoku inqabile.
I-offcanvas
Guqula i-navbar yakho eyandayo nedilikayo ibe yidrowa ye-offcanvas eneplagi ye-offcanvas. Sandisa zombini izitayile ezingagqibekanga ze-offcanvas kwaye sisebenzise .navbar-expand-*
iiklasi zethu ukwenza ibar esecaleni eguqukayo neguqukayo.
Kulo mzekelo ungezantsi, ukwenza i-offcanvas navbar ehlala iwile kuzo zonke iindawo zoqhawulo, shiya .navbar-expand-*
iklasi ngokupheleleyo.
<nav class="navbar navbar-light bg-light fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="#">Offcanvas navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasNavbar" aria-labelledby="offcanvasNavbarLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasNavbarLabel">Offcanvas</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<ul class="navbar-nav justify-content-end flex-grow-1 pe-3">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="offcanvasNavbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu" aria-labelledby="offcanvasNavbarDropdown">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</div>
</nav>
Ukwenza i navbar ye offcanvas eyandisa kwi navbar eqhelekileyo kwindawo ethile yoqhawulo lg
, sebenzisa .navbar-expand-lg
.
<nav class="navbar navbar-light navbar-expand-lg bg-light fixed-top">
<a class="navbar-brand" href="#">Offcanvas navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#navbarOffcanvasLg" aria-controls="navbarOffcanvasLg">
<span class="navbar-toggler-icon"></span>
</button>
<div class="offcanvas offcanvas-end" tabindex="-1" id="navbarOffcanvasLg" aria-labelledby="navbarOffcanvasLgLabel">
...
</div>
</nav>
Sass
Izinto eziguquguqukayo
$navbar-padding-y: $spacer * .5;
$navbar-padding-x: null;
$navbar-nav-link-padding-x: .5rem;
$navbar-brand-font-size: $font-size-lg;
// Compute the navbar-brand padding-y so the navbar-brand will have the same height as navbar-text and nav-link
$nav-link-height: $font-size-base * $line-height-base + $nav-link-padding-y * 2;
$navbar-brand-height: $navbar-brand-font-size * $line-height-base;
$navbar-brand-padding-y: ($nav-link-height - $navbar-brand-height) * .5;
$navbar-brand-margin-end: 1rem;
$navbar-toggler-padding-y: .25rem;
$navbar-toggler-padding-x: .75rem;
$navbar-toggler-font-size: $font-size-lg;
$navbar-toggler-border-radius: $btn-border-radius;
$navbar-toggler-focus-width: $btn-focus-width;
$navbar-toggler-transition: box-shadow .15s ease-in-out;
$navbar-dark-color: rgba($white, .55);
$navbar-dark-hover-color: rgba($white, .75);
$navbar-dark-active-color: $white;
$navbar-dark-disabled-color: rgba($white, .25);
$navbar-dark-toggler-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke='#{$navbar-dark-color}' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg>");
$navbar-dark-toggler-border-color: rgba($white, .1);
$navbar-light-color: rgba($black, .55);
$navbar-light-hover-color: rgba($black, .7);
$navbar-light-active-color: rgba($black, .9);
$navbar-light-disabled-color: rgba($black, .3);
$navbar-light-toggler-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke='#{$navbar-light-color}' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg>");
$navbar-light-toggler-border-color: rgba($black, .1);
$navbar-light-brand-color: $navbar-light-active-color;
$navbar-light-brand-hover-color: $navbar-light-active-color;
$navbar-dark-brand-color: $navbar-dark-active-color;
$navbar-dark-brand-hover-color: $navbar-dark-active-color;
Umjikelo
Iiklasi eziphendulayo zenavbar zandisa/zokuwa (umzekelo, .navbar-expand-lg
) zidityaniswe $breakpoints
nemephu kwaye zenziwe nge-loop kwi scss/_navbar.scss
.
// Generate series of `.navbar-expand-*` responsive classes for configuring
// where your navbar collapses.
.navbar-expand {
@each $breakpoint in map-keys($grid-breakpoints) {
$next: breakpoint-next($breakpoint, $grid-breakpoints);
$infix: breakpoint-infix($next, $grid-breakpoints);
// stylelint-disable-next-line scss/selector-no-union-class-name
&#{$infix} {
@include media-breakpoint-up($next) {
flex-wrap: nowrap;
justify-content: flex-start;
.navbar-nav {
flex-direction: row;
.dropdown-menu {
position: absolute;
}
.nav-link {
padding-right: $navbar-nav-link-padding-x;
padding-left: $navbar-nav-link-padding-x;
}
}
.navbar-nav-scroll {
overflow: visible;
}
.navbar-collapse {
display: flex !important; // stylelint-disable-line declaration-no-important
flex-basis: auto;
}
.navbar-toggler {
display: none;
}
.offcanvas-header {
display: none;
}
.offcanvas {
position: inherit;
bottom: 0;
z-index: 1000;
flex-grow: 1;
visibility: visible !important; // stylelint-disable-line declaration-no-important
background-color: transparent;
border-right: 0;
border-left: 0;
@include transition(none);
transform: none;
}
.offcanvas-top,
.offcanvas-bottom {
height: auto;
border-top: 0;
border-bottom: 0;
}
.offcanvas-body {
display: flex;
flex-grow: 0;
padding: 0;
overflow-y: visible;
}
}
}
}
}