Source

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 .navbarkunye neeklasi zesikimu sombala esiphendulayo..navbar-expand{-sm|-md|-lg|-xl}
  • Ii-Navbar kunye nemixholo yazo ilulwelo ngokungagqibekanga. Sebenzisa izikhongozeli ozikhethelayo ukunciphisa ububanzi bazo obuthe tye.
  • 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.
  • Iibar zeNavbar zifihlwa ngokungagqibekanga xa kushicilelwa. Nyanzelela ukuba zishicilelwe ngokongeza kwifayile .d-printye .navbar. Bona iklasi eluncedo yokubonisa .
  • Qinisekisa ukufikeleleka ngokusebenzisa <nav>isiqalelo okanye, ukuba usebenzisa into eqhelekileyo efana ne <div>, yongeza i role="navigation"navbar nganye ukuyichonga ngokucacileyo njengommandla obalulekileyo wabasebenzisi boncedo lobuchwephesha.

Funda ngokomzekelo kunye noluhlu lwamacandelwana axhaswayo.

Umxholo oxhaswayo

I-Navbars iza nenkxaso eyakhelwe-ngaphakathi kwiinxalenye ezincinci. Khetha kwezi zilandelayo njengoko kufuneka:

  • .navbar-brandyenkampani yakho, imveliso, okanye igama leprojekthi.
  • .navbar-navkubude obugcweleyo kunye nokukhanya okulula (kubandakanya inkxaso yokwehla).
  • .navbar-togglerukuze sisetyenziswe kunye neplagin yethu yokuwa kunye nezinye iindlela zokuziphatha zokuguqula .
  • .form-inlinekulo naluphi na uhlobo lolawulo kunye nezenzo.
  • .navbar-textyokongeza imitya ethe nkqo embindini wokubhaliweyo.
  • .collapse.navbar-collapsengokwamaqela kunye nokufihla imixholo ye-navbar nge-breakpoint yomzali.

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">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

Lo mzekelo usebenzisa umbala ( bg-light) kunye nezithuba ( my-2, my-lg-0, mr-sm-0, my-sm-0) iiklasi eziluncedo.

Uphawu

Inokusetyenziswa .navbar-brandkwizinto ezininzi, kodwa i-ankile isebenza ngcono njengoko ezinye izinto zinokufuna iiklasi eziluncedo okanye izimbo zesiko.

<!-- As a link -->
<nav class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
</nav>

<!-- As a heading -->
<nav class="navbar navbar-light bg-light">
  <span class="navbar-brand mb-0 h1">Navbar</span>
</nav>

Ukongeza imifanekiso .navbar-brandkuya kuhlala kufuna izitayile eziqhelekileyo okanye izinto eziluncedo kubungakanani obufanelekileyo. Nantsi eminye imizekelo yokubonisa.

<!-- Just an image -->
<nav class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">
    <img src="/docs/4.1/assets/brand/bootstrap-solid.svg" width="30" height="30" alt="">
  </a>
</nav>
<!-- Image and text -->
<nav class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">
    <img src="/docs/4.1/assets/brand/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="">
    Bootstrap
  </a>
</nav>

Ikhonkco lokujonga kwiNavbar yakhela .navkwiinketho 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.

Iimeko ezisebenzayo-nge- .activeukubonisa iphepha langoku linokusetyenziswa ngokuthe ngqo ku- .nav-links okanye umzali wabo osondeleyo .nav-item.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></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" href="#">Disabled</a>
      </li>
    </ul>
  </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">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
      <a class="nav-item nav-link" href="#">Features</a>
      <a class="nav-item nav-link" href="#">Pricing</a>
      <a class="nav-item nav-link disabled" href="#">Disabled</a>
    </div>
  </div>
</nav>

Unokusebenzisa kwakhona ukwehla kwi-navbar nav yakho. Iimenu ezithotyiweyo zifuna into yokusonga yokubeka, ke qiniseka ukuba usebenzisa izinto ezahlukileyo kunye nezifakwe kwindlwane .nav-itemkwaye .nav-linknjengoko kubonisiwe ngezantsi.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></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-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <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>
      </li>
    </ul>
  </div>
</nav>

Iifom

Beka ulawulo lweefomu ezahlukeneyo kunye namalungu ngaphakathi kwebar ye-navbar nge .form-inline.

