Source

Ibha ye-Navbar

Amadokhumenti nezibonelo zesihloko sokuzulazula esinamandla, esisabelayo se-Bootstrap, ibha ye-navbar. Kufaka usekelo lwebhrendi, ukuzulazula, nokunye okwengeziwe, okuhlanganisa usekelo lwe-plugin yethu yokugoqa.

Isebenza kanjani

Nakhu okudingeka ukwazi ngaphambi kokuthi uqale nge-navbar:

  • Ama-Navbar adinga ukugoqwa ukuze aphendule ukugoqa .navbarnamakilasi esikimu sombala ..navbar-expand{-sm|-md|-lg|-xl}
  • Ama-Navbar nokuqukethwe kwawo kuwuketshezi ngokuzenzakalelayo. Sebenzisa iziqukathi ozikhethela ukuze ukhawulele ububanzi bazo obuvundlile.
  • Sebenzisa izikhala zethu kanye namakilasi ezinsiza eziguquguqukayo ukuze ulawule isikhala nokuqondanisa ngaphakathi kwama-navbar.
  • Ama-Navbar aphendula ngokuzenzakalelayo, kodwa ungakwazi ukuwashintsha kalula ukuze ashintshe lokho. Ukuziphatha okusabelayo kuncike ku-plugin yethu ye-Collapse JavaScript.
  • Ama-Navbar afihlwa ngokuzenzakalelayo lapho uphrinta. Phoqa ukuthi aphrintwe ngokungeza kufayela .d-printle- .navbar. Bona ikilasi lensiza yokubonisa .
  • Qinisekisa ukufinyeleleka ngokusebenzisa i- <nav>elementi noma, uma usebenzisa i-elementi evamile njenge- <div>, engeza i- role="navigation"navbar ngayinye ukuze uyikhombe ngokusobala njengesifunda esiyingqopha-mlando kubasebenzisi bobuchwepheshe obusizayo.

Qhubeka ufunde ukuze uthole isibonelo kanye nohlu lwezingxenye ezincane ezisekelwayo.

Okuqukethwe okusekelwe

Ama-Navbar eza nosekelo olwakhelwe ngaphakathi lwezingxenye ezincane ezimbalwa. Khetha kokulandelayo njengoba kudingeka:

  • .navbar-brandinkampani yakho, umkhiqizo, noma igama lephrojekthi.
  • .navbar-navukuze uthole ukuzulazula okugcwele ubude nokungasindi (okuhlanganisa nosekelo lokwehliswayo).
  • .navbar-togglerukuze sisetshenziswe ne-plugin yethu yokugoqa nokunye ukuziphatha kokuguqula indlela yokuzulazula.
  • .form-inlinenganoma yiziphi izilawuli nezenzo zefomu.
  • .navbar-textyokwengeza amayunithi ezinhlamvu aphakathi nendawo aqondile.
  • .collapse.navbar-collapseukuze uqoqe futhi ufihle okuqukethwe kwe-navbar ngephuzu lomzali.

Nasi isibonelo sazo zonke izingxenye ezingaphansi ezifakwe ku-navbar enetimu yokukhanya esabelayo egoqa ngokuzenzakalelayo endaweni lgyokuphumula (enkulu).

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

Lesi sibonelo sisebenzisa umbala ( bg-light) kanye nesikhala ( my-2, my-lg-0, mr-sm-0, my-sm-0) amakilasi awusizo.

Ibhrendi

I- .navbar-brandelekthronikhi ingasetshenziswa kuma-elementi amaningi, kodwa ihange lisebenza kangcono njengoba ezinye izici zingase zidinge amakilasi osizo noma izitayela zangokwezifiso.

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

Ukwengeza izithombe kuhlelo .navbar-brandcishe kuzodinga izitayela zangokwezifiso noma izinsiza ukuze usayizi ofanele. Nazi ezinye izibonelo zokubonisa.

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

Izixhumanisi zokuzulazula ze-Navbar zakhela phezu .navkwezinketho zethu ngesigaba sazo sokuguqula futhi zidinga ukusetshenziswa kwamakilasi e-toggler ukuthola isitayela esifanele sokuphendula. Ukuzulazula kuma-navbar nakho kuzokhula kuthathe indawo evundlile ngangokunokwenzeka ukuze ugcine okuqukethwe kwakho kwe-navbar kuhambisana ngokuphephile.

Izimo ezisebenzayo-nge .active--ukukhombisa ikhasi lamanje kungasetshenziswa ngokuqondile ku- .nav-links noma kubazali babo abaseduze .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>

Futhi ngenxa yokuthi sisebenzisa amakilasi kuma-navs ethu, ungagwema indlela esuselwe ohlwini ngokuphelele uma uthanda.

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

Ungasebenzisa futhi okwehliswayo ku-navbar yakho nav. Amamenyu okudonsela phansi adinga into yokugoqa ukuze ibekwe, ngakho-ke qiniseka ukuthi usebenzisa izinto ezihlukene nezibekwe esidlekeni .nav-itemfuthi .nav-linknjengoba kukhonjisiwe ngezansi.

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

