简体   繁体   English

在 Angular 中向下滚动 div animation

[英]Scroll down div animation in Angular

How to animate the each box when scrolling down the box will be animate like fade-in, if scroll to the.box pageYOffset 20 that box will be fade-in, I tried with AOS third-party library and its worked fine but i want to know how to do scroll down animation without any third-party library向下滚动框时如何为每个框设置动画将像淡入一样动画,如果滚动到.box pageYOffset 20该框将淡入,我尝试使用AOS第三方库并且它工作正常但我想要要知道如何在没有任何第三方库的情况下向下滚动 animation

myCode 我的代码

 @HostListener('window:scroll', ['$event']) onWindowScroll(e) { const box = document.querySelector('.box'); if (window.pageYOffset < box.clientHeight ) { box.classList.add('colorChange'); } else { box.classList.remove('colorChange'); } }
 .container{ text-align: center; margin: auto; padding-top: 768px; }.box{ background: #007aff; width: 200px; height: 200px; display: block; word-spacing: 30px; line-height: 30px; margin: 30px auto; }.colorChange{ background: #fcad2e; animation: fade-in 1s ease-in-out; } @keyframes fade-in { from{ opacity: 0; transform: translateY(50px); } to{ opacity: 1; transform: translateY(0); } }
 <div class="container"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div>

This works by specifically adding a window scroll event listener.这通过专门添加 window 滚动事件侦听器来工作。

import { Component, VERSION, HostListener } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular " + VERSION.major;
  constructor(){
    window.addEventListener("scroll", (event)=>{
    debugger;
    const box = document.querySelector('.box');
    if (window.pageYOffset < box.clientHeight ) {
      box.classList.add('colorChange');
    } else {
      box.classList.remove('colorChange');
    }
  });
  }
  }

Here's link to working example这是工作示例的链接

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

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