简体   繁体   English

在Bootstrap 4.1中添加平滑滚动(同时使用scrollspy)?

[英]Adding smooth scrolling to bootstrap 4.1 (While using scrollspy)?

So what i'm trying to do over here is to use the bootstrap 4.1s scrollspy function in my navbar. 因此,我在此处尝试做的是在我的导航栏中使用bootstrap 4.1s scrollspy函数。 I've managed to get that to work. 我设法使它起作用。 However I would also like to add smooth scrolling to it. 但是,我还要添加平滑的滚动。 After searching over the whole internet I still haven't managed to fix it. 在整个互联网上搜索后,我仍然没有设法修复它。

Here's the original code that i am working with (same as from bootstrap). 这是我正在使用的原始代码(与引导程序相同)。

<nav id="navbar-example2" class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav nav-pills">
    <li class="nav-item">
      <a class="nav-link" href="#fat">@fat</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#mdo">@mdo</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
      <div class="dropdown-menu">
        <a class="dropdown-item" href="#one">one</a>
        <a class="dropdown-item" href="#two">two</a>
        <div role="separator" class="dropdown-divider"></div>
        <a class="dropdown-item" href="#three">three</a>
      </div>
    </li>
  </ul>
</nav>
<div data-spy="scroll" data-target="#navbar-example2" data-offset="0">
  <h4 id="fat">@fat</h4>
  <p>...</p>
  <h4 id="mdo">@mdo</h4>
  <p>...</p>
  <h4 id="one">one</h4>
  <p>...</p>
  <h4 id="two">two</h4>
  <p>...</p>
  <h4 id="three">three</h4>
  <p>...</p>
</div>

And here's what i've tried to add to enable smooth scrolling. 这就是我尝试添加以实现平滑滚动的内容。

      <script>
          $(document).ready(function() {
    $(".nav-item").click(function() {
        var t = $(this).attr("href");
        $('.active').removeClass('active');
        $("html, body").animate({
            scrollTop: $(t).offset().top - 50
        }, {
            duration: 1e3,

        });

        $('body').scrollspy({ target: '#navbar-example2',offset: $(t).offset().top });
        $(this).parent().addClass('active');





        return false;
    });

});



//navbar
var distance = $('#site-header').offset().top,
    $window = $(window);

$window.scroll(function() {
    if ( $window.scrollTop() >= distance ) {
        $('#site-header').addClass('fixed-nav')
    }
    if ( $window.scrollTop() <= distance ) {
        $('#site-header').removeClass('fixed-nav')
    }
});


$('body').scrollspy({ target: '#navbar-example2',offset: 0 });
        </script>

As of now I feel stuck and I don't understand what it is i am doing wrong. 截至目前,我感到自己被卡住了,我不明白自己在做什么错。 Because I can't make it work. 因为我无法使它工作。 I've also tried adding different variations of css:s but I can't make it work. 我也尝试添加不同的css:s变体,但无法使其正常工作。 Any help would be appriciated. 任何帮助将被申请。

Change the jQuery selector for the click handler to... 将点击处理程序的jQuery选择器更改为...

$(".nav-link,.dropdown-item").click(function() {
  ...
});

https://www.codeply.com/go/y0LweX2FBY https://www.codeply.com/go/y0LweX2FBY

The problem with the nav-link selector is that it doesn't have an href so the click handler isn't doing anything. nav-link选择器的问题在于它没有href,因此点击处理程序没有做任何事情。 Also, you probably want the Navbar fixed-top so that it doesn't scroll away when navigating to a section. 另外,您可能希望将Navbar fixed-top以便在导航到某个部分时不会滚动。

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

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