Control Bodymovin animation on mouse scroll according to page height
See original GitHub issueHi. sorry, but this is more of my technical limit rather than the bodymovin’s. but i have a question regarding something im trying to achieve.
I am trying to make my frame 1 of my animation at the top of my page, and final frame of animation at the bottom of a scrolling page. So when you scroll, the animation will animate and reach the last frame at the bottom.
I’ve manage to do the play on mousewheel part, but i cant figure out how to tie the start and end of animation depending on the height of the page.
here’s what ive done so far (scroll on right side of page): http://fariskassim.com/stage/bodymovin/v1/index_mod.html
`
var animData = {
wrapper: document.getElementById('bodymovin'),
animType: 'svg',
loop: false,
prerender: true,
autoplay: false,
path: 'data.json'
};
var anim = bodymovin.loadAnimation(animData),
container = document.getElementById('bodymovin');
contentid = document.getElementById('contentid');
animLoaded();
function animLoaded() {
if (!anim.isLoaded) {
setTimeout("animLoaded();", 1000);
return;
} else {
attachScroll(anim, contentid, 50);
}
}
function attachScroll(anim, contentid, speed) {
var val = 0,
totalDuration = anim.totalFrames/anim.frameRate*1000;
if (contentid.addEventListener) {
contentid.addEventListener("mousewheel", MouseWheelHandler, false);
contentid.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
}else{
contentid.attachEvent("onmousewheel", MouseWheelHandler);
}
function MouseWheelHandler(e) {
var e = window.event || e;
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
if (delta < 0) {
if (val < totalDuration) {
val += (Math.abs(delta))*speed;
}
}else {
if (val > 0) {
val -= (Math.abs(delta))*speed;
}
}
bodymovin.goToAndStop(val);
}
}
`
Issue Analytics
- State:
- Created 6 years ago
- Comments:15
Top Results From Across the Web
Play a Lottie / Bodymovin animation on scroll on an HTML ...
Let's pretend that you loaded this from a .json file const canvas = document.createElement('canvas'); const ctx = canvas.
Read more >Web animations with After Effects & Lottie | Webflow University
Learn how to create an animation that triggers when someone loads a webpage, clicks, hovers, or scrolls. Use this technology to render an...
Read more >Animate while scrolling | Webflow University
0 percent is where the animation begins, but at a hundred percent, by the time the Lottie animation has scrolled past our screen,...
Read more >UX | XAML Brewer, by Diederik Krols
The app uses some of the newer WinUI controls such as ... Mouse scrolling is a bit problematic: the movie repeaters take almost...
Read more >lottie animations Archives - Page 2 of 2 - vidalgo
Use Vidalgo Lottie free animation to celebrate this year's Irish festival and embrace the luck ... height: 300px;” loop controls autoplay></lottie-player>.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

sorry for the late replies. if you are referring to the one i did here: https://www.fariskassim.com/stage/typojanchi/old_v1/
you can view the source and the code for what you want is there in the script tags. but i’ll just repost it here:
Basically, i just measure the scroll position relative to scrollHeight of the scrollable div on the right in percentages. Then with this percentage, i just use it to ‘goToAndStop’ on the frame of the animation.
hello, i want to apply this to my animation. Can you help me by sharing the code?