简体   繁体   English

动画不适用于 React 组件

[英]Animation not working with React Component

I am using react to render a span element with the class of "wave".我正在使用 react 来渲染具有“wave”类的 span 元素。 I want the span to be animated to slowly wave, but that is not happening.我希望跨度动画缓慢地摆动,但这并没有发生。 Here is my css:这是我的CSS:

@keyframes wave {
    0% { transform: rotate(5deg); }
    10% { transform: rotate(-5deg); }
    20% { transform: rotate(5deg); }
    30% { transform: rotate(-5deg); }
    40% { transform: rotate(5deg); }
    50% { transform: rotate(-5deg); }
    60% { transform: rotate(5deg); }
    100% { transform: rotate(5deg); }
}

span.wave {
  animation: wave 5s infinite;
}

here is my jsx:这是我的 jsx:

import React, { useState } from 'react';
import './App.css';

function App() {
  const [count, incrementCount] = useState(0);

  return (
    <div>
      <div id="buttonClass" className="centered">
        <h1 className="noselect">You have</h1>
        <input id="nameInput" placeholder="Clicked (edit me)" autoFocus autocomplete="off"></input>
        <h1 className="noselect"><span className="wave">🎉</span> {count} times! <span className="wave">🎉</span></h1>
      </div>
      <div className="btnrow">
        <button className="button1 noselect inrow" onClick={() => incrementCount(count + 1)}>+</button>
        <button className="button1 noselect inrow" onClick={() => incrementCount(count - 1)}>-</button>
      </div>
      <div className="btnrow">
        <button className="button2 noselect ten" onClick={() => incrementCount(count + 10)}>+10</button>
        <button className="button2 noselect ten" onClick={() => incrementCount(count - 10)}>-10</button>
        <button className="button2 noselect ten" onClick={() => incrementCount(count + 100)}>+100</button>
        <button className="button2 noselect ten" onClick={() => incrementCount(count - 100)}>-100</button>
      </div>
      <div id="center">
      <button className="button2 noselect" id="resetbtn" onClick={() => incrementCount(0)}>Reset Counter</button>
      </div>
    </div>
  );
}

export default App;

The span elements are appearing, but not animated.跨度元素出现,但没有动画。

add display: inline-block;添加display: inline-block; . . Inline elements behave like text, while block elements behave like containers.内联元素的行为类似于文本,而块元素的行为类似于容器。 You cannot rotate text characters, but you can rotate their container您不能旋转文本字符,但可以旋转它们的容器

span.wave {
  display: inline-block;
  animation: wave 5s infinite;
}

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

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