Source

Nwoma mmobɔwee

Yɛ Bootstrap navigation anaa list group components a egyina scroll gyinabea so no foforo ankasa de kyerɛ link a ɛreyɛ adwuma mprempren wɔ viewport no mu.

Sɛnea ɛyɛ adwuma

Scrollspy wɔ ahwehwɛde kakraa bi na ama ayɛ adwuma yiye:

  • Sɛ worekyekye yɛn JavaScript no afi fibea a, ɛhwehwɛ sɛutil.js .
  • Ɛsɛ sɛ wɔde di dwuma wɔ Bootstrap nav component anaa list group so .
  • Scrollspy hwehwɛ position: relative;wɔ element a woreyɛ akwansra so no so, mpɛn pii no <body>.
  • Sɛ woreyɛ akwansrafo wɔ nneɛma afoforo a ɛnyɛ <body>, hwɛ hu sɛ wowɔ heightset na wode adi overflow-y: scroll;dwuma.
  • Ankora ( <a>) ho hia na ɛsɛ sɛ ɛkyerɛ element bi a ɛwɔ saa id.

Sɛ wɔde di dwuma yiye a, wo nav anaa list kuw no bɛsesa sɛnea ɛfata, de .activeadesuakuw no afi ade biako so akɔ foforo so a egyina wɔn botae ahorow a ɛbata ho so.

Nhwɛso wɔ navbar mu

Twe beae a ɛwɔ navbar no ase no na hwɛ adesuakuw a ɛreyɛ adwuma no nsakrae. Wɔbɛtwe adwene asi nneɛma a ɛwɔ ase hɔ no so nso.

@kɛseɛ

Ad leggings keytar, brunch id adwini apontow dolor adwumayɛfo. Pitchfork yr enim lo-fi ansa na wɔretɔn qui. Tumblr afuw-kɔ-pon sakre hokwan ahorow biara. Anim keffiyeh carles a ɔyɛ ɔkwasea. Velit seitan mcsweeney mfonini dan 3 ɔkraman ɔsram irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui ebia wontee wɔn ho asɛm et cardigan ahotoso sikakorabea culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred aseresɛm biodiesel keffiyeh nsaanodwumayɛfo ullamco consequat.

@mdo na ɔkyerɛwee

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 ɔbɔfo bag marfa nea ɛte biara delectus aduan lɔre. Sapiente synth id a ɛkyerɛ sɛ obi yɛ ade. Locavore sed helvetica cliche irony, aprannaa a ebia wontee wɔn ho asɛm consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat ansa na wɔretɔn awie, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica adwumfo. Cardigan nsaanodwuma beer seitan a wɔasiesie velit. VHS chambray adwuma no yɛ bere tiaa mu veniam. Anim mollit minim commodo ullamco aprannaa a ɛrebɔ.

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

Nhwɛso a ɛne nested nav

Scrollspy nso ne nested .navs yɛ adwuma. Sɛ nested bi .navyɛ a .active, n'awofo nso bɛyɛ .active. Twe beae a ɛwɔ navbar no nkyɛn no na hwɛ adesuakuw a ɛreyɛ adwuma no nsakrae.

Adeɛ 1

Ex consequat commodo adipisicing apɔw-mu-teɛteɛ aute excepteur occaecat ullamco duis aliqua id magna ullamco eu. Yɛ aute ipsum ipsum ullamco cillum a wɔde yɛ adwuma no. Fugiat laborum incididunt bere tiaa mu eu nea efi mu ba enim dolore proident. Qui laborum do non excepteur nulla magna eiusmod consectetur in. Aliqua ne nea esii no officia quis ne nea esii no voluptate a ɛnyɛ anim reprehenderit adipisicing dolore ut nea efi mu ba no deserunt mollit dolore. Aliquip nulla enim veniam a ɛnyɛ guankɔbea id cupidat nulla elit cupidatat commodo velit ut eiusmod cupidatat elit dolore.

Asɛm 1-1

Amet bere tiaa mu mollit aliquip pariatur excepteur commodo yɛ ea cillum commodo Lorem ne occaecat elit qui et. Aliquip adwuma ex ex esse voluptate occaecat Lorem ullamco deserunt. Aliqua cillum excepteur a ɛnyɛ nea ɛde ba no id quis ea. Tena ase proident ullamco aute magna pariatur hwene mu adwuma. Reprehenderit aliqua commodo eiusmod aliquip est yɛ duis amet proident magna consectetur a ɛde ba eu commodo guankɔbea a ɛnyɛ quis. Enim aliquip apɔw-mu-teɛteɛ ullamco adipisicing voluptate excepteur apɔw-mu-teɛteɛ ketewaa bi minim commodo adipisicing apɔw-mu-teɛteɛ officia nisi adipisicing. Anim id duis qui consequat adwuma adipisicing bɔne dolor elit cillum anim ne guankɔbea.

Asɛm 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 tena ase culpa commodo elit adipisicing tena ase aliquip elit proident voluptate minim mollit nostrud aute reprehenderit yɛ. Mollit excepteur eu Lorem ipsum anim commodo sint adwuma Lorem wɔ apɔw-mu-teɛteɛ mu velit incididunt. Occaecat consectetur nisi wɔ occaecat proident minim enim sunt reprehenderit apɔw-mu-teɛteɛ cupidat ne yɛ adwuma. Aliquip consequat ad adwuma adwuma mollit ut amet. Tena pariatur bere tiaa mu proident wɔ veniam culpa aliqua mu gye sɛ 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">Item 2</a>
    <a class="nav-link" href="#item-3">Item 3</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>

