motion-v
Version:
<p align="center"> <img width="100" height="100" alt="Motion logo" src="https://user-images.githubusercontent.com/7850794/164965523-3eced4c4-6020-467e-acde-f11b7900ad62.png" /> </p> <h1 align="center">Motion for Vue</h1>
45 lines (44 loc) • 1.31 kB
JavaScript
import { watchEffect, unref } from "vue";
import { isSSR } from "../utils/is.mjs";
import { getElement } from "../components/hooks/use-motion-elm.mjs";
import { scroll } from "../external/.pnpm/framer-motion@12.23.12/external/framer-motion/dist/es/render/dom/scroll/index.mjs";
import { motionValue } from "../external/.pnpm/motion-dom@12.23.12/external/motion-dom/dist/es/value/index.mjs";
function createScrollMotionValues() {
return {
scrollX: motionValue(0),
scrollY: motionValue(0),
scrollXProgress: motionValue(0),
scrollYProgress: motionValue(0)
};
}
function useScroll(scrollOptions = {}) {
const values = createScrollMotionValues();
watchEffect((onCleanup) => {
if (isSSR) {
return;
}
const cleanup = scroll(
(_progress, { x, y }) => {
values.scrollX.set(x.current);
values.scrollXProgress.set(x.progress);
values.scrollY.set(y.current);
values.scrollYProgress.set(y.progress);
},
{
offset: unref(scrollOptions.offset),
axis: unref(scrollOptions.axis),
container: getElement(scrollOptions.container),
target: getElement(scrollOptions.target)
}
);
onCleanup(() => {
cleanup();
});
}, {
flush: "post"
});
return values;
}
export {
useScroll
};