简体   繁体   English

bootstrap 4 beta scrollspy不起作用

[英]bootstrap 4 beta scrollspy not working

I realize this is a duplicate of this question , but the answer given there isn't using the scrollspy plug in but is instead writing out all of the javascript to highlight active links on scroll. 我意识到这是这个问题的重复,但是给出的答案没有使用scrollspy插件,而是写出了所有JavaScript以突出显示滚动中的活动链接。

I've gone through pretty much all other iterations of this question that I could find but my code looks the same as in the solutions given so I'm not sure of the problem. 我已经经历了该问题的几乎所有其他迭代,但是我的代码看起来与给定的解决方案中的代码相同,因此我不确定这个问题。 (most other questions are also not for bootstrap 4 beta but rather earlier versions) (大多数其他问题也不是针对bootstrap 4 beta,而是早期版本)

I'm using the javascript file from this scrolling nav template to scroll down to a section when you click a link. 单击链接时,我正在使用此滚动导航模板中的javascript文件向下滚动到某个部分。 This works, so the links are all connected to the sections by their ID's properly. 这可行,因此所有链接均通过其ID正确连接到这些部分。

The template's JS file has the JS to activate scrollspy built in, but it's not working for me. 模板的JS文件具有内置的JS来激活scrollspy,但不适用于我。

The body's position is set as relative. 身体的位置设置为相对。 The jQuery.js file is placed before the before the bootstrap.js file. jQuery.js文件放在bootstrap.js文件之前。

I've also tried the method of applying the data-spy and data-target to the body tag. 我还尝试了将data-spydata-target应用于body标签的方法。

I have styled the .active class but before and after doing this, I've inspected my page in chrome, and the .active class is not being applied to the links as you scroll so I don't think the problem is related to CSS . 我已经为.active类设置了样式,但是在此之前和之后,我都在chrome中检查了我的页面,并且在.active类未应用于链接,因此我认为问题与CSS无关。

Here is the javascript: 这是JavaScript:

$('body').scrollspy({
    target: '#mainNav',
    offset: 0
  });

and the code: 和代码:

<nav class="navbar navbar-expand-md navbar-dark fixed-top" id="mainNav" style="margin-bottom:0;padding-bottom:0;">
  <div class="container-fluid">
    <a class="navbar-brand js-scroll-trigger" href="#page-top"><img class="img-fluid" src="backgroundimages/logoSmall.png"></a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarResponsive">
    <ul class="navbar-nav">
      <li class="nav-item navMenuBox">
        <a class="nav-link js-scroll-trigger" href="#howto" style="display:block;">How to Use</a>
      </li>
      <li class="nav-item navMenuBox">
        <a class="nav-link js-scroll-trigger" href="#Mappy" style="display:block;">Map</a>
      </li>
        <li class="nav-item navMenuBox">
        <a class="nav-link js-scroll-trigger" href="#about" style="display:block;">About</a>
      </li>
        <li class="nav-item navMenuBox">
        <a class="nav-link js-scroll-trigger" href="#contact" style="display:block;">Contact</a>
      </li>
    </ul>
  </div>
</div>
</nav>

Based on the information provided, it's difficult to discern where the actual issue is coming from. 根据提供的信息,很难确定实际问题的出处。 My guess is that you're missing a dependency from the scrolling nav template from github, but I could be wrong. 我的猜测是您从github的滚动导航模板中丢失了一个依赖项,但是我可能是错的。

If you haven't included the jQuery.easing from their package.json file, that could be the issue. 如果您尚未从其package.json文件中包含jQuery.easing ,则可能是问题所在。

I've taken the code you were using and put it into a codepen project, and was able to make it work using jQuery 3.2.1 and jQuery-easing 1.4.1 . 我已经使用了您正在使用的代码并将其放入Codepen项目中,并且能够使用jQuery 3.2.1jQuery-easing 1.4.1使其工作。

You can view it here . 您可以在这里查看

SO. 所以。 to fix this I realized that the spyscroll is mainly dependent on having either navbar-dark or navbar-light applied to your navbar . 为了解决这个问题,我意识到spyscroll主要取决于将navbar-darknavbar-light应用于您的navbar It would've been very helpful if this was in the documentation! 如果这在文档中,将是非常有帮助的! Even though I had navbar-dark applied, since I'd assumed the .active state was connected to the nav-links or nav-items , I had styled the nav-links , causing the .active state to not work. 即使我应用了navbar-dark ,由于我假设.active状态已连接到nav-linksnav-items ,所以我对nav-links了样式,导致.active状态不起作用。 To fix my issue I applied my custom styles to the .navbar-dark li a , and my custom active state to 为了解决我的问题,我将自定义样式应用于.navbar-dark li a ,并将我的自定义活动状态应用于

.navbar-dark .navbar-nav .active > .nav-link,
.navbar-dark .navbar-nav .nav-link.show,
.navbar-dark .navbar-nav .nav-link.active

(this was pulled from the bootstrap css, I searched through it to figure out how the .active was being styled & just copy/pasted these classes into my custom CSS document) (这是从引导css中拉出的,我在其中进行了搜索,以找出.active样式,并将这些类复制/粘贴到我的自定义CSS文档中)

Hope this helps someone else with a similar problem!! 希望这可以帮助其他有类似问题的人!

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

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