【问题标题】:How to properly move a React component from left to right using react-spring on scroll?如何在滚动时使用 react-spring 从左到右正确移动 React 组件?
【发布时间】:2020-06-23 20:24:30
【问题描述】:

我有一个 div,其中包含一个 image 和一些 text 对齐到中心

我需要使用react-spring 进行转换,当我滚动时它应该看起来像文本来自-x 值到0 并且它必须非常平滑和真实看着。

所以我查看了react-spring 文档,他们没有关于这类事情的丰富文档。只是几个例子。

举个例子,我怎样才能找到适合这种场景的其他道具?

import {useTransition, animated} from 'react-spring'

const component = () => {

  const props = useSpring({opacity: 1, from: {opacity: 0}}) // how can I know other parameters like opcacity, from, to etc...

  return (
    <animated.div>
      {div contents here}
    </animated.div>
  )
}

还有谁可以帮助我进行左右转换,当滚动时,文本来自左侧并落在上述图像的中心?

谢谢。

【问题讨论】:

    标签: reactjs react-spring


    【解决方案1】:

    我想你可能对translateX感兴趣

    从左到右落在上述图片的中心

    将以上内容与display: flex 和align-items: center 结合起来

    示例

    import React, { useState, useEffect } from "react";
    import ReactDOM from "react-dom";
    import { useSpring, animated } from "react-spring";
    
    const style = {
      background: 'url("https://picsum.photos/200/300") center center / cover no-repeat',
      padding: '10px',
      width: '300px',
      height: '200px',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
    }
    
    const textStyle = {
      color: 'white',
      fontSize: '50px',
      background: 'black'
    }
    
    const App = props => {
      const [isLoaded, setLoaded] = useState(false);
      const springProps = useSpring({ 
        opacity: 1, 
        delay: 700,
        reset: isLoaded,
        transform: 'translateX(0px)',
        from: { 
          opacity: 0,
          transform: 'translateX(-250px)'
        } });
    
      useEffect(() => {
        fetch("https://picsum.photos/200/300")
          .then(pr => {
            setLoaded(true);
          })
      }, [])
    
      return <>{isLoaded ? <div style={style}>
            <animated.div style={{...textStyle, ...springProps}}>Some text</animated.div>
          </div> : <span></span>}</>
    };
    

    什么时候滚动?

    在这种情况下,您将不得不对useSpring 使用第二个重载,并使用析构的set 方法来更新onscroll 回调中的值

    示例

    const App = props => {
      const [isLoaded, setLoaded] = useState(false);
      const [{ param }, set] = useSpring(() => ({ param: 0 }));
    
      const onScroll = () => {
        let ratio = window.scrollY / window.innerHeight;
        ratio = ratio > 1 ? 1 : ratio;
    
        set({
          param: ratio
        });
      };
    
      useEffect(() => {
        window.addEventListener("scroll", onScroll);
    
        fetch("https://picsum.photos/200/300").then(pr => {
          setLoaded(true);
        });
    
        return () => {
          window.removeEventListener("scroll", onScroll);
        };
      }, []);
    
      return (
        <div style={containerStyle}>
          {isLoaded ? (
            <div style={style}>
              <animated.div
                style={{
                  ...textStyle,
                  opacity: param.interpolate({
                    range: [0, 0.5, 0.75, 1],
                    output: [0, 0.5, 0.75, 1]
                  }),
                  transform: param
                    .interpolate({ range: [0, 0.5, 1], output: [-50, -25, 0] })
                    .interpolate(x => `translateX(${x}px)`)
                }}
              >
                Some text
              </animated.div>
            </div>
          ) : (
            <span />
          )}
        </div>
      );
    };
    

    【讨论】:

    • 谢谢你,我怎样才能用同样的机制来做 y 轴?
    • nvm 找到了。谢谢 :) 但是你有没有用spring-react 做的例子让我看看?
    • There are codepen examples at the bottom of the page 大多数时候,他们对useSpring 函数使用第二次重载,并使用param.interpolate 函数将更改反映为css 值。不幸的是,正如您所说,文档很少。
    • 谢谢。有没有一种方法可以仅在我滚动一些像素时激活此效果?假设我想在已经滚动 200 像素或向下滚动到某个元素时激活此效果。如果我向下滚动到特定元素,如果您能告诉我一种激活它的方法,那就太好了
    • 你必须操纵那个表达式。 let ratio = window.scrollY / window.innerHeight; 有点。例如可以是let ratio = window.scrollY === 200 ? 1 : 0
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多