[FEATURE] Imperatively animate scroll position
See original GitHub issueIs your feature request related to a problem? Please describe.
I’m trying to use Framer Motion to scroll a DOM element with spring physics.
Describe the solution you’d like
I guess it should be possible somehow with MotionValue
, but I wasn’t sure how to trigger a spring animation.
This is the code I’ve tried:
const scrollMotionValue = new MotionValue(scrollParent.scrollTop);
scrollMotionValue.onChange(value => {
scrollParent.scrollTop = value;
});
scrollMotionValue.set(target);
This sets the value directly and it doesn’t animate. I guess you’d have to use attach
somehow? I couldn’t quite figure it out.
Describe alternatives you’ve considered
I was able to get it to work with popmotion like this:
spring({
from: scrollParent.scrollTop,
to: target,
mass: 1,
stiffness: 400,
damping: 40
}).start({
update(value) {
scrollParent.scrollTop = value;
}
});
I haven’t used popmotion before and I’m not sure if that is entirely correct.
Thanks for your help!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Scroll-linked Animations - W3C
Represents the starting (0% progress) scroll position of the view progress timeline as a length offset (in px) from the scroll origin. Null...
Read more >How to animate scroll position in React - Benevolent Bytes Blog
When the scrollbar is at the bottom of the page, our animation is at the last frame. Scrolling back up the page will...
Read more >ScrollMagic Documentation
animate based on scroll position – either trigger an animation or synchronize it to the scrollbar movement (like a playback scrub control).
Read more >Smooth Scrolling | CSS-Tricks
Here's the code to perform a smooth page scroll to an anchor on the same ... location.hostname == this.hostname ) { // Figure...
Read more >Examples | Framer for Developers
#Scroll-triggered animations ... Motion also provides a whileInView prop that defines a visual state to animate to while a component is in the...
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 FreeTop 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
Top GitHub Comments
Hey @natew! Thanks, that seems to work.
motion.set
now animates using the spring.However I also need a way to immediately set the motion value to a new value. The use case is that I always want to start from the current scroll position. I mean I could add a scroll event listener, which syncs the value back into the motion value, but then you’d end up in a strange chain between the scroll position and the motion value which syncs back and forth.
I couldn’t find a function which does that. Calling
motion.stop()
beforemotion.set()
doesn’t seem to make a difference.@mattgperry the
onChange
method in your example should beonUpdate
, right?