【问题标题】:animate div width from left to right从左到右动画 div 宽度
【发布时间】:2021-11-16 20:57:44
【问题描述】:

我是动画新手,但我想为骨架加载器制作动画。我希望它以 0% 的宽度从左侧开始,然后以 100% 的宽度向右移动,然后让加载程序变为零但反向,以便它以 0% 的宽度在右侧结束。我的问题是我无法让动画从左到右消失。我正在使用带有样式的组件的反应。这是我的代码:

const reveal = keyframes`
  0% {
    width: 0%;
  }
  50% {
    width: 100%;
  }
  100% {
    width: -100%;
  }
`;

const StyledSkeleton = styled.div`
  height: 40px;
  font-size: 64px;
  line-height: 70px;
  overflow: hidden;
  background-color: ${colors.LOADER_BACKGROUND};
  background-size: 100%;

  animation-name: ${reveal};
  animation-duration: 2s;
  animation-timing-function: ease-in;
  animation-delay: 0s;
  animation-iteration-count: infinite;
  animation-direction: normal;
`;

【问题讨论】:

    标签: javascript css animation styled-components


    【解决方案1】:

    您可以使用绝对位置为加载器的大小设置动画,而不是使用宽度。

    .container {
      width: 100%;
      height: 40px;
      position: relative;
    }
    
    .loader {
      position: absolute;
      top: 0;
      bottom: 0;
      background-color: red;
      animation-name: animate;
      animation-duration: 2s;
      animation-timing-function: ease-in;
      animation-iteration-count: infinite;
    }
    
    @keyframes animate {
       0% {
        left: 0;
        right: 100%;
      }
      50% {
        left: 0;
        right: 0;
      }
      100% {
        left: 100%;
        right: 0;
      }
    }
    <div class='container'>
      <div class='loader'></div>
    </div>

    【讨论】:

    • loader div 可以生孩子吗?我正在尝试使用它来“显示”一些文本,但我不能这样做
    • 我也在尝试在 flexbox 行中使用它,这会造成一些麻烦。
    • 请发布您的代码。
    • 我现在可以使用它了。我将提供我的代码作为替代答案。
    【解决方案2】:

    这就是我最终实现它的方式。其他人可能有不同的用例,但@yeniv 的回答使我能够找到这个解决方案。我正在将 NextJS 与 TypeScript 和样式化组件一起使用。

    import React from 'react';
    import styled, { keyframes } from 'styled-components';
    
    import { colors } from '../../../styles';
    
    
    interface SkeletonProps {
      delay?: number;
      style?: object;
      children?: React.ReactNode;
    }
    
    export const Skeleton = ({ delay, children, style = {} }: SkeletonProps): JSX.Element => {
    
      return (
        <div>
          <Container style={{...style}}>
            <TextContainer>
              {children && children}
            </TextContainer>
            <StyledSkeleton delay={delay} />
          </Container>
        </div>
      );
    };
    
    
    export default Skeleton;
    
    
    
    
    
    const reveal = keyframes`
      0% {
        display: none;
        opacity: 0;
      }
      50% {
        opacity: 0;
      }
      100% {
        display: inline-block;
        opacity: 1;
      }
    `;
    
    const animate = keyframes`
      0% {
       left: 0;
       right: 100%;
     }
     50% {
       left: 0;
       right: 0;
     }
     100% {
       left: 100%;
       right: 0;
     }
    `;
    
    const Container = styled.div`
      position: relative;
      height: 40px;
      width: 100%;
    `;
    
    interface TextContainerProps {
      delay?: number;
    }
    
    const TextContainer = styled.span<TextContainerProps>`
      animation-name: ${reveal};
      animation-duration: 2s;
      animation-delay: ${props => `${props.delay}s`};
      animation-timing-function: ease-in;
      animation-iteration-count: 1;
    `;
    
    interface StyledSkeletonProps {
      delay?: number;
    }
    
    const StyledSkeleton = styled.div<StyledSkeletonProps>`
      position: absolute;
      top: 0;
      bottom: 0;
      overflow: hidden;
    
      background-color: ${colors.LOADER_BACKGROUND};
      background-size: 100%;
    
      animation-name: ${animate};
      animation-duration: 1.5s;
      animation-delay: ${props => `${props.delay}s`};
      animation-timing-function: ease-in;
      animation-iteration-count: 1;
    `;
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2022-07-09
      相关资源
      最近更新 更多