Source

卷轴间谍

根据滚动位置自动更新 Bootstrap 导航或列表组组件,以指示当前在视口中哪个链接处于活动状态。

这个怎么运作

Scrollspy 有一些要求才能正常运行:

  • 如果您从源代码构建我们的 JavaScript,它需要util.js.
  • 它必须用于 Bootstrap导航组件列表组
  • Scrollspy 需要position: relative;您正在监视的元素,通常是<body>.
  • 监视 以外的元素时<body>,请务必height设置并overflow-y: scroll;应用。
  • Anchors( <a>) 是必需的,并且必须指向带有它的元素id

成功实施后,您的导航或列表组将相应更新,根据.active相关目标将类从一项移动到下一项。

导航栏中的示例

滚动导航栏下方的区域并观察活动类的变化。下拉项目也将突出显示。

@胖的

广告紧身裤 keytar,早午餐 id 艺术派对 dolor labe。Pitchfork yr enim lo-fi 在售罄之前 qui。Tumblr 从农场到餐桌的自行车权利。Anim keffiyeh carles 开衫。Velit seitan mcsweeney 的照相亭 3 wolf moon irure。Cosby 毛衣 lomo jean 短裤,williamsburg 连帽衫 minim qui 你可能没听说过它们和开衫信托基金过失生物柴油韦斯安德森美学。Nihil 纹身 accusamus,具有讽刺意味的生物柴油 keffiyeh 工匠 ullamco 后果。

@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 博客,过错邮差包 marfa 不管什么美味的食品卡车。Sapiente 合成器 id 假设。Locavore sed helvetica 陈词滥调具有讽刺意味,您可能没有听说过它们导致连帽衫无麸质 lo-fi fap aliquip。在售罄之前,Labore 曾登上过一席之地,terry Richardson 早午餐 nesciunt quis cosby 毛衣 pariatur keffiyeh ut helvetica artisan。开衫精酿啤酒 seitan 现成的 velit。VHS chambray labouris tempor veniam。Anim mollit minim commodo ullamco 雷猫。

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

嵌套导航示例

Scrollspy 也适用于嵌套.nav的 s。如果一个嵌套.nav.active,它的父母也将是.active。滚动导航栏旁边的区域并观察活动类的变化。

项目 1

Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id magna ullamco eu。做 aute ipsum ipsum ullamco cillum consectetur ut et aute consectetur labore。Fugiat laborum incididunt tempor eu consequat enim dolore proident。Qui labourum do non excepteur nulla magna eiusmod consectetur in. Aliqua et aliqua officia quis et incididunt voluptate non anim reprehenderit adipisicing dolore ut consequat deserunt mollit dolore。Aliquip nulla enim veniam non fugiat id cupidatat nulla elit cupidatat commodo velit ut eiusmod cupidatat elit dolore。

项目 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。Aliqua cillum excepteur irure consequat id quis ea。坐高贵的 ullamco aute magna pariatur nostrud 劳动。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 exercitation officia nisi adipisicing。Anim id duis qui consequat labore adipisicing sint dolor elit cillum anim et fugiat。

项目 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 坐 culpa commodo elit adipisicing 坐 aliquip elit proident voluptate minim mollit nostrud aute reprehenderit do。Mollit excepteur eu Lorem ipsum anim commodo sint labore Lorem in exercitation velit incididunt。Occaecat consectetur nisi in occaecat proident minim enim sunt reprehenderit exercitation cupidatat et do officia。Aliquip consequat ad laboure mollit ut amet。坐在 veniam culpa aliqua excepteur elit magna fugiat eiusmod amet officia 的 pariatur tempor proident。

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

列表组示例

Scrollspy 也适用于.list-groups。滚动列表组旁边的区域并观察活动班级的变化。

项目 1

Ex consequat commodo adipisicing exercitation aute excepteur occaecat ullamco duis aliqua id magna ullamco eu。做 aute ipsum ipsum ullamco cillum consectetur ut et aute consectetur labore。Fugiat laborum incididunt tempor eu consequat enim dolore proident。Qui labourum do non excepteur nulla magna eiusmod consectetur in. Aliqua et aliqua officia quis et incididunt voluptate non anim reprehenderit adipisicing dolore ut consequat deserunt mollit dolore。Aliquip nulla enim veniam non fugiat id cupidatat nulla elit cupidatat commodo velit ut eiusmod cupidatat elit dolore。

第 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 sat ipsum anim Lorem。Inciddunt veniam velit elit elit veniam Lorem aliqua quis ullamco deserunt sat 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。

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

第 4 项

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。

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

用法

通过数据属性

要轻松地将 scrollspy 行为添加到您的顶栏导航,请添加data-spy="scroll"到您要监视的元素(通常是<body>)。然后添加带有任何 Bootstrap组件data-target的父元素的 ID 或类的属性。.nav

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>

通过 JavaScript

添加position: relative;CSS 后,通过 JavaScript 调用 scrollspy:

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

需要可解析的 ID 目标

导航栏链接必须具有可解析的 id 目标。例如,a<a href="#home">home</a>必须对应 DOM 中的某些内容,例如<div id="home"></div>.

:visible目标元素被忽略

:visible不符合 jQuery的目标元素将被忽略,并且它们相应的导航项将永远不会突出显示。

方法

.scrollspy('refresh')

当使用 scrollspy 和从 DOM 中添加或删除元素时,你需要像这样调用 refresh 方法:

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

.scrollspy('dispose')

销毁元素的滚动间谍。

选项

选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 中data-,如data-offset="".

姓名 类型 默认 描述
抵消 数字 10 计算滚动位置时从顶部偏移的像素。

活动

事件类型 描述
激活.bs.scrollspy 每当滚动间谍激活新项目时,此事件就会在滚动元素上触发。
$('[data-spy="scroll"]').on('activate.bs.scrollspy', function () {
  // do something…
})