简体   繁体   English

在没有jQuery的Angular 5上滚动触发活动类

[英]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. 我需要有关在滚动应用程序时如何触发添加类而不使用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 : 我有六个部分,高度为100vh,当我在特定部分上滚动时,我需要触发活动类,因此,当我在部分上(例如)时,我的活动类在以下位置:

 <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/ 想法是我在背景上触发动画,以便获得如下网页所示的效果: 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. 我已经在CSS中为SlidingInLeft和SlidingInRight做过动画,所以我只需要包装这些东西并实现它们。

What is the best solution for this? 最好的解决方案是什么?

This can be achieved using a directive by listening to the mousewheel event as below, 可以使用directive通过以下方式监听mousewheel事件来实现此目的,

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 的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 现场演示

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

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