Source

Scrollspy

Whakahōu aunoatia te whakaterenga Bootstrap, whakarārangihia rānei ngā wāhanga rōpū i runga i te tūnga panuku hei tohu ko tēhea hononga e hohe ana i te tauranga tirohanga.

Pehea te mahi

He torutoru nga whakaritenga a Scrollspy hei mahi tika:

  • Mena kei te hanga koe i to maatau JavaScript mai i te puna, meutil.js .
  • Me whakamahi i runga i te waahanga Bootstrap nav , te roopu rarangi ranei .
  • Ka hiahia a Scrollspy position: relative;ki te huānga e tuteia ana e koe, ko te tikanga ko te <body>.
  • A, no te tutei i runga i huānga atu i te <body>, kia tino ki te whai i te heighthuinga me overflow-y: scroll;te tono.
  • Ko nga punga ( <a>) e hiahiatia ana me tohu ki tetahi huānga me tera id.

Ina oti pai te whakatinanatanga, ka whakahōuhia e to nav me to roopu rarangi ingoa, ka nekehia te .activeakomanga mai i tetahi mea ki tetahi atu i runga i o raatau whaainga.

Tauira i roto i te navbar

Panuku i te waahi kei raro i te pae whakatere ka mataki i te huringa o te karaehe hohe. Ko nga taonga takaiho ka tohua ano.

@ngako

Ad leggings keytar, brunch id toi party dolor laboure. Pitchfork yr enim lo-fi i mua i to ratou hoko atu. Tumblr pāmu-ki-tepu mana pahikara ahakoa. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Ko nga tarau tarau jean koti, williamsburg hoodie minim qui kaore pea koe i rongo mo ena me te cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artistan 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 messenger bag marfa ahakoa he taraka kai. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats kaore pea koe i rongo mo ratou e consequat hoodie gluten-free lo-fi fap aliquip. Whakamahia he waahi i mua i te hokonga atu, ko terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artistan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.

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

Tauira me te nav kohanga

Ka mahi ano a Scrollspy me nga kohanga .navs. Mena he kohanga .nav, .activeka noho ano ona matua .active. Panukuhia te waahi ki te taha o te pae whakaterenga ka matakitaki i te huringa o te akomanga hohe.

Te nama take 1

Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id magna ullamco eu. Do aute ipsum ipsum ullamco cillum consectetur ut et aute consectetur labore. Fugiat laborum incididunt tempor eu consequat enim dolore proident. E kore e taea e koe te mahi i waho i te nui o te mahi. Ko nga mea e kore e taea e koe te tango i te cupidatat no te mea ka taea e koe te tango i te kapu o te kapu.

Te nama take 1-1

Amet tempor mollit aliquip pariatur excepteur commodo do ea cillum commodo Lorem et occaecat elit qui et. Aliquip labore ex ex esse voluptate occaecat Lorem ullamco deserunt. Ko te ahua o te ahua o te ahua o te ahua o te ahua. Noho proident ullamco aute magna pariatur nostrud labore. Reprehenderit aliqua commodo eiusmod aliquip est do duis amet proident magna consectetur consequat eu commodo fugiat non quis. Enim aliquip exercitation ullamco adipisicing voluptate excepteur minim exercitation minim minim commodo adipisicing exercicing officia etahi adipisicing. Anim id duis qui consequat labore adipisicing sint dolor elit cillum anim and fugit.

Te nama take 1-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

Ko te mahi i runga i nga mahi ka taea e koe te noho ki te taha o te taha o te taha o te taha ki runga ki te mahi. Mollit excepteur eu Lorem ipsum anim commodo sint labore Lorem in exercitation velit incididunt. Occaecat consectetur etahi i roto i occaecat proident iti enim sunt reprehenderit exercitation cupidatat and do officia. Whakanuia te mahi ki te mahi mollit ut amet. Noho pai mo te wa poto i roto i te waahi ka taea e koe te noho ki waho.

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

Tauira me te roopu-rarangi

Ka mahi ano a Scrollspy me .list-groups. Panukuhia te waahi ki te taha o te roopu rarangi ka matakitaki i te huringa o te akomanga kaha.

Te nama take 1

Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id magna ullamco eu. Do aute ipsum ipsum ullamco cillum consectetur ut et aute consectetur labore. Fugiat laborum incididunt tempor eu consequat enim dolore proident. E kore e taea e koe te mahi i waho i te nui o te mahi. Ko nga mea e kore e taea e koe te tango i te cupidatat no te mea ka taea e koe te tango i te kapu o te kapu.

Te nama take 2

Ka taea e koe te noho i roto i nga wa katoa i mua i te wa. Ko te mahi mahi kore e taea e koe te mahi ki te mahi. Esse ex consectetur mollit voluptate est in duis laboris ad sit ipsum anim Lorem. Ka taea e koe te whakatau i nga mea e tika ana mo te kore e taea e koe. Ko te mahi mo te wa poto ka taea e koe te mahi i waho o te mahi. Ipsum fugit ex est consectetur ipsum commodo tempor sunt in proident.

Te nama take 3

Ka taea e koe te mahi i tetahi take e mau ana i to whakaaro. 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.

Te nama take 4

Ka taea e koe te mahi i tetahi take e mau ana i to whakaaro. 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.

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

Whakamahinga

Ma nga huanga raraunga

Kia ngawari te taapiri i te whanonga scrollspy ki to whakatere pae runga, taapiri atu data-spy="scroll"ki te huānga e hiahia ana koe ki te tutei (te nuinga ko te <body>). Na ka taapirihia te data-targethuanga me te ID, te karaehe ranei o te huānga matua o tetahi .navwaahanga 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>

Ma te JavaScript

Whai muri i te taapiri position: relative;i to CSS, waeahia te scrollspy ma te JavaScript:

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

Ko nga whaainga ID ka taea te whakatau

Ko nga hononga Navbar me whai tohu id ka taea te whakatau. Hei tauira, <a href="#home">home</a>me rite ki tetahi mea i roto i te DOM rite <div id="home"></div>.

Ko nga huānga kore - :visiblewhaainga i arohia

Ko nga huānga whainga kaore i te :visiblerite ki te jQuery ka kore e arohia, ka kore rawa e miramirahia o raatau taonga nav.

Nga tikanga

.scrollspy('refresh')

Ina whakamahi i te scrollspy me te taapiri, te tango ranei i nga huānga mai i te DOM, me waea koe ki te tikanga whakahou penei:

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

.scrollspy('dispose')

Ka whakangaro i te panuku o tetahi huānga.

Kōwhiringa

Ko nga whiringa ka taea te tuku ma nga huanga raraunga, JavaScript ranei. Mo nga huanga raraunga, apitihia te ingoa whiringa ki data-, penei i te data-offset="".

Ingoa Momo Taunoa Whakaahuatanga
whakatiki tau 10 Pika hei whakatiki mai i runga i te tatau i te waahi o te panuku.
tikanga aho auto Ka kimihia ko tehea tekiona kei roto te huānga torotoro. autoka kowhiria te huarahi tino pai ki te tiki taunga panuku. offsetka whakamahi i te jQuery offset tikanga ki te tiki taunga panuku. positionka whakamahi tikanga tūnga jQuery ki te tiki taunga panuku.
whainga aho Tautuhi huānga ki te tono Scrollspy mono.

Nga huihuinga

Momo Takahanga Whakaahuatanga
activate.bs.scrollspy Ka puhipuhi tenei takahanga ki te huānga panuku i nga wa katoa ka whakahohehia e te scrollspy tetahi mea hou.
$('[data-spy="scroll"]').on('activate.bs.scrollspy', function () {
  // do something...
})