如何在Angular 6中实现bootstrap scrollspy?

时间:2018-09-03 18:35:02

标签: javascript angular twitter-bootstrap bootstrap-4 scrollspy

我的代码在Angular 6和Bootstrap 4上运行。最后,我想在此设置中运行bootstrap的scrollspy组件。 Bootstrap需要jQuery,但我不知道如何使它正常工作。

HTML

<header>
  <div class="header-nav">
    <nav id="home-nav" class="navbar navbar-expand-md navbar-light bg-white fixed-top">
      <a class="navbar-brand" href="#">Dashboard</a>
      <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#navbar_header"
        aria-controls="navbar_header" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbar_header">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#our_mission">Our Mission</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#our_clients">our clients</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#features">features</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#pricing">pricing</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#faqs">Faqs</a>
          </li>
        </ul>
      </div>
    </nav>
  </div>
</header>

<div class="main-content">
  <section id="home">
    <div class="carousel slide" data-ride="carousel">
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img src="../../../assets/landing-page/images/slider-1.jpg" width="100%">
        </div>
        <div class="carousel-item">
          <img src="../../../assets/landing-page/images/slider-1.jpg" width="100%">
        </div>
        <div class="carousel-item">
          <img src="../../../assets/landing-page/images/slider-1.jpg" width="100%">
        </div>
      </div>
    </div>
  </section>
  <section id="our_mission">
    <p>AAAAAAAAAA</p>
  </section>
  <section id="our_clients"></section>
  <section id="features"></section>
  <section id="pricing"></section>
  <section id="faqs"></section>
</div>

TypeScript

ngOnInit() {
  $('.main-content').scrollspy({ target: '#home-nav' });
}

1 个答案:

答案 0 :(得分:0)

尝试使用此代码,如果您在Codepen或stackblitz等网站上构建了一个有效的示例,效果会更好。这将使我们易于发现问题。

<header>
  <div class="header-nav" data-spy="scroll" data-target=".navbar">
    <nav id="home-nav" class="navbar navbar-expand-md navbar-light bg-white fixed-top">
      <a class="navbar-brand" href="#">Dashboard</a>
      <button class="navbar-toggler d-lg-none" type="button" data-toggle="collapse" data-target="#navbar_header"
        aria-controls="navbar_header" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbar_header">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#our_mission">Our Mission</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#our_clients">our clients</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#features">features</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#pricing">pricing</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#faqs">Faqs</a>
          </li>
        </ul>
      </div>
    </nav>
  </div>
</header>

<div class="main-content">
  <section id="home">
    <div class="carousel slide" data-ride="carousel">
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img src="../../../assets/landing-page/images/slider-1.jpg" width="100%">
        </div>
        <div class="carousel-item">
          <img src="../../../assets/landing-page/images/slider-1.jpg" width="100%">
        </div>
        <div class="carousel-item">
          <img src="../../../assets/landing-page/images/slider-1.jpg" width="100%">
        </div>
      </div>
    </div>
  </section>
  <section id="our_mission">
    <p>AAAAAAAAAA</p>
  </section>
  <section id="our_clients"></section>
  <section id="features"></section>
  <section id="pricing"></section>
  <section id="faqs"></section>
</div>