스크롤 이벤트에 반응해 애니메이션의 특정 프레임을 재생하는 기능을 만들었습니다.
스크롤에 따라 애니메이션 키프레임으로 렌더링되는 라이브러리입니다.
Scroll Animation 클래스는 window 객체 안에 ScrollAnimation 이라는 이름으로 정의되어 있습니다.
입력 파라미터는 아래와 같습니다.
new ScrollAnimation(element, scrollTarget, animationName, scrollStartPosition, scrollEndPosition);
항목 이름 | 설명 | 형식 | 비고 |
---|---|---|---|
element | 애니메이션을 적용받을 엘리먼트입니다. | HTMLElement | |
scrollTarget | 스크롤 이벤트를 받을 엘리먼트입니다. | Window HTMLElement | |
animationName | CSS에 정의된 애니메이션 이름입니다. | string | |
scrollStartPosition | 스크롤이 시작될 지점을 지정합니다. | number function | 고정된 값과, 함수를 받을 수 있습니다. 함수를 전달할 경우, number 형식의 값을 리턴해야 합니다. |
scrollEndPosition | 스크롤이 끝날 지점을 지정합니다. | number function | 고정된 값과, 함수를 받을 수 있습니다. 함수를 전달할 경우, number 형식의 값을 리턴해야 합니다. |
@keyframe animation-name {
from { color:red; background:rgb(0,0,0); }
50% { background:rgba(128,2555,128,1); }
to { color:blue; background:#fff; }
}
<div id="animation">
<p>HELLO</p>
</div>
const element = document.querySelector('#animation');
const scrollTarget = window;
const animationName = 'animation-name';
const start = 0;
const end = () => document.documentElement.scrollHeight - document.documentElement.offsetHeight;
const animation = new ScrollAnimation(element, scrollTarget, animation, start, end);
0% { transform:scale(calc(50% + 50vw)); }
100% { transform:scale(calc(0% + 0vw)); }
0% { transform:scale(calc(50vw + 50%)); }
100% { transform:scale(calc(0% + 0vw)); }