Giter VIP home page Giter VIP logo

Comments (6)

DexterShepherd avatar DexterShepherd commented on June 5, 2024 5

I've also ended up needing this feature. If help is needed with the PR I'm happy to chip in.

from motion.

joebentaylor1995 avatar joebentaylor1995 commented on June 5, 2024 3

Is anything happening with this, really need this

from motion.

mattgperry avatar mattgperry commented on June 5, 2024 2

Yeah we’d take a PR for this!

from motion.

souporserious avatar souporserious commented on June 5, 2024

Awesome! I should be able to get something together tomorrow 😁

from motion.

joebentaylor1995 avatar joebentaylor1995 commented on June 5, 2024

Or: is there away to do this manually @souporserious @InventingWithMonster ?

from motion.

sondretj avatar sondretj commented on June 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.