React Visual - 带波浪效果的按钮

本贴最后更新于 2019 天前,其中的信息可能已经时异事殊

2019-10-12

描述

渲染一个当点击后有波浪动画效果的按钮。

  • 为波浪效果定义一些适当的 CSS 样式和动画
  • 使用 React.useState() hook 为坐标和按钮的动画状态创建变量
  • 使用 React.useEffect() hook 来更新动画状态,每当坐标状态变量修改时,就开始进行动画
  • 在动画播放结束后,使用前一个 hook 中的 Use setTimeout() 来对动画进行清除
  • 每当 isRippling 状态变量为 false 时,再次使用 React.useEffect() hook 对 coords 进行重置
  • onClick 事件中需要更新 coords 状态变量并调用传递的回调方法
  • 最后渲染一个包含一个或两个 <span> 元素的 <button> 元素,基于 coords 状态变量设置 .ripple 元素的位置

实现

.ripple-button { border-radius: 4px; border: none; margin: 8px; padding: 14px 24px; background: #1976d2; color: #fff; overflow: hidden; position: relative; cursor: pointer; } .ripple-button > .ripple { width: 20px; height: 20px; position: absolute; background: #63a4ff; display: block; content: ""; border-radius: 9999px; opacity: 1; animation: 1.2s ease 1 forwards ripple-effect; } @keyframes ripple-effect { 0% { transform: scale(1); opacity: 1; } 50% { transform: scale(10); opacity: 0.375; } 100% { transform: scale(35); opacity: 0; } } .ripple-button > .content { position: relative; z-index: 2; }
function RippleButton({ children, onClick }) { const [coords, setCoords] = React.useState({ x: -1, y: -1 }); const [isRippling, setIsRippling] = React.useState(false); React.useEffect( () => { if (coords.x !== -1 && coords.y !== -1) { setIsRippling(true); setTimeout(() => setIsRippling(false), 1200); } else setIsRippling(false); }, [coords] ); React.useEffect( () => { if (!isRippling) setCoords({ x: -1, y: -1 }); }, [isRippling] ); return ( <button className="ripple-button" onClick={e => { var rect = e.target.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; setCoords({ x, y }); onClick && onClick(e); }} > {isRippling ? ( <span className="ripple" style={{ left: coords.x + 10, top: coords.y }} /> ) : ( "" )} <span className="content">{children}</span> </button> ); }

使用

ReactDOM.render( <RippleButton onClick={e => console.log(e)}>Click me</RippleButton>, document.getElementById('root') );

返回总目录

每天 30 秒系列之 React

  • 30Seconds

    📙 前端知识精选集,包含 HTML、CSS、JavaScript、React、Node、安全等方面,每天仅需 30 秒。

    • 精选常见面试题,帮助您准备下一次面试
    • 精选常见交互,帮助您拥有简洁酷炫的站点
    • 精选有用的 React 片段,帮助你获取最佳实践
    • 精选常见代码集,帮助您提高打码效率
    • 整理前端界的最新资讯,邀您一同探索新世界
    488 引用 • 384 回帖 • 9 关注
  • React

    React 是 Facebook 开源的一个用于构建 UI 的 JavaScript 库。

    192 引用 • 291 回帖 • 381 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
Vanessa
我们终此一生,就是要摆脱他人的期待,找到真正的自己。 昆明