html - Bootstrap 4 - 如何在不使用 nav 或 list-group 的情况下使 scrollspy 工作?

标签 html css bootstrap-4 scrollspy

有没有办法在不使用 nav 的情况下使 scrollspy 工作?或 list-group

scrollspy 的 Bootstrap 文档声明它只能在 nav 上使用或 list group成分。 DOCUMENTATION

How it works

Scrollspy has a few requirements to function properly:

  • If you’re building our JavaScript from source, it requires util.js.
  • It must be used on a Bootstrap nav component or list group.
  • Scrollspy requires position: relative; on the element you’re spying on, usually the <body>.
  • When spying on elements other than the <body>, be sure to have a height set and overflow-y: scroll; applied.
  • Anchors (<a>) are required and must point to an element with that id.

This question类似,但对于 Bootstrap 3(不是 4),答案是添加 role="tablist" .好吧,它在这里不起作用。 SO 上有很多关于 scrollspy 的问题,但主要针对 Bootstrap 3。

代码:

/*DEMO*/
nav{top:50%;left:1.5rem;transform:translateY(-50%)}
nav a{display:block;width:20px;height:20px;margin-bottom:.5rem}

/*COLORS*/
nav a{background:black}
nav a.active{background:red}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

</head>
<body data-spy="scroll" data-target="#nav" data-offset="0">

<section class="container-fluid">
    <div class="row">
        <div id="item-1" class="col-12 vh-100 bg-primary"></div>
        <div id="item-2" class="col-12 vh-100 bg-warning"></div>
        <div id="item-3" class="col-12 vh-100 bg-danger"></div>
        <div id="item-4" class="col-12 vh-100 bg-success"></div>
        <div id="item-5" class="col-12 vh-100 bg-info"></div>
    </div>
</section>

<nav id="nav" class="d-flex flex-column position-fixed">
    <a href="#item-1"></a>
    <a href="#item-2"></a>
    <a href="#item-3"></a>
    <a href="#item-4"></a>
    <a href="#item-5"></a>
</nav>


<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>

</body>
</html>

最佳答案

您可以通过使用所需的类 list-group-item 并根据需要重置 CSS 来克服此问题:

/*DEMO*/

nav {
  top: 50%;
  left: 1.5rem;
  transform: translateY(-50%)
}

#nav a {
  display: block;
  width: 20px;
  height: 20px;
  margin-bottom: .5rem;
  padding: 0;
  border: none;
  border-radius: 0;
}


/*COLORS*/

#nav a {
  background: black
}

#nav a.active {
  background: red
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

</head>

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

  <section class="container-fluid">
    <div class="row">
      <div id="item-1" class="col-12 vh-100 bg-primary"></div>
      <div id="item-2" class="col-12 vh-100 bg-warning"></div>
      <div id="item-3" class="col-12 vh-100 bg-danger"></div>
      <div id="item-4" class="col-12 vh-100 bg-success"></div>
      <div id="item-5" class="col-12 vh-100 bg-info"></div>
    </div>
  </section>

  <nav id="nav" class="d-flex flex-column position-fixed">
    <a href="#item-1" class="list-group-item"></a>
    <a href="#item-2" class="list-group-item"></a>
    <a href="#item-3" class="list-group-item"></a>
    <a href="#item-4" class="list-group-item"></a>
    <a href="#item-5" class="list-group-item"></a>
  </nav>


  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>

</body>

</html>

关于html - Bootstrap 4 - 如何在不使用 nav 或 list-group 的情况下使 scrollspy 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54681458/

相关文章:

bootstrap-4 - 在 blazor webassembly 应用程序中更新到更新版本的 Bootstrap 和字体的最佳方法

javascript - 如何使用javascript根据变量隐藏/显示div

javascript - 使用自动播放功能使用 HTML5 音频播放电台

javascript - 如何使 toggleClass ("hover") 淡入?

CSS:如何忽略一个元素以便父级的填充将适应其他元素?

javascript - Uncaught TypeError : $(. ..).tooltip 不是 Laravel 应用程序中的函数(Bootstrap 4 预设)

html - bootstrap 4 navbar-toggler-icon(hamburger) 显示两行而不是三行

html - 在表中列出 HTML 表单在语义上是否正确?

html - 主体 {background-color} 在外部 CSS 中不起作用?

html - 使用 CSS 将我页面上的所有内容居中 - 菜单栏、表格等