Bootstrap的Scrollspy根本不工作
问题描述:
我需要帮助设置Bootstrap的Scrollspy。我遵循了文档中的准则,但没有奏效。请帮我找到解决这个问题Bootstrap的Scrollspy根本不工作
我使用的数据在体内元素属性和设置位置相对 这是我的HTML代码:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" />
<link href="https://fonts.googleapis.com/css?family=Fascinate+Inline|Anton|Raleway:200|Megrim" rel="stylesheet">
<style>
body {
height: 100vh;
background: url("https://image.ibb.co/i1Qu1R/Adobe_Stock_123146928.png") no-repeat fixed;
background-size: cover;
position: relative;
top: 121px;
}
.active {
color: black !important;
</style>
<nav id="main-nav" class="nav justify-content-end fixed-top">
<!-- nav-fill nav-justified flex-column flex-md-row-->
<a href="#" class="navbar-brand">marouane <span>rassili</span></a>
<a href="#home" class="nav-link home active">
<!-- flex-md-fill text-md-center"-->Home</a>
<a href="#gallery" class="nav-link gallery">
<!-- flex-md-fill text-md-center"-->Gallery</a>
<a href="#about" class="nav-link about">
<!-- flex-md-fill text-md-center"-->About</a>
<a href="#contact" class="nav-link contact">
<!-- flex-md-fill text-md-center"-->Contact</a>
</nav>
<body data-spy="scroll" data-target="#main-nav">
<section id="home">
<div class="text-center">
<h1>HOME</h1>
</div>
</section>
<section id="gallery">
<div class="text-center">
<h1>GALLERY</h1>
</div>
</section>
<section id="about">
<div class="text-center">
<h1>ABOUT ME</h1>
</div>
</section>
<section id="contact">
<div class="text-center">
<h1>CONTACT ME</h1>
</div>
</section>
(我用Codepen)
答
请更正您的标记以下建议:
- 把
#main-nav
内body
- 为每个部分添加一些内容(或仅通过CSS的高度),以创建一个可滚动的视口。
- import Bootstrap 4需要JS文件来运行scrollspy组件逻辑。
- 右括号添加到
.active
CSS规则 - 使用元素
html
,head
创建HTML标记,并记得关闭body
标签(现代浏览器呈现无效的HTML比较精细,但它鼓励遵循的标记规则和最佳实践避免进一步的问题)
我已经创建了
你可以把它放在我的笔工作实施https://codepen.io/ZorphDark/pen/mBQwYZ的变化codepen:https://codepen.io/mrassili/pen/LzzRxw – mrassili