본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Angular 애니메이션 온 스크롤 디렉티브

원문 보기 위키 갱신

Angular 애니메이션 온 스크롤 디렉티브 (Animate On Scroll Directive)

AnimateOnScroll은 스크롤하는 동안 요소가 화면에 들어오거나 화면에서 벗어날 때 애니메이션을 적용해 주는 Angular 디렉티브예요. enterClass와 leaveClass 속성으로 각각 진입 시와 퇴장 시에 적용할 애니메이션 클래스를 지정하는 구조예요. 예제에서는 tailwindcss-primeui 플러그인의 애니메이션을 활용하지만, 유효한 CSS 애니메이션이라면 어떤 것이든 사용할 수 있어요. 문서에는 기본 사용법과 Props 목록이 정리되어 있고, 상호작용 요소가 없어 별도의 역할이나 키보드 지원이 필요하지 않다는 접근성 안내도 포함되어 있어요

출처: 문서

본문

접근성

스크린 리더 AnimateOnScroll은 어떤 role이나 속성도 필요로 하지 않아요. 키보드 지원 컴포넌트에는 대화형 요소가 포함되어 있지 않아요.

기본

애니메이션 클래스는 enterClass와 leaveClass 프로퍼티로 정의해요. 이 예제는 tailwindcss-primeui 플러그인의 애니메이션을 활용하지만, 유효한 CSS 애니메이션이라면 어떤 것이든 지원돼요.

import { Component } from '@angular/core';
import { AvatarModule } from 'primeng/avatar';

