0

正如标题所说,我想在 React Js 中使用成帧器动作进行页面转换,例如https://allhero.co.jp

我试过

.anim-page{
    &,._red{
      position: fixed;
      top: 0;
      width: 100%;
      height: 100%;
      z-index:999;
    }
    &._black{
      background-color: black;
    }
    ._red{
      background-color: red;
    }
  }
//-------------------------------------
const page_anim = {
    red: {
      hidden: { scaleX: 0 },
      visible: {
        scaleX: [1, 0],
        transformOrigin: 'right',
        transition: { duration: 0.5, {delay:0.3} },
      },
    },
    black: {
      hidden: { right: '100%' },
      visible: {
        // skewX: ['-25deg', '0deg'],
        right: '0%',
        transition: { duration: 0.7 },
      },
    },
  }

// -----------------------------------
<motion.div
          className='anim-page _black'
          variants={page_anim.black}
          initial='hidden'
          animate='visible'
        >
          <motion.div className='_red' variants={page_anim.red}></motion.div>
        </motion.div>

看起来有点像,但我不知道这是否是正确的做法,而且看起来并不多。重要的是我想让它可重复使用。我喜欢加载页面时黑色消失的功能

4

1 回答 1

0

我认为这是一个 gsap 的事情,但我做到了,不是很好,但我喜欢它

动画加载.jsx

import { motion } from 'framer-motion'

import React from 'react'
const AnimLoading = () => {
  const page_anim = {
    red: {
      hidden: { scaleX: 0 },
      visible: {
        scaleX: [1, 0],
        transformOrigin: 'right',
        transition: { duration: 0.4, delay: 0.2 },
      },
    },
    black: {
      hidden: { right: '100%' },
      visible: {
        skewX: ['0deg', '-5deg', '0deg'],
        right: ['100%', '0%'],
        transition: { duration: 0.5 },
      },
      done: {
        right: '-100%',
        skewX: ['0deg', '-5deg', '0deg'],
        transition: { duration: 0.5 },
      },
    },
    text: {
      hidden: { opacity: 0 },
      visible: { opacity: 1, transition: { delay: 0.4 } },
      done: { opacity: 0 },
    },
  }

  return (
    <motion.div
      className='anim-page _black'
      variants={page_anim.black}
      initial='hidden'
      animate='visible'
      exit='done'
    >
      <motion.div className='_red' variants={page_anim.red}></motion.div>
      <motion.h1 variants={page_anim.text} className='text-norm light'>
        LOADING...
      </motion.h1>
    </motion.div>
  )
}

export default AnimLoading

一点scss

.anim-page{
    &,._red{
      position: fixed;
      top: 0;
      width: 100%;
      height: 100%;
      z-index:999;
    }
    &._black{
      background-color: ${({ theme }) => theme.bg_light};
      box-shadow: 0 2px 7px ${({ theme }) => theme.shadow_1};
    }
    ._red{
      width: 70%;
      right: 0;
      background-color: ${({ theme }) => theme.bg_blue};
    }
    h1{
      line-height: 100vh;
      font-size:7vw;
      display: grid;
      place-items: center;
    }
  }

应用程序.js

const route = location.pathname.split('/')[1]
const [animate, setAnimate] = useState(false)

  useEffect(() => {
    setAnimate(true)
    setTimeout(() => {
      setAnimate(false)
    }, 1000)
  }, [route])

<AnimatePresence>{animate && <AnimLoading />}</AnimatePresence>

我正在使用 React Router,这种情况一直发生在路由更改......

于 2021-09-28T04:33:33.250 回答