返回

React 气泡文字 Logo 特效:缤纷动态,创意无限

前端

1. 构建 React 组件

首先,我们创建一个新的 React 组件,用于定义文字 Logo 和动画效果。组件的结构如下:

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

const LogoContainer = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
`;

const LogoText = styled.h1`
  font-size: 100px;
  font-weight: bold;
`;

const LogoBubbles = styled.div`
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
`;

const Bubble = styled.div`
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: #f4f4f4;
  animation: bubble-animation 2s infinite;
`;

const Logo = () => {
  const [bubbles, setBubbles] = useState([]);

  useEffect(() => {
    setInterval(() => {
      setBubbles([...bubbles, { x: Math.random() * window.innerWidth, y: Math.random() * window.innerHeight }]);
    }, 1000);
  }, [bubbles]);

  return (
    <LogoContainer>
      <LogoText>Logo</LogoText>
      <LogoBubbles>
        {bubbles.map((bubble, index) => (
          <Bubble key={index} style={{ top: bubble.y, left: bubble.x }} />
        ))}
      </LogoBubbles>
    </LogoContainer>
  );
};

export default Logo;

2. 自定义样式

使用 CSS3 定义样式来实现动画效果和视觉呈现:

@keyframes bubble-animation {
  0% {
    transform: scale(0);
    opacity: 0;
  }

  50% {
    transform: scale(1);
    opacity: 1;
  }

  100% {
    transform: scale(0);
    opacity: 0;
  }
}

3. 添加气泡动画

在 React 组件中,利用 useState 钩子管理气泡状态,并通过 useEffect 钩子不断创建新的气泡。

const [bubbles, setBubbles] = useState([]);

useEffect(() => {
  setInterval(() => {
    setBubbles([...bubbles, { x: Math.random() * window.innerWidth, y: Math.random() * window.innerHeight }]);
  }, 1000);
}, [bubbles]);

4. 渲染气泡

在组件中使用 map 方法渲染气泡元素,并根据每个气泡的位置设置样式。

{bubbles.map((bubble, index) => (
  <Bubble key={index} style={{ top: bubble.y, left: bubble.x }} />
))}

5. 应用场景

该特效可用于多种场景,例如:

  • 网站或应用程序的 Logo 展示
  • 活动或展览中的动态视觉元素
  • 社交媒体上的创意内容
  • 产品介绍中的动态效果

6. 结语

通过 React 和 CSS3,我们创造了一个动态的文字 Logo 气泡特效。这是一个有趣且富有创意的项目,可以为您的项目增添独特性和趣味性。