简体   繁体   English

如何使用 React 在组件渲染中禁用元素的动画?

[英]How do I disable animation of a element in a component render using React?

I am studying web development and this afternoon I have coded this switch using React and styled-components, but when the page loads the animation from the button is played.我正在学习 Web 开发,今天下午我使用 React 和 styled-components 对这个开关进行了编码,但是当页面加载时,会播放来自按钮的动画。 How do I disable it from initially animating?如何禁用它最初的动画?

Ive tried playing around in the CSS styles but it isnt working very well.我试过使用 CSS 样式,但效果不佳。

I have uploaded a live version to CodeSandbox我已将实时版本上传到 CodeSandbox

https://codesandbox.io/s/translate-button-forked-8jfpu?file=/src/Button.jsx https://codesandbox.io/s/translate-button-forked-8jfpu?file=/src/Button.jsx

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

const toggle = keyframes`
      from {
        transform: translate(0);
      }
      to {
        transform: translate(149px);
      }`;
const unToggle = keyframes`
      from {
        transform: translate(149px);
      }
      to {
        transform: translate(0);
      }`;

const ButtonStyles = styled.div`
  margin-top: 1rem;
  width: 300px;
  height: 50px;
  background-color: #fff;
  border-radius: 50px;
  padding: 3px;
  cursor: pointer;

  .button-container {
    display: flex;
    height: inherit;
    width: inherit;
    align-items: center;

    .button-background {
      width: 150px;
      height: inherit;
      background-color: blue;
      border-radius: 50px;
      animation: 0.6s ease-in-out ${unToggle};
      animation-fill-mode: forwards;
      &.active {
        animation: 0.6s ease-in-out ${toggle};
        animation-fill-mode: forwards;
      }
    }

    .button-country {
      width: inherit;
      position: absolute;
      display: flex;
      justify-content: space-around;
      color: #979797;
      transition: 1.2s;
      span:nth-child(1) {
        transition: 1.2s;
        color: #fff;
      }
      &.white {
        transition: 1.2s;
        color: #fff;
        span:nth-child(1) {
          color: #979797;
        }
      }
    }
  }
`;

export default function Button() {
  const [toggleButton, setToggleButton] = useState("");
  const [labelColor, setLabelColor] = useState("");

  return (
    <ButtonStyles>
      <div
        onClick={() => {
          setToggleButton(!toggleButton ? "active" : "");
          setLabelColor(!labelColor ? "white" : "");
        }}
        className="button-container"
      >
        <div className={`button-background ${toggleButton}`}></div>
        <div className={`button-country ${labelColor}`}>
          <span>Brasil</span>
          <span>EUA</span>
        </div>
      </div>
    </ButtonStyles>
  );
}

Make it easier.让它更容易。 Use transition.使用过渡。 In the code below, I removed the keyframes and changed the ".button-background" class.在下面的代码中,我删除了关键帧并更改了“.button-background”类。

Now this class uses transition instead of animation.现在这个类使用过渡而不是动画。 This will now work the way you wanted it to.这现在将按照您希望的方式工作。

https://codesandbox.io/s/translate-button-forked-h1yow https://codesandbox.io/s/translate-button-forked-h1yow

.button-background {
  width: 150px;
  height: inherit;
  background-color: blue;
  border-radius: 100px;
  transition: 0.6s ease-in-out;

  &.active {
    transform: translate(100%);
  }
}

If you still want to use animation.如果你还想使用动画。 Then you can enable these animations depending on the class or prop passed in.然后你可以根据传入的类或道具启用这些动画。

You can see the code below, the ".button-background" class has been changed and the arguments passed to setToggleButton have been changed.您可以看到下面的代码,“.button-background”类已更改,传递给 setToggleButton 的参数已更改。

import React, { useState } from "react";
import styled, { keyframes } from "styled-components";

const toggle = keyframes`
    from {
      transform: translate(0);
    }

    to {
      transform: translate(100%);
    }
`;

const unToggle = keyframes`
    from {
      transform: translate(100%);
    }

    to {
      transform: translate(0);
    }
`;

const ButtonStyles = styled.div`
  margin-top: 1rem;
  width: 300px;
  height: 50px;
  background-color: #fff;
  border-radius: 100px;
  padding: 2px;
  cursor: pointer;

  .button-container {
    display: flex;
    height: inherit;
    width: inherit;
    align-items: center;

    .button-background {
      width: 150px;
      height: inherit;
      background-color: blue;
      border-radius: 100px;
      animation: 0.6s ease-in-out;
      animation-fill-mode: forwards;
      &.unToggle {
        animation-name: ${unToggle};
      }
      &.toggle {
        animation-name: ${toggle};
      }
    }

    .button-country {
      width: inherit;
      position: absolute;
      display: flex;
      justify-content: space-around;
      color: #979797;
      transition: 1.2s;
      span:nth-child(1) {
        transition: 1.2s;
        color: #fff;
      }
      &.white {
        transition: 1.2s;
        color: #fff;
        span:nth-child(1) {
          color: #979797;
        }
      }
    }
  }
`;

export default function Button() {
  const [toggleButton, setToggleButton] = useState("");
  const [labelColor, setLabelColor] = useState("");

  return (
    <ButtonStyles>
      <div
        onClick={() => {
          setToggleButton(toggleButton === 'toggle' ? "unToggle" : "toggle");
          setLabelColor(!labelColor ? "white" : "");
        }}
        className="button-container"
      >
        <div className={`button-background ${toggleButton}`}></div>
        <div className={`button-country ${labelColor}`}>
          <span>Brasil</span>
          <span>EUA</span>
        </div>
      </div>
    </ButtonStyles>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM