Source

Xiphepherhele xa swiphepherhele

Hi ku tisungulela pfuxeta ku famba-famba ka Bootstrap kumbe ku xaxameta swiphemu swa ntlawa hi ku ya hi xiyimo xa ku rhendzeleka ku kombisa leswaku hi xihi xihlanganisi lexi tirhaka sweswi eka xivono.

Ndlela leyi swi tirhaka ha yona

Scrollspy yi na swilaveko swi nga ri swingani leswaku yi tirha kahle:

  • Loko u aka JavaScript ya hina ku suka eka xihlovo, swi lavautil.js .
  • Yi fanele ku tirhisiwa eka xiphemu xa nav xa Bootstrap kumbe ntlawa wa nxaxamelo .
  • Scrollspy yi lava position: relative;eka elemente leyi u yi hlotaka, hi ntolovelo <body>.
  • Loko u hlota swiaki swin’wana handle ka <body>, tiyiseka leswaku u ni heightsete ni ku overflow-y: scroll;tirhisiwa.
  • Tianchor ( <a>) ta laveka naswona ti fanele ku kombetela eka elemente leyi nga na sweswo id.

Loko yi tirhisiwile hi ndlela leyi humelelaka, ntlawa wa wena wa nav kumbe wa nxaxamelo wu ta pfuxeta hi ku landza sweswo, wu rhurhela .activetlilasi ku suka eka nchumu wun’wana ku ya eka wun’wana hi ku ya hi swikongomelo swa wona leswi fambisanaka na swona.

Xikombiso eka navbar

Skrolela ndhawu leyi nga ehansi ka navbar ivi u hlalela ku cinca ka tlilasi leyi tirhaka. Swilo leswi nga ehansi swi ta kombisiwa na swona.

@mafurha

Ad leggings keytar, brunch id vutshila nkhuvo dolor labore. Pitchfork yr enim lo-fi va nga si xavisa qui. Tumblr timfanelo ta xikanyakanya xa purasi ku ya etafuleni whatever. Anim keffiyeh xitsonga xitsonga xitsonga xitsonga xitsonga. Velit seitan mcsweeney's xifaniso xa xiyindlwana 3 mpfundla n'weti irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui kumbexana a wu se twa hi vona et cardigan nkwama wa vutshembeki culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred xihlekiso biodiesel keffiyeh mutirhi wa mavoko ullamco consequat.

@mdo

Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt.

one

Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone.

two

In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt.

three

Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.

Keytar twee blog, culpa nkwama wa murhumiwa marfa yini na yini delectus lori ya swakudya. Sapiente synth id ya ku ehleketa. Locavore sed helvetica cliche irony, ti thundercats kumbexana a wu si twa hi tona consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat va nga si xavisiwa, terry richardson proident brunch nesciunt quis cosby xisuti pariatur keffiyeh ut helvetica mutirhi wa mavoko. Cardigan craft biya seitan yi lunghekile velit. VHS chambray laboris xitsongwatsongwana xa nkarhinyana. Anim mollit minim commodo ullamco switsongwatsongwana swa ku dzindza ka tilo.

<nav id="navbar-example2" class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav nav-pills">
    <li class="nav-item">
      <a class="nav-link" href="#fat">@fat</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#mdo">@mdo</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
      <div class="dropdown-menu">
        <a class="dropdown-item" href="#one">one</a>
        <a class="dropdown-item" href="#two">two</a>
        <div role="separator" class="dropdown-divider"></div>
        <a class="dropdown-item" href="#three">three</a>
      </div>
    </li>
  </ul>
</nav>
<div data-spy="scroll" data-target="#navbar-example2" data-offset="0">
  <h4 id="fat">@fat</h4>
  <p>...</p>
  <h4 id="mdo">@mdo</h4>
  <p>...</p>
  <h4 id="one">one</h4>
  <p>...</p>
  <h4 id="two">two</h4>
  <p>...</p>
  <h4 id="three">three</h4>
  <p>...</p>
</div>

Xikombiso xa nested nav

Scrollspy yi tlhela yi tirha na nested .navs. Loko nested .navyi ri .active, vatswari va yona na vona va ta va .active. Skrolela ndhawu leyi nga ekusuhi na navbar ivi u hlalela ku cinca ka tlilasi leyi tirhaka.

Nchumu wa 1

Ex consequat commodo adipisicing ku endla vutiolori aute handle ka occaecat ullamco duis aliqua id magna ullamco eu. Endla aute ipsum ipsum ullamco cillum consectetur ut na ku tirha hi ku landzelelana. Fugiat laborum incididunt nkarhinyana eu vuyelo enim dolore proident. qui laborum do non excepteur nulla magna eiusmod consecteturt in. aliqua et aliqua ya fis et incididunt voluptate non anim reprehenderit adipisicing dolore ut consequat deserunt mollit dolore. Aliquip nulla enim veniam non fugiat id xitombo xa xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga

Nchumu wa 1-1

Amet tempor mollit aliquip pariatur handle ka commodo yi endla ea cillum commodo Lorem na occaecat elit qui et. Aliquip ku tirha ex ex esse voluptate occaecat Lorem ullamco ku baleka. Aliqua cillum excepteur irure vuyelo bya xiyimo xa le henhla id quis ea. Tshama proident ullamco aute magna pariatur ku tirha hi nomu. Reprehenderit aliqua commodo eiusmod aliquip est do duis amet proident magna consectetur consequat eu xikweleti lexi nga balekangiki lexi nga riki xinene. Enim aliquip ku endla vutiolori ullamco adipisicing voluptate excepteur ku endla vutiolori lebyintsongo lebyintsongo bya commodo ku endla vutiolori officia nisi adipisicing. Anim id duis qui consequat ku tirha ntirho wo adipisicing sint dolor elit cillum anim na ku baleka.

Nchumu wa 2-2

Cillum nisi deserunt magna eiusmod qui eiusmod velit voluptate pariatur laborum sunt enim. Irure laboris mollit consequat incididunt sint et culpa culpa incididunt adipisicing magna magna occaecat. Nulla ipsum cillum eiusmod sint elit excepteur ea labore enim consectetur in labore anim. Proident ullamco ipsum esse elit ut Lorem eiusmod dolor et eiusmod. Anim occaecat nulla in non consequat eiusmod velit incididunt.

Item 2

Quis magna Lorem anim amet ipsum do mollit sit cillum voluptate ex nulla tempor. Laborum consequat non elit enim exercitation cillum aliqua consequat id aliqua. Esse ex consectetur mollit voluptate est in duis laboris ad sit ipsum anim Lorem. Incididunt veniam velit elit elit veniam Lorem aliqua quis ullamco deserunt sit enim elit aliqua esse irure. Laborum nisi sit est tempor laborum mollit labore officia laborum excepteur commodo non commodo dolor excepteur commodo. Ipsum fugiat ex est consectetur ipsum commodo tempor sunt in proident.

Item 3

Quis anim sit do amet fugiat dolor velit sit ea ea do reprehenderit culpa duis. Nostrud aliqua ipsum fugiat minim proident occaecat excepteur aliquip culpa aute tempor reprehenderit. Deserunt tempor mollit elit ex pariatur dolore velit fugiat mollit culpa irure ullamco est ex ullamco excepteur.

Item 3-1

Deserunt quis elit Lorem eiusmod amet enim enim amet minim Lorem proident nostrud. Ea id dolore anim exercitation aute fugiat labore voluptate cillum do laboris labore. Ex velit exercitation nisi enim labore reprehenderit labore nostrud ut ut. Esse officia sunt duis aliquip ullamco tempor eiusmod deserunt irure nostrud irure. Ullamco proident veniam laboris ea consectetur magna sunt ex exercitation aliquip minim enim culpa occaecat exercitation. Est tempor excepteur aliquip laborum consequat do deserunt laborum esse eiusmod irure proident ipsum esse qui.

Item 3-2

Labore tshama culpa commodo elit adipisicing tshama aliquip elit proident voluptate minim mollit nostrud aute ku sola ku endla. Mollit excepteur eu Lorem ipsum anim commodo sint ntirho Lorem eka ku endla vutiolori velit incididunt. Occaecat consectetur nisi eka occaecat proident minim enim sunt ku sola ku endla vutiolori cupidatat et do officia. Aliquip consequat ad ku tirha ntirho wo tirha mollit ut amet. Tshama pariatur nkarhinyana proident eka veniam culpa aliqua handle ka elit magna fugiat eiusmod amet officia.

<nav id="navbar-example3" class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <nav class="nav nav-pills flex-column">
    <a class="nav-link" href="#item-1">Item 1</a>
    <nav class="nav nav-pills flex-column">
      <a class="nav-link ml-3 my-1" href="#item-1-1">Item 1-1</a>
      <a class="nav-link ml-3 my-1" href="#item-1-2">Item 1-2</a>
    </nav>
    <a class="nav-link" href="#item-2">Item2</a>
    <a class="nav-link" href="#item-3">Item3</a>
    <nav class="nav nav-pills flex-column">
      <a class="nav-link ml-3 my-1" href="#item-3-1">Item 3-1</a>
      <a class="nav-link ml-3 my-1" href="#item-3-2">Item 3-2</a>
    </nav>
  </nav>
</nav>

<div data-spy="scroll" data-target="#navbar-example3" data-offset="0">
  <h4 id="item-1">Item 1</h4>
  <p>...</p>
  <h5 id="item-1-1">Item 1-1</h5>
  <p>...</p>
  <h5 id="item-1-2">Item 2-2</h5>
  <p>...</p>
  <h4 id="item-2">Item 2</h4>
  <p>...</p>
  <h4 id="item-3">Item 3</h4>
  <p>...</p>
  <h5 id="item-3-1">Item 3-1</h5>
  <p>...</p>
  <h5 id="item-3-2">Item 3-2</h5>
  <p>...</p>
</div>

Xikombiso xa list-group

Scrollspy yi tlhela yi tirha na .list-groups. Skrolela ndhawu leyi nga ekusuhi ni ntlawa wa nxaxamelo ivi u hlalela ku cinca ka tlilasi leyi tirhaka.

Nchumu wa 1

Ex consequat commodo adipisicing ku endla vutiolori aute handle ka occaecat ullamco duis aliqua id magna ullamco eu. Endla aute ipsum ipsum ullamco cillum consectetur ut na ku tirha hi ku landzelelana. Fugiat laborum incididunt nkarhinyana eu vuyelo enim dolore proident. qui laborum do non excepteur nulla magna eiusmod consecteturt in. aliqua et aliqua ya fis et incididunt voluptate non anim reprehenderit adipisicing dolore ut consequat deserunt mollit dolore. Aliquip nulla enim veniam non fugiat id xitombo xa xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga xitsonga

Nchumu wa 2

Quis magna Lorem anim amet ipsum endla mollit tshama cillum voluptate ex nulla nkarhinyana. Laborum consequat non elit enim ku endla vutiolori cillum aliqua vuyelo bya id aliqua. Esse ex consectetur mollit voluptate est eka ntirho wa ku tirha ad sit ipsum anim Lorem. Incididunt veniam velit elit elit veniam Lorem aliqua quis ullamco deserunt tshama enim elit aliqua esse irure. Laborum nisi sit est ntirho wa nkarhinyana mollit ntirho officia ntirho excepteur commodo non commodo dolor handle ka commodo. Ipsum fugiat ex est consectetur ipsum commodo nkarhi wo leha sunt eka proident.

Nchumu wa 3

Quis anim tshama do amet fugiat dolor velit tshama ea ea endla ku sola culpa duis. Nostrud aliqua ipsum fugiat minim proident occaecat handle ka xitsongwatsongwana xa xirhendzevutani xa ku sola ka nkarhinyana. Deserunt tempor mollit elit ex pariatur dolore velit ku baleka xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo.

Nchumu wa 4