<nav class="navbar navbar-light bg-light">
  <form class="form-inline">
    <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  </form>
</nav>

Izinto zangoku zabantwana .navbarekusetyenzisweni koyilo oluguquguqukayo kwaye ziya kuhlala justify-content: between. Sebenzisa izixhobo ezongezelelweyo eziguquguqukayo njengoko kufuneka ukulungisa le ndlela yokuziphatha.

<nav class="navbar navbar-light bg-light">
  <a class="navbar-brand">Navbar</a>
  <form class="form-inline">
    <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  </form>
</nav>

Amaqela ongeniso ayasebenza, nawo:

<nav class="navbar navbar-light bg-light">
  <form class="form-inline">
    <div class="input-group">
      <div class="input-group-prepend">
        <span class="input-group-text" id="basic-addon1">@</span>
      </div>
      <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="form-inline">
    <button class="btn btn-outline-success" 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">
  <span class="navbar-text">
    Navbar text with an inline element
  </span>
</nav>

Hlanganisa kwaye udibanise nezinye izixhobo kunye nezinto eziluncedo njengoko kufuneka.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar w/ text</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></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>
</nav>

Izikimu zemibala

Ukwenza umxholo kwi-navbar akukaze kube lula ukubulela kwindibaniso yeeklasi ze-theming kunye background-colornezinto eziluncedo. Khetha phakathi .navbar-lightkokusetyenziswa ngemibala ekhanyayo yangasemva, okanye .navbar-darkimibala 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, ungasonga i navbar kwi .containerukuyibeka embindini kwiphepha okanye wongeze enye phakathi kwiziko kuphela imixholo ye navbar emileyo okanye engatshintshiyo ephezulu .

<div class="container">
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
  </nav>
</div>

Xa isikhongozeli singaphakathi kwe-navbar yakho, i-padding yayo ethe tye iyasuswa kwiindawo zokuphumla ezisezantsi kunodidi lwakho oluxeliweyo .navbar-expand{-sm|-md|-lg|-xl}. Oku kuqinisekisa ukuba asiphindwa kabini ekukhuselweni ngokungeyomfuneko kwiindawo zokujonga ezisezantsi xa i-navbar yakho iwile.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container">
    <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-topkwi <body>) ukunqanda ukugqithelana nezinye izinto.

Kwakhona qaphela ukuba .sticky-topisebenzisa position: sticky, engaxhaswanga ngokupheleleyo kumkhangeli zincwadi ngamnye .

<nav class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">Default</a>
</nav>
<nav class="navbar fixed-top navbar-light bg-light">
  <a class="navbar-brand" href="#">Fixed top</a>
</nav>
<nav class="navbar fixed-bottom navbar-light bg-light">
  <a class="navbar-brand" href="#">Fixed bottom</a>
</nav>
<nav class="navbar sticky-top navbar-light bg-light">
  <a class="navbar-brand" href="#">Sticky top</a>
</nav>

Ukuziphatha okuphendulayo

Iibar zeNavbar zinokusebenzisa .navbar-toggler, .navbar-collapse, kunye .navbar-expand{-sm|-md|-lg|-xl}neeklasi ukutshintsha xa umxholo wabo usihla emva kweqhosha. Ngokudityaniswa nezinye izinto eziluncedo, unokukhetha ngokulula ixesha lokubonisa okanye ukufihla izinto ezithile.

Kwiinavbar ezingasoze zidilike, yongeza .navbar-expandiklasi kwi navbar. Kwiinavbar ezihlala zisihla, musa ukongeza naluphi na .navbar-expanduhlobo.

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-brandkokuboniswa kweyona ndawo iphantsi yoqhawulo:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 mr-auto mt-2 mt-lg-0">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

Ngegama lebrendi eliboniswe ekhohlo kunye ne-toggler ekunene:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 mr-auto mt-2 mt-lg-0">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

Ngokuguqula ngasekhohlo kunye negama lebhrendi ekunene:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 mr-auto mt-2 mt-lg-0">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

Umxholo wangaphandle

Ngamanye amaxesha ufuna ukusebenzisa iplagin yokuwa ukuze uqalise umxholo ofihliweyo kwenye indawo kwiphepha. Kuba iplagin yethu isebenza idkwaye data-targetihambelana, oko kwenziwa ngokulula!

<div class="pos-f-t">
  <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">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
  </nav>
</div>