Amafomu

Beka izilawuli zamafomu ahlukahlukene kanye nezingxenye ngaphakathi kwe-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 ezisheshayo zezingane .navbarekusetshenzisweni kwesakhiwo esiguqukayo futhi zizozenzakalela zibe justify-content: between. Sebenzisa izinsiza ezengeziwe eziguquguqukayo njengoba kudingeka ukuze ulungise lokhu kuziphatha.

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

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

Izinkinobho ezahlukahlukene zisekelwa njengengxenye yalawa mafomu e-navbar, nawo. Lesi futhi isikhumbuzo esihle sokuthi izinsiza zokuqondanisa okume mpo zingasetshenziswa ukuqondisa izinto ezinosayizi abahlukahlukene.

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

Umbhalo

Ama-Navbar angase aqukathe izingcezu zombhalo ngosizo lwe- .navbar-text. Leli klasi lilungisa ukuqondanisa okume mpo kanye nesikhala esivundlile seyunithi yezinhlamvu zombhalo.

<nav class="navbar navbar-light bg-light">
  <span class="navbar-text">
    Navbar text with an inline element
  </span>
</nav>

Hlanganisa futhi uhlanganise nezinye izingxenye kanye nezinsiza njengoba kudingeka.

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

Ukwenza i-navbar akukaze kube lula ngenxa yenhlanganisela yamakilasi etimu background-colornezinsiza. Khetha kuzo .navbar-lightukuze usebenzise ngemibala engemuva ekhanyayo, noma .navbar-darkngemibala engemuva emnyama. Bese, yenza ngendlela oyifisayo .bg-*nezinsiza.

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

Izitsha

Nakuba kungadingeki, ungakwazi ukugoqa ibha ye-navbar .containerukuze uyibeke maphakathi nekhasi noma wengeze eyodwa phakathi ukuze ubeke phakathi kuphela okuqukethwe kwe- navbar ephezulu emile noma engashintshi .

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

Uma isiqukathi singaphakathi kwe-navbar yakho, i-padding yaso evundlile iyasuswa ezindaweni zokuphumula ezingaphansi .navbar-expand{-sm|-md|-lg|-xl}kwesigaba sakho esicacisiwe. Lokhu kuqinisekisa ukuthi asiphindaphindi kabili ekuphedeni kungenasidingo ezimbobeni zokubuka eziphansi uma ibha yakho ye-navbar igoqiwe.

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

Ukubekwa

Sebenzisa izinsiza zethu zesikhundla ukuze ubeke ama-navbar ezindaweni ezingezona ezimile. Khetha kusukela kokugxilile kuye phezulu, okugxilile kuye phansi, noma kunamathele phezulu (skrola ngekhasi lize lifike phezulu, bese lihlala lapho). Ama-navbar angashintshi asebenzisa position: fixed, okusho ukuthi adonswa ekugelezeni okuvamile kwe-DOM futhi angase adinge i-CSS yangokwezifiso (isb., padding-topku- <body>) ukuze kuvinjelwe ukugqagqana nezinye izici.

Futhi qaphela ukuthi .sticky-topisebenzisa position: sticky, okungasekelwe ngokugcwele kuso sonke isiphequluli .

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

Ama-Navbar angasebenzisa .navbar-toggler, .navbar-collapse, kanye .navbar-expand{-sm|-md|-lg|-xl}namakilasi ukuze ashintshe lapho okuqukethwe kwawo kugoqeka ngemva kwenkinobho. Ngokuhambisana nezinye izinsiza, ungakhetha kalula ukuthi uzibonisa nini noma uzifihle nini izinto ezithile.

Kuma-navbar angawi, engeza .navbar-expandiklasi ku-navbar. Kuma-navbar ahlala egoqa, ungangezi noma yisiphi .navbar-expandisigaba.

I-Toggler

Iziguquli zebha ye-Navbar ziqondaniswe kwesokunxele ngokuzenzakalelayo, kodwa uma zilandela i-elementi eyizelamani njenge- , zizoqondaniswa .navbar-brandngokuzenzakalelayo kwesokudla. Ukubuyisela emuva imakhaphu yakho kuzohlehlisa ukubekwa kwesiguquli. Ngezansi kunezibonelo zezitayela ezahlukene zokuguqula.

Ngaphandle .navbar-brandkokuboniswa kuphoyinti eliphansi kakhulu:

<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 lomkhiqizo eliboniswa kwesokunxele kanye ne-toggler kwesokudla:

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

Nge-toggler kwesokunxele kanye negama lomkhiqizo kwesokudla:

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

Okuqukethwe kwangaphandle

Kwesinye isikhathi ufuna ukusebenzisa i-plugin yokugoqa ukuze uqalise okuqukethwe okufihliwe kwenye indawo ekhasini. Ngoba i-plugin yethu isebenza ekufaniseni idnasekufaniseni data-target, lokho kwenziwa kalula!

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