gpt4 book ai didi

javascript - 在滚动上触发事件类,没有 Jquery 的 Angular 5

转载 作者:行者123 更新时间:2023-11-28 03:53:20 24 4
gpt4 key购买 nike

我需要有关如何在应用程序中滚动时触发添加类而不是使用 jQuery 的说明。

我有具有这种结构的侧菜单

<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>

我有六个高度为 100vh 的部分,当我滚动到特定部分时,我需要触发事件类,因此当我在部分(例如)Portfolio 上时,我的事件类处于打开状态:

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

我的问题与另一个问题有关,当我处于该特定部分时,我还需要触发一些其他事件。想法是我在背景上触发动画,这样我就可以获得像此网页上一样的效果:https://sonikpass.com/

我有这样的结构:

<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>

我已经在CSS中为SlidingInLeft和SlidingInRight完成了动画,所以我只需要包装这些东西并实现它们。

最好的解决方案是什么?

最佳答案

这可以使用指令通过监听mousewheel事件来实现,如下所示,

指令代码

@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

关于javascript - 在滚动上触发事件类,没有 Jquery 的 Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47777547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com