Nhwɛso a ɛne list-group

Scrollspy nso de .list-groups yɛ adwuma. Twe beae a ɛwɔ list kuw no nkyɛn no na hwɛ adesuakuw a ɛreyɛ adwuma no nsakrae.

Adeɛ 1

Ex consequat commodo adipisicing apɔw-mu-teɛteɛ aute excepteur occaecat ullamco duis aliqua id magna ullamco eu. Yɛ aute ipsum ipsum ullamco cillum a wɔde yɛ adwuma no. Fugiat laborum incididunt bere tiaa mu eu nea efi mu ba enim dolore proident. Qui laborum do non excepteur nulla magna eiusmod consectetur in. Aliqua ne nea esii no officia quis ne nea esii no voluptate a ɛnyɛ anim reprehenderit adipisicing dolore ut nea efi mu ba no deserunt mollit dolore. Aliquip nulla enim veniam a ɛnyɛ guankɔbea id cupidat nulla elit cupidatat commodo velit ut eiusmod cupidatat elit dolore.

Adeɛ 2

Quis magna Lorem anim amet ipsum yɛ mollit tena cillum voluptate ex nulla bere tiaa mu. Laborum consequat non elit enim apɔw-mu-teɛteɛ cillum aliqua nea efi mu ba id aliqua. Esse ex consectetur mollit voluptate est wɔ duis adwumayɛ mu ad sit ipsum anim Lorem. Incididunt veniam velit elit elit veniam Lorem a ɔyɛ ɔkwasea a ɔyɛ ɔkwasea no tena ase a ɔyɛ ɔkwasea a ɔyɛ ɔkwasea. Laborum nisi sit est bere tiaa mu adwuma mollit adwuma officia adwuma a ɛnyɛ commodo non commodo dolor excepteur commodo. Ipsum fugiat ex est consectetur ipsum commodo bere tiaa mu sunt wɔ proident mu.

Adeɛ 3

Quis anim tena ase yɛ amet fugiat dolor velit tena ea ea yɛ reprehenderit culpa duis. Nostrud aliqua ipsum fugiat minim proident occaecat gye sɛ ɛyɛ aduru a ɛma obi ho kyere no. Deserunt tempor mollit elit ex pariatur dolore velit guankɔbea mollit culpa irure ullamco est ex ullamco gye sɛ obi yɛ ɔkwasea.

Adeɛ 4

Quis anim tena ase yɛ amet fugiat dolor velit tena ea ea yɛ reprehenderit culpa duis. Nostrud aliqua ipsum fugiat minim proident occaecat gye sɛ ɛyɛ aduru a ɛma obi ho kyere no. Deserunt tempor mollit elit ex pariatur dolore velit guankɔbea mollit culpa irure ullamco est ex ullamco gye sɛ obi yɛ ɔkwasea.

<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">Item 2</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>

Sɛnea wɔde di dwuma

Via data su ahorow so

Sɛnea ɛbɛyɛ a ɛbɛyɛ mmerɛw sɛ wode scrollspy suban bɛka wo topbar navigation no ho a, fa ka data-spy="scroll"element a wopɛ sɛ wohwɛ so no ho (mpɛn pii no eyi bɛyɛ <body>). Afei fa data-targetattribute no ka ID anaa class a ɛwɔ ɔwofo element a ɛwɔ Bootstrap .navcomponent biara mu no ho.

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>

Ɛdenam JavaScript so

Sɛ wode position: relative;wo CSS no ka ho wie a, frɛ scrollspy no fa JavaScript so:

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

ID botae ahorow a wobetumi asiesie a ɛho hia

Ɛsɛ sɛ Navbar link ahorow no nya id botae ahorow a wobetumi asiesie. Sɛ nhwɛso no, <a href="#home">home</a>ɛsɛ sɛ a ne biribi a ɛwɔ DOM no mu te sɛ <div id="home"></div>.

Wɔabu :visiblewɔn ani agu nneɛma a ɛnyɛ botae so

Wɔbɛbu ani agu target elements a ɛnsɛ :visiblesɛ jQuery kyerɛ no so na wɔrensi wɔn nav nneɛma a ɛne no hyia no so dua da.

Akwan a wɔfa so yɛ

.scrollspy('refresh')

Sɛ wode scrollspy di dwuma de ka nneɛma a wode bɛka ho anaa woayi afi DOM no mu ho a, ɛho behia sɛ wofrɛ refresh kwan no te sɛ saa:

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

.scrollspy('dispose')

Ɔsɛe element bi scrollspy.

Nneɛma a wubetumi apaw

Wobetumi afa data attributes anaa JavaScript so de nneɛma a wobɛpaw no akɔ. Sɛ wopɛ data su ahorow a, fa option din no ka ho data-, sɛnea ɛwɔ data-offset="".

Din Korɔ Mfiaseɛ Nkyerɛmu
offset a wɔde hyɛ mu nɔma 10. 10 Pixels to offset fi soro bere a worebu gyinabea a scroll no.

Nsɛm a esisi

Nsɛm a Ɛsisii no Su Nkyerɛmu
yɛ adwuma.bs.scrollspy Saa adeyɛ yi tow wɔ nhoma mmobɔwee element no so bere biara a ade foforo bi bɛyɛ adwuma denam nhoma mmobɔwee no so.
$('[data-spy="scroll"]').on('activate.bs.scrollspy', function () {
  // do something…
})