question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Imperatively animate scroll position

See original GitHub issue

Is 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:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
amannncommented, Sep 5, 2019

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() before motion.set() doesn’t seem to make a difference.

1reaction
justintempscommented, Feb 25, 2021

@mattgperry the onChange method in your example should be onUpdate, right?

animate(0, 100, {
  duration: 2,
  onUpdate: v => window.scrollTop = v
})
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found