title: Angular 6: How to highlight element on navbar depending on scroll?
body: <p>I am making a single page in Angular 6. It a simple page and I would like to highlight <code>navbar</code> depending on scroll.</p>
  
  <p>here is my code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.sticky {
    position: sticky;
    top: 0;
  }
  #i ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: blue;
  }
  
  #i li {
    float: left;
  }
  
  #i li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
  }
  
  #i li a:hover {
    border-radius: 0px 0px 10px 10px;
    background-color: rgb(43, 137, 226);
  }
  
  /* #i {
    margin: 0px 0px 40px 0px;
  } */
  
  #footer-id {
    background-color:blue;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;span id="i"&gt;
    &lt;ul class="sticky"&gt;
      &lt;li id="i"&gt;&lt;a class="active" href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#news" (click)="scrollToElement(new)"&gt;News&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#contact" (click)="scrollToElement(con)"&gt;Contact&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#about" (click)="scrollToElement(about)"&gt;About&lt;/a&gt;&lt;/li&gt;
  
      &lt;ul class="nav navbar-nav pull-right"&gt;
        &lt;li class="nav"&gt;&lt;a href="#"&gt;Contact&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;ul class="nav navbar-nav pull-right"&gt;
        &lt;li class="nav"&gt;
          &lt;a href="#ta" (click)="scrollToElement(con)"&gt;Target&lt;/a&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/ul&gt;
  &lt;/span&gt;
  
  &lt;div class="container" style="margin-bottom: 20px;"&gt;
    &lt;div #home&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;h1&gt;Home&lt;/h1&gt;
      &lt;p&gt;You contact us here. Thank for your greatness&lt;/p&gt;
      &lt;p&gt;
        Welcome to the Random Phrase and Idiom Generator. There will be times when
        you may need more than a random word for what you want to accomplish, and
        this free online tool can help. The use of this tool is quite simple. All
        you need to do is indicate the number of random phrases you'd like to be
        displayed, and then hit the "Generate Random Phrases" button. Once done,
        your chosen number of idioms will be displayed along with the meaning of
        the idiom.
      &lt;/p&gt;
      &lt;p&gt;
        Welcome to the Random Phrase and Idiom Generator. There will be times when
        you may need more than a random word for what you want to accomplish, and
        this free online tool can help. The use of this tool is quite simple. All
        you need to do is indicate the number of random phrases you'd like to be
        displayed, and then hit the "Generate Random Phrases" button. Once done,
        your chosen number of idioms will be displayed along with the meaning of
        the idiom.
      &lt;/p&gt;
    &lt;/div&gt;
    &lt;div #con&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;h1&gt;Contact&lt;/h1&gt;
      &lt;p&gt;You contact us here. Thank for your greatness&lt;/p&gt;
      &lt;p&gt;
        Idioms are a wonderful part of the English language that give it a lot of
        flavor. They force people to know more than the literal meaning of words.
        Idioms are commonly used phrases which have a meaning completely different
        than their literal meaning. This can be quite confusing to those who
        aren't familiar with the idiom and those who are studying English.
      &lt;/p&gt;
      &lt;p&gt;
        Idioms are a wonderful part of the English language that give it a lot of
        flavor. They force people to know more than the literal meaning of words.
        Idioms are commonly used phrases which have a meaning completely different
        than their literal meaning. This can be quite confusing to those who
        aren't familiar with the idiom and those who are studying English.
      &lt;/p&gt;
    &lt;/div&gt;
  
    &lt;div #new&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;h1&gt;News&lt;/h1&gt;
      &lt;p&gt;This are new from us&lt;/p&gt;
      &lt;p&gt;
        Using this tool can be excellent practice for students studying English as
        a second language because it gives the literal meaning of each phrase.
        This allows them to see the phrase and its meaning at the same time. While
        there are long idiom lists available online, trying to navigate through
        them all on a single page can be daunting. Being able to create the exact
        number of random idioms which best suits your learning needs is a
        advantage this tool has over standard phrase list. In this way, this tool
        provides an excellent way for those learning English to practice their
        knowledge of English idioms and to learn new ones in the process.
      &lt;/p&gt;
      &lt;p&gt;
        Using this tool can be excellent practice for students studying English as
        a second language because it gives the literal meaning of each phrase.
        This allows them to see the phrase and its meaning at the same time. While
        there are long idiom lists available online, trying to navigate through
        them all on a single page can be daunting. Being able to create the exact
        number of random idioms which best suits your learning needs is a
        advantage this tool has over standard phrase list. In this way, this tool
        provides an excellent way for those learning English to practice their
        knowledge of English idioms and to learn new ones in the process.
      &lt;/p&gt;
    &lt;/div&gt;
  
    &lt;div #about&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;h1&gt;About&lt;/h1&gt;
      &lt;p&gt;This are new from us&lt;/p&gt;
      &lt;p&gt;
        It can also be a wonderful way for writers to brainstorm and spur more
        creativity in their writing. The tool can be used to get writing ideas
        flowing forcing the writer to use more creativity than they would with a
        single random word. For example, the writer can use the generated phrase
        to create a new paragraph or story. Since the writer has no idea what will
        appear, or even if they will be familiar with the idiom, it forces the
        writer to use creativity to incorporate it into what they're writing.
      &lt;/p&gt;
      &lt;p&gt;
        It can also be a wonderful way for writers to brainstorm and spur more
        creativity in their writing. The tool can be used to get writing ideas
        flowing forcing the writer to use more creativity than they would with a
        single random word. For example, the writer can use the generated phrase
        to create a new paragraph or story. Since the writer has no idea what will
        appear, or even if they will be familiar with the idiom, it forces the
        writer to use creativity to incorporate it into what they're writing.
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!-- Footer --&gt;
  &lt;footer class="page-footer font-small indigo" id="footer-id"&gt;
    &lt;!-- Footer Links --&gt;
    &lt;div class="container text-center text-md-left"&gt;
      &lt;!-- Grid row --&gt;
      &lt;div class="row"&gt;
        &lt;!-- Grid column --&gt;
        &lt;div class="col-md-3 mx-auto"&gt;
          &lt;!-- Links --&gt;
          &lt;h5 class="font-weight-bold text-uppercase mt-3 mb-4"&gt;Links&lt;/h5&gt;
  
          &lt;ul class="list-unstyled"&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Very long link 1&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Very long link 2&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Very long link 3&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Very long link 4&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
        &lt;!-- Grid column --&gt;
  
        &lt;hr class="clearfix w-100 d-md-none" /&gt;
  
        &lt;!-- Grid column --&gt;
        &lt;div class="col-md-3 mx-auto"&gt;
          &lt;!-- Links --&gt;
          &lt;h5 class="font-weight-bold text-uppercase mt-3 mb-4"&gt;Links&lt;/h5&gt;
  
          &lt;ul class="list-unstyled"&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 1&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 2&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 3&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 4&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
        &lt;!-- Grid column --&gt;
  
        &lt;hr class="clearfix w-100 d-md-none" /&gt;
  
        &lt;!-- Grid column --&gt;
        &lt;div class="col-md-3 mx-auto"&gt;
          &lt;!-- Links --&gt;
          &lt;h5 class="font-weight-bold text-uppercase mt-3 mb-4"&gt;Links&lt;/h5&gt;
  
          &lt;ul class="list-unstyled"&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 1&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 2&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 3&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 4&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
        &lt;!-- Grid column --&gt;
  
        &lt;hr class="clearfix w-100 d-md-none" /&gt;
  
        &lt;!-- Grid column --&gt;
        &lt;div class="col-md-3 mx-auto"&gt;
          &lt;!-- Links --&gt;
          &lt;h5 class="font-weight-bold text-uppercase mt-3 mb-4"&gt;Links&lt;/h5&gt;
  
          &lt;ul class="list-unstyled"&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 1&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 2&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 3&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="#!"&gt;Link 4&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
        &lt;!-- Grid column --&gt;
      &lt;/div&gt;
      &lt;!-- Grid row --&gt;
    &lt;/div&gt;
    &lt;!-- Footer Links --&gt;
  
    &lt;!-- Copyright --&gt;
    &lt;div class="footer-copyright text-center py-3"&gt;
      © 2018 Copyright:
      &lt;a href="https://mdbootstrap.com/education/bootstrap/"&gt; MDBootstrap.com&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- Copyright --&gt;
  &lt;/footer&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>How can I highlight navbar element when I scroll ?</p>
  
  <p>I have done some research and  it seem that I can use HostListener like this:</p>
  
  <pre><code>    @HostListener('window:scroll', ['$event'])
    scrollHandler(event) {
      console.log('Scroll Event');
      // console.log(event);
      const verticalOffset =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop ||
        0;
  
      console.log(verticalOffset);
    }
  </code></pre>
  
  <p>But I still unable to link scroll with div. Any idea ?</p>