Boostrap Scrollspy

Asked

Viewed 281 times

-3

Hello, I am following the documentation of Bootstrap 4.0-beta, and with difficulty to execute a simple "Scrollspy", I would love to learn, following their examples, but it doesn’t work anyway... http://getbootstrap.com/docs/4.0/components/scrollspy/

Could someone give me a done example, from the "Example in navbar" link above, so I can understand the right functioning ? thank you

1 answer

1

The possibility of not working for you is due to lack of one of these requirements:

  • It is necessary util.js (if you compiled the bootstrap.js via source)
  • Should be used with component Nav or the list group component.
  • Requires position: relative; in the element that will 'roll", usually the <body>
  • When scrolling/spiraling to elements that are not in the <body> it will be necessary to apply height and overflow-y: scroll; (for example a sub-element)
  • Links/anchors (<a>) are necessary and should point to an element with the id="..."

Example with navbar (no <body>)

As I said, you must apply the position: relative; in the <body> (in the tests I did was not necessary, but may vary according to DOCTYPE or browser), example:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
   <style>
   body {
      position: relative;
   }
   </style>
</head>

<body data-spy="scroll" data-target="#navbar-example2" data-offset="0">

    <nav id="navbar-example2" class="navbar navbar-light bg-light fixed-top">
      <a class="navbar-brand" href="#">Navbar</a>
      <ul class="nav nav-pills">
        <li class="nav-item">
          <a class="nav-link" href="#guilhermenascimento">@guilhermenascimento</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#bootstrap">@bootstrap</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="#foo">foo</a>
            <a class="dropdown-item" href="#bar">bar</a>
            <div role="separator" class="dropdown-divider"></div>
            <a class="dropdown-item" href="#baz">baz</a>
          </div>
        </li>
      </ul>
    </nav>

    <h4 id="guilhermenascimento">@guilhermenascimento</h4>
    <p>...</p>
    <h4 id="bootstrap">@bootstrap</h4>
    <p>...</p>
    <h4 id="foo">foo</h4>
    <p>...</p>
    <h4 id="bar">bar</h4>
    <p>...</p>
    <h4 id="baz">baz</h4>
    <p>...</p>

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
    <script type="text/javascript" src="http://getbootstrap.com/assets/js/docs.min.js"></script>
</body>
</html>

In a sub-element (<section>)

An example test with fixed height and scroll:

.scrollspy-exemplo {
    position: relative;
    height: 200px;
    margin-top: .5rem;
    overflow: auto;
}
.simular-text-grande {
    height: 400px;
}
<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="#guilhermenascimento">@guilhermenascimento</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#bootstrap">@bootstrap</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="#foo">foo</a>
        <a class="dropdown-item" href="#bar">bar</a>
        <div role="separator" class="dropdown-divider"></div>
        <a class="dropdown-item" href="#baz">baz</a>
      </div>
    </li>
  </ul>
</nav>

<section class="scrollspy-exemplo" data-spy="scroll" data-target="#navbar-example2" data-offset="0">

    <h4 id="guilhermenascimento">@guilhermenascimento</h4>
    <p class="simular-text-grande">...</p>
    <h4 id="bootstrap">@bootstrap</h4>
    <p class="simular-text-grande">...</p>
    <h4 id="foo">foo</h4>
    <p class="simular-text-grande">...</p>
    <h4 id="bar">bar</h4>
    <p class="simular-text-grande">...</p>
    <h4 id="baz">baz</h4>
    <p class="simular-text-grande">...</p>

</section>

<!-- CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">

<!-- JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.