最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Initial Position of Elements based on Screen Size (Framer) - Stack Overflow

programmeradmin2浏览0评论

On desktop I have text which floats in from an X axis, it is out of view until you scroll to it. On mobile I intend to change this to be a small "bump" on the Y axis.

I have tried multiple things which in my eyes should have worked, but there is no response.

This is my code at the minute.

const ParallaxHeader = ({
    text,
    className,
}) => {
    const { scrollY } = useScroll();
    const [initialPosition, setInitialPosition] = useState({x: 600, y: 0});
    const [animatePosition, setAnimatePosition] = useState({x: 0, y: 0});

    const opacity = useTransform(
        scrollY,
        [50, 300],
        [0, 1]);
    
    useEffect(() => {
        const updateAnimation = () => {
            if(window.innerWidth < 768) {
                setInitialPosition({ x: 0, y: 50 }); // Smaller offset for mobile
                setAnimatePosition({ x: 0, y: 0 });
            } else {
                setInitialPosition({ x: 600, y: 0 }); // Larger offset for desktop
                setAnimatePosition({ x: 0, y: 0 });
            }
        };

        updateAnimation();

        window.addEventListener('resize', updateAnimation);

        return () => window.removeEventListener('resize', updateAnimation);
    }, []);  

I have attempted creating a custom hook for a media query instead, however nothing seems to want to get it to register, it seems that the useEffect has no effect on the position at all, I can alter all the values and nothing will change.

The only thing that will alter the values is changing the initialPosition where it is declared with useState().

发布评论

评论列表(0)

  1. 暂无评论