anime-scrolltrigger v0.1.0
Anime-ScrollTrigger

Features
- Trigger animation when scroller offset intersects with trigger element.
- Support Scroll Triggering without the need of using animation ( i.e you can still use scroll trigger when you don't want to animate)
- Pinning trigger element
- Option for linear interpolation of animation based on scroll. i.e, trigger animation state based on scroll
- Debugging the offset markers.
- Calculates offsets using
boundingClientRectandscrollTopinstead of usingIntersection Observer.
Demo codepen: https://codepen.io/Zaw-Lin-Tun-the-encoder/pen/vYbervK
Introduction
Anime-ScrollTrigger is a library which is aimed to animate on scroll just
like ScrollTrigger. Some name conventions would be different but
the logic is pretty similar.
I strongly recommend you to use that library because it is awesome and maintained.
I don’t know how exactly that library is implemented in the context of coding. I only have abstract ideas of that library and tried to create my own one based on those ideas.
💡 The animation system of this library is solely dependent on
animejslibrary.
Most of usages are similar to ScrollTrigger . Please have a look at
the following instructions.
Understanding How Trigger Works
It's important to know that there are two types of trigger offsets ( trigger positions ):
trigger element: start offset
startTriggerOffsetand end offsetendTriggerOffset:Offsets are calculated on height of the trigger element relative to the top of the trigger element. You can change the value with first word of
startorendattribute underscrollTriggerattribute.scroller/container element: start offset
startScrollerOffsetand end offsetendScrollerOffset.Offsets are calculated relative to clientHeight of the scroller element. You can change the value with second word of
startorendattribute underscrollTriggerattribute.
Trigger will start when startTriggerOffset meets startScrollerOffset.
Trigger will end when endTriggerOffset meets endScrollerOffset.
For example,
...
scrollTrigger: {
start: 'top bottom',
end: '10% bottom',
}The above values indicate that
- animation will start when
the topof trigger element and thebottom endof scroller meets. - animation will end when
10% of the trigger element height + topof trigger element andbottom endof scroller meets.
Changelogs
Read Changelog here
Installation
Use npm
npm install anime-scrolltriggerOr cdn
import AnimeScrollTrigger from 'https://cdn.jsdelivr.net/npm/anime-scrolltrigger@{enter version e.g, 0.1.0}/dist/anime-scrolltrigger.es.js';Usages
Import AnimeScrolltrigger
import AnimeScrollTrigger from 'anime-scrolltrigger' Create an instance.
- container: Scroller HTML element
- animations: Array of animation object
let container = document.getElementById('container');
let boxes = document.querySelectorAll('.box')
let animations = [
{
targets: boxes[0],
translateX: 100,
easing: 'linear',
scrollTrigger: {
trigger: boxes[0],
start: 'top 3%',
end: 'bottom 30%',
}
}, {
targets: boxes[1],
backgroundColor: '#a993ff',
easing: 'linear',
scrollTrigger: {
trigger: boxes[1],
start: 'top 40%',
end: 'bottom center',
lerp: true,
}
}];
new AnimeScrollTrigger(container, animations);Animation
Animation object has the following structure.
- targets (optional) : HTML elements to animate
- attributes (optional) which you want to animate ( same as animejs) - for example
{ translateX: 100, backgroundColor: 100, ... } - scrollTrigger: scroll trigger object
Example Animation Object
{
targets: boxes[1],
backgroundColor: '#a993ff',
easing: 'linear',
scrollTrigger: {
...
}
}Scroll Trigger
trigger:
HTMLElementstart:
StringIndicate where
startTriggerOffsetoftriggerelement will intersect withstartScrollerOffsetofscrollerelement and when it intersects, animation will * *start**. Format is"start-trigger-offset start-scroller-offset". Default value is"top center".Offset can be provided as percentage (e.g. 10%, 20%, -5%) or constant values: top, center, bottom.
end:
StringIndicate where
endTriggerOffsetwill intersect withendScrollerOffsetand when it intersects, animation will end .
Format is"end-trigger-offset end-scroller-offset". Default value is"bottom center".Offset can be provided as percentage (e.g. 10%, 20%, -5%) or constant values: top, center, bottom.
actions:
StringAction behavior when a certain event is triggered. Format is
"on-enter-action on-leave-action on-enter-back-action on-leave-back-action". Default value is "play none none reverse".Note: when
lerpis enabled, user-definedon-enter-actionandon-enter-back-actionwill be ignored which means that animation will be forwarded on scrolling down and backwarded on scrolling up.lerp:
BooleanLerp ( linear interpolation) enables progressive transition of animation which means that animation state will be triggered based on scroll position instead of triggering at once when scroller reach trigger start offset.
pin:
BooleanorStringorHTMLElementPinning will pin the trigger element to the top of container element. Pinning state will start when it reaches animation-trigger-start-offset and ends when it reaches animation-trigger-end-offset.
A pinned element should exist equal or below
topof the trigger element so that it will pin the element when trigger element is reached.debug:
BooleanorObjectIndicate to show start offset markers and end offset markers in order to see where they are located visually. You can pass object in order to
change markers color.debug: { startTriggerOffsetMarker: '#f6a945', endTriggerOffsetMarker: '#ffd291', startScrollerOffsetMarker: '#4b45f6', endScrollerOffsetMarker: '#d5d4ff', }events:
ObjectEvents triggered on scroll.
- onEnter:
- onLeave:
- onEnterBack:
- onLeaveBack:
- onUpdate:
Example scroll trigger object is
{
trigger: boxes[1],
start: 'top 40%',
end: 'bottom center',
lerp: true,
debug: true,
pin: false,
actions: 'play none none reverse',
events:
{ // Scroll Trigger Events
onEnter: (trigger) => {},
onLeave: (trigger) => {},
onEnterBack: (trigger) => {},
onLeaveBack: (trigger) => {},
onUpdate: (trigger,progress) => {},
}
}Examples
Tips and mistakes
- Sometimes when the animation is not working, make sure that container element you provided is actually scrolling.
You may want to listen to scroll event of that element.
container.addEventListener('scroll',()=>console.log('yay scrolling')) - The start-intersection-trigger-offset needs to be lower than end-intersection-trigger-point offset. If it is not, animation/ triggering won't work.
- Incorrect trigger offsets could probably happen because of initializing trigger before dom tree hasn't completed building yet. So workaround might be setting timeout.
setTimeout(()=>{ new AnimeScrollTrigger(element,animations) },300)
TO-DO
- configurable marker colors
- pin option
- it should pin the target element to trigger element until it reaches animation-end-offset
- test on horizontal scroll