@Component({
    template: `
        <div class="card flex flex-col items-center overflow-hidden">
            <div class="flex flex-col items-center gap-2">
                <span class="text-xl font-medium">Scroll Down</span>
                <span class="animate-bounce h-8 w-8 bg-primary text-primary-contrast rounded-full inline-flex items-center justify-center">
                    <i class="pi pi-arrow-down"></i>
                </span>
            </div>
            <div class="h-[40rem]"></div>
            <div class="flex flex-wrap justify-center gap-8">
                <div
                    pAnimateOnScroll
                    enterClass="animate-enter fade-in-10 slide-in-from-l-8 animate-duration-1000"
                    leaveClass="animate-leave fade-out-0"
                    class="flex flex-col border border-surface shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4"
                >
                    <div class="rounded-full bg-primary text-primary-contrast w-12 h-12 flex items-center justify-center">
                        <i class="pi pi-user !text-2xl"></i>
                    </div>
                    <span class="text-2xl font-bold">Individual</span>
                    <span class="text-muted-color text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div pAnimateOnScroll enterClass="animate-enter fade-in-10 animate-duration-1000" leaveClass="animate-leave fade-out-0" class="flex flex-col border border-surface shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4">
                    <div class="rounded-full bg-primary text-primary-contrast w-12 h-12 flex items-center justify-center">
                        <i class="pi pi-users !text-2xl"></i>
                    </div>
                    <span class="text-2xl font-bold">Team</span>
                    <span class="text-muted-color text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div
                    pAnimateOnScroll
                    enterClass="animate-enter fade-in-10 slide-in-from-r-8 animate-duration-1000"
                    leaveClass="animate-leave fade-out-0"
                    class="flex flex-col border border-surface shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4"
                >
                    <div class="rounded-full bg-primary text-primary-contrast w-12 h-12 flex items-center justify-center">
                        <i class="pi pi-building !text-2xl"></i>
                    </div>
                    <span class="text-2xl font-bold">Enterprise</span>
                    <span class="text-muted-color text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
            </div>
            <div class="h-[30rem]"></div>
            <div class="flex flex-wrap justify-center gap-8">
                <div
                    pAnimateOnScroll
                    enterClass="animate-enter fade-in-10 slide-in-from-t-20 animate-duration-1000"
                    leaveClass="animate-leave fade-out-0"
                    class="flex flex-col border border-primary-200 shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4"
                >
                    <p-avatar image="https://primefaces.org/cdn/primeng/images/demo/avatar/amyelsner.png" shape="circle" size="xlarge" />
                    <span class="text-2xl font-medium">Jenna Thompson</span>
                    <span class="text-muted-color text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div pAnimateOnScroll enterClass="animate-enter fade-in-10 slide-in-from-b-20 animate-duration-1000" class="flex flex-col border border-primary-200 shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4">
                    <p-avatar image="https://primefaces.org/cdn/primeng/images/demo/avatar/asiyajavayant.png" shape="circle" size="xlarge" />
                    <span class="text-2xl font-medium">Isabel Garcia</span>
                    <span class="text-muted-color text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div pAnimateOnScroll enterClass="animate-enter fade-in-10 slide-in-from-t-20 animate-duration-1000" class="flex flex-col border border-primary-200 shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4">
                    <p-avatar image="https://primefaces.org/cdn/primeng/images/demo/avatar/onyamalimba.png" shape="circle" size="xlarge" />
                    <span class="text-2xl font-medium">Xavier Mason</span>
                    <span class="text-muted-color text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
            </div>
            <div class="h-[30rem]"></div>
            <div class="flex flex-wrap justify-center gap-8">
                <div
                    pAnimateOnScroll
                    enterClass="animate-enter fade-in-10 spin-in-45 slide-in-from-t-12 animate-duration-1000"
                    class="flex flex-col bg-primary text-primary-contrast border-primary shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4"
                >
                    <span class="bg-white/20 text-xl font-medium rounded-xl px-4 py-2">850K</span>
                    <span class="text-2xl font-bold">Customers</span>
                    <span class="text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div
                    pAnimateOnScroll="{ enterClass: 'animate-enter fade-in-10 zoom-in-50 slide-in-from-t-20 animate-duration-1000' }"
                    class="flex flex-col bg-primary text-primary-contrast border-primary shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4"
                >
                    <span class="bg-white/20 text-xl font-medium rounded-xl px-4 py-2">$1.5M</span>
                    <span class="text-2xl font-bold">Revenue</span>
                    <span class="text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div
                    pAnimateOnScroll
                    enterClass="animate-enter fade-in-10 spin-in-[-45deg] slide-in-from-t-16 animate-duration-1000"
                    class="flex flex-col bg-primary text-primary-contrast border-primary shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4"
                >
                    <span class="bg-white/20 text-xl font-medium rounded-xl px-4 py-2">140K</span>
                    <span class="text-2xl font-bold">Sales</span>
                    <span class="text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
            </div>
            <div class="h-[30rem]"></div>
            <div class="flex flex-wrap justify-center gap-8">
                <div pAnimateOnScroll enterClass="animate-enter fade-in-10 zoom-in-50 animate-duration-1000" class="flex flex-col bg-purple-500 text-white border-purple-500 shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4">
                    <div class="rounded-full border-2 border-white w-12 h-12 flex items-center justify-center">
                        <i class="pi pi-wifi !text-2xl"></i>
                    </div>
                    <span class="text-2xl font-bold">Bandwidth</span>
                    <span class="text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div pAnimateOnScroll enterClass="animate-enter fade-in-10 zoom-in-75 animate-duration-1000" class="flex flex-col bg-teal-500 text-white border-teal-500 shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4">
                    <div class="rounded-full border-2 border-white w-12 h-12 flex items-center justify-center">
                        <i class="pi pi-database !text-2xl"></i>
                    </div>
                    <span class="text-2xl font-bold">Storage</span>
                    <span class="text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
                <div pAnimateOnScroll enterClass="animate-enter fade-in-10 zoom-in-50 animate-duration-1000" class="flex flex-col bg-indigo-500 text-white border-indigo-500 shadow-lg justify-center items-center max-w-80 rounded-2xl p-8 gap-4">
                    <div class="rounded-full border-2 border-white w-12 h-12 flex items-center justify-center">
                        <i class="pi pi-arrows-v !text-2xl"></i>
                    </div>
                    <span class="text-2xl font-bold">Requests</span>
                    <span class="text-center">Lorem ipsum dolor sit amet consectetur adipisicing elit.</span>
                </div>
            </div>
        </div>
    `,
    standalone: true,
    imports: [AvatarModule]
})
export class AnimateonscrollBasicDemo {}

스크롤 시 애니메이션

AnimateOnScroll은 스크롤 중 요소가 뷰포트에 들어오거나 벗어날 때 애니메이션을 적용하는 데 사용해요.

프로퍼티

이름 타입 기본값 설명
enterClass string - 진입 애니메이션의 CSS 클래스를 정의하는 셀렉터예요.
leaveClass string - 퇴장 애니메이션의 CSS 클래스를 정의하는 셀렉터예요.
root HTMLElement - IntersectionObserver API의 root 옵션을 지정해요.
rootMargin string - IntersectionObserver API의 rootMargin 옵션을 지정해요.
threshold number 0.5 IntersectionObserver API의 threshold 옵션을 지정해요.
once boolean false 최초 실행 후 스크롤 이벤트 리스너를 제거할지 여부예요.
dt InputSignal undefined 컴포넌트의 스코프 디자인 토큰을 정의해요.
unstyled InputSignal undefined 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요.
pt InputSignal undefined 컴포넌트 내부의 DOM 요소에 속성을 전달하는 데 사용해요.
ptOptions InputSignal undefined 컴포넌트의 passthrough(pt) 옵션을 구성하는 데 사용해요.

더 알아보기 (Learn more)

이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.