Comments (6)
I've also ended up needing this feature. If help is needed with the PR I'm happy to chip in.
from motion.
Is anything happening with this, really need this
from motion.
Yeah we’d take a PR for this!
from motion.
Awesome! I should be able to get something together tomorrow 😁
from motion.
Or: is there away to do this manually @souporserious @InventingWithMonster ?
from motion.
Edit: Did not see the work done with this PR and this PR, but I'll just leave my answer here anyway.
You could do something like this, inspired by the the source.
export function useViewportScrollForElement(element) {
const viewportMotionValues = {
scrollX: motionValue(0),
scrollY: motionValue(0),
scrollXProgress: motionValue(0),
scrollYProgress: motionValue(0)
};
const setProgress = (offset, maxOffset, value) => {
value.set(!maxOffset || !offset ? 0 : offset / maxOffset);
};
const hasEventListenerRef = useRef(false);
useEffect(() => {
if (!element) return;
const updateScrollValues = () => {
window.requestAnimationFrame(() => {
const xOffset = element.scrollLeft;
const yOffset = element.scrollTop;
// Set absolute positions
viewportMotionValues.scrollX.set(xOffset);
viewportMotionValues.scrollY.set(yOffset);
// Set 0-1 progress
setProgress(
xOffset,
element.clientWidth - element.clientHeight,
viewportMotionValues.scrollXProgress
);
setProgress(
yOffset,
element.scrollHeight - element.clientHeight,
viewportMotionValues.scrollYProgress
);
});
};
if (!hasEventListenerRef.current) {
hasEventListenerRef.current = true;
if (typeof window === "undefined") return;
updateScrollValues();
window.addEventListener("resize", updateScrollValues);
window.addEventListener("scroll", updateScrollValues, true);
}
return () => {
hasEventListenerRef.current = false;
window.removeEventListener("resize", updateScrollValues);
window.removeEventListener("scroll", updateScrollValues, true);
};
}, [element, viewportMotionValues]);
return viewportMotionValues;
}
And use it like this for example:
const { scrollYProgress } = useViewportScroll(scrollRef.current || document.getElementById('scroll-container'));
Regarding getting the nearest scrollcontainer this code can be used with this scrollparent npm package.
Keep in mind I haven't thoroughly tested the code, but it works so far for my needs.
from motion.
Related Issues (20)
- [BUG] LazyMotion sets `initial` immediately; before `LazyMotion.features` load
- [BUG] with new version 11.0.24 HOT 3
- [FEATURE] Expose moved item index in Reorder.group
- ref prop does not work on div directly inside AnimatePresence with mode="popLayout" [BUG] HOT 1
- how to jump to specific position in animation sequence?
- [BUG] ScaleZ doesn't get applied to the motion component
- [BUG] problems with useMotionValue and useSpring to make an object follow the cursor after v11.
- [BUG] RotateZ not working in Shared Layout Animation HOT 2
- [BUG] v11 breaks all animations using vh and dvh as css unit HOT 1
- [BUG] v11 regression when cloning `motion` components HOT 2
- [BUG] Blinking on Enter and Exit Animations with layoutId and AnimatePresence
- [BUG] AnimatePresence - enter and exit animations are mixed when they alternate rapidly HOT 5
- [BUG] When resizing a canvas with a LayoutCamera, pointer events on meshes are aligned incorrectly.
- [BUG] Elements do not animate in latest React 19 canaries HOT 6
- [BUG] ReferenceError: React is not defined Just by importing framer-motion HOT 3
- [BUG] Docs: Application error: a client-side exception has occurred (see the browser console for more information). HOT 1
- [BUG] Property '$$typeof' is missing in `ForwardRefComponent` HOT 2
- [BUG] Next.js render error when first loaded
- [BUG] whileInView transition values override the other transition values
- [BUG] Components wrapped in `motion` don't pass props starting with "drag"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from motion.