스크롤 애니메이션 스킬
워크플로
- 애니메이션 스타일, 타이밍, 한 번 실행 또는 반복 실행 여부를 확인합니다.
- 키프레임 + JS 옵저버 스니펫과 적용할 정확한 Tailwind 클래스를 제공합니다.
- 임계값(threshold), rootMargin, 지속 시간(duration), 지연 시간(delay), transform/blur 값만 집중적으로 조정합니다.
사용 체크리스트
- JS 스니펫을
<head> 내 키프레임 뒤에 삽입합니다.
- 요소에 애니메이션 클래스와
animate-on-scroll을 추가합니다.
- 키프레임 이름이 Tailwind 애니메이션 참조와 일치하는지 확인합니다.
IntersectionObserver 트리거
<script>
(function () {
const style = document.createElement("style");
style.textContent = `
/* 기본값: 일시 정지 */
.animate-on-scroll { animation-play-state: paused !important; }
/* JS에 의해 활성화 */
.animate-on-scroll.animate { animation-play-state: running !important; }
`;
document.head.appendChild(style);
const once = true;
if (!window.__inViewIO) {
window.__inViewIO = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("animate");
if (once) window.__inViewIO.unobserve(entry.target);
}
});
}, { threshold: 0.2, rootMargin: "0px 0px -10% 0px" });
}
window.initInViewAnimations = function (selector = ".animate-on-scroll") {
document.querySelectorAll(selector).forEach((el) => {
window.__inViewIO.observe(el);
});
};
document.addEventListener("DOMContentLoaded", () => initInViewAnimations());
})();
</script>
키프레임
<style>
@keyframes animationIn {
0% {
opacity: 0;
transform: translateY(30px);
filter: blur(8px);
}
100% {
opacity: 1;
transform: translateY(0);
filter: blur(0px);
}
}
</style>
Tailwind 예시
<div class="animate-on-scroll [animation:animationIn_0.8s_ease-out_0.1s_both]">
...
</div>
사용자 정의 옵션
- 트리거:
threshold와 rootMargin을 조정하여 더 일찍/늦게 표시합니다.
- 반복:
once = false로 설정하여 요소가 다시 진입할 때 재생할 수 있습니다.
- 모션: 키프레임에서
translateY와 blur를 조정합니다.
- 타이밍: Tailwind 애니메이션 값에서 지속 시간(duration)과 지연 시간(delay)을 변경합니다.
일반적인 실수
- JS 스니펫 앞에 키프레임을 포함하지 않은 경우.
- Tailwind 애니메이션에서 사용한 키프레임 이름과 다른 이름을 사용한 경우.
- 옵저버 초기화 전에 요소가 이미 뷰포트에 있어 애니메이션이 실행되지 않는 경우.
사양이 누락되었을 때 질문할 사항
- 애니메이션은 한 번 실행되어야 합니까, 아니면 요소가 다시 진입할 때마다 실행되어야 합니까?
- 뷰포트에 진입하기 전에 얼마나 미리 시작해야 합니까?
- 어떤 모션 스타일(페이드, 슬라이드, 블러, 스케일)을 원하십니까?