css - 使用 React 在点击区域周围制作小动画(最好只使用 CSS)

标签 css reactjs animation onclick

我正在寻找一种方法来在我点击的区域周围显示一个小动画,例如,一个先扩大然后缩小直到消失的圆圈。看起来简单的部分是创建动画,这将是一个 CSS 过渡,对我来说困难的是让某些东西出现在我点击的地方(使用 CSS)。

如果没有基于 CSS 的解决方案,我想知道如何使用 React 解决。

谢谢

最佳答案

您可以使用 react-transition-group 中的 CSSTransition。 您正在寻找的一个小例子可能如下所示。

import React from "react";
import "./Style.css";
import { CSSTransition } from "react-transition-group";

export default class Modal extends React.Component {
   state = {
     animate: false
   }
   render() {
    return (
        <React.Fragment>
            <CSSTransition
                in={this.state.animate}
                classNames="animate-circle"
                timeout={500}
            >
                <div 
                    className="circle" 
                    onClick={()=>this.setState({animate: animate ? false : true})} 
                >
                        Click to expand and click again to diminish
                </div>
            </CSSTransition>
        </React.Fragment>
    )
  }
}

Style.css 应该编写类似这样的代码

.circle {
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background-color: red;
}
.animate-circle-enter-active {
    width: 500px;
    height: 500px;
    transition: all 500ms infinite;
}
.animate-circle-enter-done {
    width: 500px;
    height: 500px;
}
.animate-circle-exit {
    width: 500px;
    height: 500px;
}
.animate-circle-exit-active {
    width: 0px;
    height: 0px;
    transition: all 500ms infinite;
}
.animate-circle-exit-done {
    width: 0px;
    height: 0px;
}

关于css - 使用 React 在点击区域周围制作小动画(最好只使用 CSS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51584959/

相关文章:

html - 在有序列表 CSS/HTML 中删除数字

reactjs - URL 更改无需在 React 路由器中重新渲染

iphone - 停止 iPhone 调整 HTML 电子邮件大小的问题

html - 媒体查询在本地工作,但上传到网站时不起作用

javascript - 使用 Flux 构建一个编辑表单,实际上是谁将数据 POST 到服务器 : actions, 存储、 View ?

javascript - 在 jQuery 中动画元素时需要一个事件

javascript - jQuery 动画回调不起作用

iphone - iOS:如何在两个移动的物体之间画一条线?

css - 设计 CSS 样式

javascript - 仅使用 react 导航的自定义侧边栏组件出错