Quis anim tshama do amet fugiat dolor velit tshama ea ea endla ku sola culpa duis. Nostrud aliqua ipsum fugiat minim proident occaecat handle ka xitsongwatsongwana xa xirhendzevutani xa ku sola ka nkarhinyana. Deserunt tempor mollit elit ex pariatur dolore velit ku baleka xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo xa xitombo.

<div id="list-example" class="list-group">
  <a class="list-group-item list-group-item-action" href="#list-item-1">Item 1</a>
  <a class="list-group-item list-group-item-action" href="#list-item-2">Item2</a>
  <a class="list-group-item list-group-item-action" href="#list-item-3">Item 3</a>
  <a class="list-group-item list-group-item-action" href="#list-item-4">Item 4</a>
</div>
<div data-spy="scroll" data-target="#list-example" data-offset="0" class="scrollspy-example">
  <h4 id="list-item-1">Item 1</h4>
  <p>...</p>
  <h4 id="list-item-2">Item 2</h4>
  <p>...</p>
  <h4 id="list-item-3">Item 3</h4>
  <p>...</p>
  <h4 id="list-item-4">Item 4</h4>
  <p>...</p>
</div>

Matirhiselo

Hi ku tirhisa swihlawulekisi swa data

Ku engetela hi ku olova mahanyelo ya scrollspy eka ku famba-famba ka wena ka topbar, engetela data-spy="scroll"eka elemente leyi u lavaka ku hlota eka yona (hi ntolovelo leswi swi ta va swi ri <body>). Kutani engetela data-targetxihlawulekisi hi ID kumbe tlilasi ya elemente ya mutswari ya xiphemu xihi na xihi .navxa Bootstrap.

body {
  position: relative;
}
<body data-spy="scroll" data-target="#navbar-example">
  ...
  <div id="navbar-example">
    <ul class="nav nav-tabs" role="tablist">
      ...
    </ul>
  </div>
  ...
</body>

Hi ku tirhisa JavaScript

Endzhaku ko engetela position: relative;eka CSS ya wena, vitana scrollspy hi ku tirhisa JavaScript:

$('body').scrollspy({ target: '#navbar-example' })

Swikongomiso swa ID leswi nga lulamisiwaka swa laveka

Swihlanganisi swa Navbar swi fanele ku va na swikongomelo swa id leswi nga lulamisiwaka. Xikombiso, a yi <a href="#home">home</a>fanele ku fambisana na nchumu wo karhi eka DOM ku fana na <div id="home"></div>.

Swiaki leswi nga riki :visibleswa xikongomelo swi honisiwile

Swiaki leswi kongomisiweke leswi nga riki :visiblehi ku ya hi jQuery swi ta honisiwa naswona swilo swa swona swa nav leswi fambelanaka a swi nge pfuki swi kombisiwile.

Maendlelo

.scrollspy('refresh')

Loko u tirhisa scrollspy hi ku fambisana na ku engetela kumbe ku susa swiaki eka DOM, u ta lava ku vitana ndlela yo pfuxeta ku fana na sweswo:

$('[data-spy="scroll"]').each(function () {
  var $spy = $(this).scrollspy('refresh')
})

.scrollspy('dispose')

Ku herisa scrollspy ya elemente.

Swihlawulekisi

Swihlawulekisi swi nga hundzisiwa hi ku tirhisa swihlawulekisi swa data kumbe JavaScript. Eka swihlawulekisi swa datha, engetela vito ra xihlawuhlawu eka data-, tanihi le ka data-offset="".

Vito Muxaka Ku tlula Nhlamuselo
offset nomboro 10. Tipikseli to offset kusuka ehenhla loko u hlayela xiyimo xa scroll.

Swiendlakalo

Muxaka wa Xiendlakalo Nhlamuselo
ku tirhisa.bs.scrollspy Xiendlakalo lexi xi pfurha eka elemente ya scroll nkarhi wun’wana na wun’wana loko nchumu lowuntshwa wu sungula ku tirhisiwa hi scrollspy.
$('[data-spy="scroll"]').on('activate.bs.scrollspy', function () {
  // do something…
})