简体   繁体   中英

Triggering active class on scroll, Angular 5 without Jquery

I need instructions on how to trigger adding classes when scrolling in my app and not to use jQuery.

I have side menu with this structure

<div class="menu">
  <div class="menu-item">
    <div class="line"></div>
    <span class="home">Home</span>
  </div>
  <div class="menu-item">
    <div class="line"></div>
    <span class="other">Venture Creation</span>
  </div>
  <div class="menu-item">
    <div class="line"></div>
    <span class="other">Product Studio</span>
  </div>
  <div class="menu-item">
    <div class="line"></div>
    <span class="other">Portfolio</span>
  </div>
  <div class="menu-item">
    <div class="line"></div>
    <span class="other">what i do</span>
  </div>
  <div class="menu-item">
    <div class="line"></div>
    <span class="other">Stay in Touch</span>
  </div>
</div>

And I have six sections with a height of 100vh, I need to trigger active class when I scroll over the specific section, so when I am on section (for example) Portfolio my active class is on :

 <div class="menu-item">
    <div class="line"></div>
    <span class="other">Portfolio</span>
  </div>

My question is connected with another problem, I also need to trigger some other events when I am in that specific sections. Idea is that I trigger animation on background so that I can get effect like on this web page: https://sonikpass.com/

I have a structure like this:

<div class="global-background">
 <div class="top-part">
  <img class="img-one-top" src="../assets/images/brain1-01.svg" alt="" />
  <img class="img-two-top" src="../assets/images/whoiam1-01.svg" alt="" />
  <img class="img-three-top" src="../assets/images/whatidid1-01.svg" alt="" />
  <img class="img-four-top" src="../assets/images/whatido1-01.svg" alt="" />
  <img class="img-five-top" src="../assets/images/connect1-01.svg" alt="" />
 </div>
 <div class="bottom-part">
  <img class="img-one-bottom" src="../assets/images/brain2-02.svg" alt="" />
  <img class="img-two-bottom" src="../assets/images/whoiam2-02.svg" alt="" />
  <img class="img-three-bottom" src="../assets/images/whatidid2-02.svg" alt="" />
  <img class="img-four-bottom" src="../assets/images/whatido2-02.svg" alt="" />
  <img class="img-five-bottom" src="../assets/images/connect2-02.svg" alt="" />
 </div>
</div>

I have done Animations in CSS for SlidingInLeft and SlidingInRight so I just need to wrap these things and implement them.

What is the best solution for this?

This can be achieved using a directive by listening to the mousewheel event as below,

Directive code

@Directive({
    selector: '[wheel]'
})
export class WheelDirective implements OnInit{
    constructor(
        private renderer: Renderer2,
        private el: ElementRef
    ){}
    ngOnInit(){
      Array.from(this.el.nativeElement.children).forEach((item,index)=>{
        if(index!==0){
          item.classList.add('hidden');
        }
      })
    }

    @HostListener('mousewheel', ['$event']) onMousewheel(event) {
      let parentDiv = event.srcElement.parentElement;
      console.log(parentDiv);
      let childNodes = Array.from(parentDiv.children);
      let currentIndex=-1;
      if(parentDiv && parentDiv.children){
      childNodes.forEach((item,index)=>{
        if(item!==event.srcElement){
          item.classList.add('hidden');  
        }else{
          item.classList.add('hidden');  
          currentIndex = index
        }

      });
      if(currentIndex===(childNodes.length -1)){
        currentIndex=0;
      }
      childNodes[currentIndex+1].classList.remove('hidden');
      }
}

HTML

<div class="top-part" wheel>
  <img class="img-one-top" src="https://lorempixel.com/400/200" alt="" />
  <img class="img-two-top" src="https://lorempixel.com/400/201" alt="" />
  <img class="img-three-top" src="https://lorempixel.com/400/202" alt="" />
  <img class="img-four-top" src="https://lorempixel.com/400/300" alt="" />
  <img class="img-five-top" src="https://lorempixel.com/400/600" alt="" />
</div>

LIVE DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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