我try 用HTML和CSS重新创建这个动画,并使用内联样式与Typescript进行交互.我正在使用样式信息创建一个对象,并在"样式"属性中引用它.代码如下.它不起作用,我不确定我做错了什么,我怀疑这些风格没有被正确定义和引用?

下面是我试图重写的原始代码笔示例:Apple Animation

这是我的密码

import React from 'react';

const styles = {
    
    '@keyframes showTopText': {
        '0%': { transform: 'translate3d(0, 100%, 0)' },
        '40%, 60%': { transform: 'translate3d(0, 50%, 0)' },
        '100%': { transform: 'translate3d(0, 0, 0)' },
    },
    '@keyframes showBottomText': {
        '0%': { transform: 'translate3d(0, -100%, 0)' },
        '100%': { transform: 'translate3d(0, 0, 0)' },
    },

    animatedTitle: {
        color: '#222',
        fontFamily: 'Roboto, Arial, sans-serif',
        height: '90vmin',
        left: '50%',
        top: '50%',
        transform: 'translate(-50%, -50%)',
        width: '90vmin',
    },
    'animatedTitle > div': {
        height: '50%',
        overflow: 'hidden',
        position: 'absolute',
        width: '100%',
    },
    'animatedTitle > div div': {
        fontSize: '12vmin',
        padding: '2vmin 0',
        position: 'absolute',
    },
    'animatedTitle > div div span': {
        display: 'block',
    },
    'animated-title > div.text-top': {
        borderBottom: '1vmin solid #000',
        top: 0,
    },
    'animatedTitle > div.text-top div': {
        animation: 'showTopText 1s',
        animationDelay: '0.5s',
        animationFillMode: 'forwards',
        bottom: 0,
        transform: 'translate(0, 100%)',
    },
    'animatedTitle > div.text-top div span:first-child': {
        color: '#767676',
    },
    'animatedTitle > div.text-bottom': {
        bottom: 0,
    },
    'animatedTitle > div.text-bottom div': {
        animation: 'showBottomText 0.5s',
        animationDelay: '1.75s',
        animationFillMode: 'forwards',
        top: 0,
        transform: 'translate(0, -100%)',
    },
};

function Design() {
    return (
        <div style={styles.animatedTitle}>
            <div style={styles['animatedTitle > div.text-top div']}>
                <div>
                    <span>mimicking</span>
                    <span>apple's design</span>
                </div>
            </div>
            <div style={styles['animatedTitle > div.text-bottom']}>
                <div>for the win!</div>
            </div>
        </div>
    );
}

export { Design };

推荐答案

您可以try 使用styled-components来保持组件更加灵活和兼容.我用styled-components复制了codepen示例,并使其易于扩展.

因此,TopAnimateBlockBottomAnimateBlocknumOfLine个属性,因此块内有多少行.BottomAnimateBlock中的第二个属性是delayTopLine,它应该与TopAnimateBlock中的numOfLine具有相同的数字,因为我们需要等待顶线播放.

此外,您可以在TextStyle中使用color属性轻松更改文本 colored颜色 ,并将其传递为 colored颜色 值、HEX colored颜色 或rgba()/hsla().

文本动画.tsx公司

import styled, { keyframes } from 'styled-components';

const showTopText = keyframes`
  0% { transform: translate3d(0, 100% , 0); }
  40%, 60% { transform: translate3d(0, 50%, 0); }
  100% { transform: translate3d(0, 0, 0); }
`;
const showBottomText = keyframes`
  0% { transform: translate3d(0, -100%, 0); }
  100% { transform: translate3d(0, 0, 0); }
`;

const Section = styled.section`
  width: calc(100% + 10vmin);
  display: flex;
  flex-flow: column;
  padding: 2vmin 0;
  overflow: hidden;
  &:last-child {
    border-top: 1vmin solid white;
  }
`;

const Block = styled.div<{ numOfLine: number }>`
  position: relative;
`;
const TopAnimateBlock = styled(Block)`
  animation: ${showTopText} calc(0.5s * ${props => props.numOfLine}) forwards;
  animation-delay: 0.5s;
  transform: translateY(calc(100% * ${props => props.numOfLine}));
`;
const BottomAnimateBlock = styled(Block)<{ delayTopLine: number }>`
  animation: ${showBottomText} calc(0.5s * ${props => props.numOfLine}) forwards;
  animation-delay: calc(0.7s * ${props => props.delayTopLine});
  transform: translateY(calc(-100% * ${props => props.numOfLine}));
`;

const TextStyle = styled.p<{ color: string }>`
  font-family: Roboto, Arial, sans-serif;
  font-size: 12vmin;
  color: ${props => props.color};
`;

export const TextAnimation = () => {
  return (
    <>
      <Section>
        <TopAnimateBlock numOfLine={2}>
          <TextStyle color="grey">mimicking</TextStyle>
          <TextStyle color="white">apple's design</TextStyle>
        </TopAnimateBlock>
      </Section>
      <Section>
        <BottomAnimateBlock numOfLine={1} delayTopLine={2}>
          <TextStyle color="white">for the win!</TextStyle>
        </BottomAnimateBlock>
      </Section>
    </>
  );
};

Edit dazziling-code

If we want to animate 3 lines instead of 2, just add / change:

  1. 添加新的TextStyle组件
  2. 从2更改为3 TopAnimateBlock中的numOfLineBottomAnimateBlock中的delayTopLine
  3. 并将关键帧替换到此片段:
const showTopText = keyframes`
  0% { transform: translate3d(0, 100% , 0); }
  25%, 40% { transform: translate3d(0, 66%, 0); }
  60%, 75% { transform: translate3d(0, 33%, 0); }
  100% { transform: translate3d(0, 0, 0); }
`;

Html相关问答推荐

打印pdf时100 vh迪夫有半页高

我需要主页按钮出现在中间

如何从ThymeLeaf模板中分离CSS

Angular Project中的星级 Select

如何在所有屏幕大小下使带有背景图像的伪元素作为标题的衬底?

我应该怎么做才能显示出整个字符串?

如何创建带有粘性列和标题的网格?

如何将内容元素放在侧边栏旁边?

有没有什么方法可以很容易地给一个SVG一个插框阴影?

如何在css中在span中绘制圆弧?

为什么一个 CSS 网格框比其他网格框低?

UL 的行为就像它不属于任何类别

使用简单的 HTML 设计公司徽标和文本

在HTML请求中添加源URL

获取链接到功能的网页表单

CSS 字母间距将按钮向右扩展

在包含 html 标签的字符串的子字符串中应用不同的样式

辅助功能:alt 或 alt="" 用于装饰图像

CSS 变量不适用于自定义属性回退值

CSS:如何在模糊的背景上剪切文本?