简体   繁体   English

我无法使scrollspy与我的引导导航栏一起使用

[英]I can't get scrollspy to work with my bootstrap navbar

I know this is a very common problem, but I have spent ~two days crawling the forums, trying fixes, can't seem to find the answer. 我知道这是一个非常常见的问题,但是我花了大约两天时间在各个论坛上进行爬网,尝试解决问题,似乎找不到答案。

I have a standard bootstrap setup that uses some parallax scrolling. 我有一个使用一些视差滚动的标准引导程序设置。 I have a navbar fixed to the bottom of the page. 我的导航栏固定在页面底部。 Each <li> is shaped like a circle, and inside the <li> is an <a> tag. 每个<li>的形状像一个圆形,并且<li>是一个<a>标记。 Each 'circular' <li> is linked to an anchor tag on the page in order to provide navigation around the page. 每个“圆形” <li>都链接到页面上的定位标记,以便提供页面导航。 This is a one-page template set up. 这是一个一页的模板设置。

Here is a fiddle: https://jsfiddle.net/k8g3qydw/1/ 这是一个小提琴: https : //jsfiddle.net/k8g3qydw/1/

I have the bootstrap.css and .js files properly enqueued, I added data-target=".scrollspy" data-spy="scroll" data-offset="0" to the body tag and added the class .scrollspy to the parent element of my <ul> like so: 我已经正确地data-target=".scrollspy" data-spy="scroll" data-offset="0"了bootstrap.css和.js文件,我在body标签中添加了data-target=".scrollspy" data-spy="scroll" data-offset="0"并将类.scrollspy添加到了父级我的<ul>元素如下所示:

<div class="scrollspy board-inner">
    <ul class="nav nav-tabs" id="myTab">
        <div class="liner"></div>
            <li class="nav-item active">

So, I am pretty confused as to what to do! 所以,我对如何做感到很困惑!

Any help is much appreciated! 任何帮助深表感谢!

There are multiple mistakes in your code. 您的代码中存在多个错误。

  1. You should use one method to add Bootstrap ScrollSpy to your page, not both: via Data Attributes or JavaScript. 您应该使用一种方法(通过数据属性 JavaScript)将Bootstrap ScrollSpy添加到页面中,而不是同时添加到页面中。 So you should delete your body attributes or javasript line $('body').scrollSpy({...}); 因此,您应该删除身体属性或javasript行$('body').scrollSpy({...});

  2. ID attribute must be unique to the whole document. ID属性对于整个文档必须是唯一的。 You have repeated IDs on page. 您在页面上有重复的ID。 And.. In wrong place. 和..在错误的地方。 On working fiddle #spy was assigned to nav wrapper, since it is the one which is being spied. 在工作时,#spy被分配给nav包装器,因为它正在被监视。

  3. Navbar links should point to "resolvable id targets" instead of <a name="service"> . <a name="service">链接应指向“可解析的ID目标”,而不是<a name="service"> You should use <a id="service"> . 您应该使用<a id="service">

  4. And also make sure that bootstrap.min.js is included after jQuery in your document. 并且还要确保bootstrap.min.js包含在文档中的jQuery之后。 Since jQuery is required for Bootstrap, not other way. 由于Bootstrap需要jQuery,因此无需其他方法。

Here is a working JSFiddle 这是一个正在工作的JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM