reactjs - 在 Gatsby 中使用 Bootstrap Scrollspy
问题描述
我正在尝试在 Gatsby JS 应用程序中使用bootstrap scrollspy 。
到目前为止,在我的主容器 div 中,我添加了这个(index.js
):
<div className="main-container" data-spy="scroll" data-target=".navbar" data-offset="50">
<Layout>
<Header />
</div>
然后对于每个部分都有一个 id,假设active
您在滚动到它时添加一个类。但它没有用。
我还尝试在我的主类中添加以下内容:
<div className="main-container" data-spy="scroll" data-target="#nav-scroll-target" data-offset="0">
<Layout>
<Header />
</div>
然后在我的<Navbar/>
组件中,我添加了一个 id 以便可以定位:
<nav id="nav-scroll-target">
</nav>
但这一个也不起作用。知道是什么导致了这个问题吗?
解决方案
你应该有这样的结构:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<style>
body {
position: relative;
}
</style>
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#section1">Section 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section2">Section 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#section3">Section 3</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
Section 4
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#section41">Link 1</a>
<a class="dropdown-item" href="#section42">Link 2</a>
</div>
</li>
</ul>
</nav>
<div id="section1" class="container-fluid bg-success" style="padding-top:70px;padding-bottom:70px">
<h1>Section 1</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section2" class="container-fluid bg-warning" style="padding-top:70px;padding-bottom:70px">
<h1>Section 2</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section3" class="container-fluid bg-secondary" style="padding-top:70px;padding-bottom:70px">
<h1>Section 3</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section41" class="container-fluid bg-danger" style="padding-top:70px;padding-bottom:70px">
<h1>Section 4 Submenu 1</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="section42" class="container-fluid bg-info" style="padding-top:70px;padding-bottom:70px">
<h1>Section 4 Submenu 2</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
</body>
</html>
关键部分是data-target:".navbar"
可滚动元素的 (<body>
在本例中为 tag )和与完全相同的类名 ( <nav class="navbar ...">
) 匹配的 。您需要将此结构调整为您的代码段,将您的更改<nav>
为:
<nav id="nav-scroll-target" className="navbar">
</nav>
推荐阅读
- amazon-web-services - 如何使用 Route53 设置 Amazon MQ RabbitMQ 集群
- apache-kafka - Kafka 集群 - 没有使用一个代理的问题
- amazon-s3 - 是否可以使用 distcp 直接写入最终文件?
- r - 如何获取 CRAN 上所有包的 github repo url?
- c# - WinForms 帮助提供程序。是否可以使用附加参数打开 HTML 文件?
- html - 使用 Postman 从带有 Cheerio 的 HTML 响应中提取值
- javascript - Pendo 集成:注销后匿名访问者
- php - Laravel 作业不更新
- google-cloud-platform - 无法将 GCP 中的虚拟机实例用作 webhook
- r - 如何在不增加行的情况下取消列出 R 和 inner_join 中的对象?