当前位置:首页 > JavaScript

js 实现气泡

2026-01-30 09:11:28JavaScript

使用 CSS 和 JavaScript 创建气泡效果

通过 CSS 定义气泡样式,结合 JavaScript 动态生成气泡元素并添加动画效果。

CSS 样式定义

.bubble {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.6);
  pointer-events: none;
  animation: float 4s ease-in-out infinite;
}

@keyframes float {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(-100px) rotate(360deg); opacity: 0; }
}

JavaScript 生成气泡

function createBubble(x, y) {
  const bubble = document.createElement('div');
  bubble.classList.add('bubble');

  const size = Math.random() * 30 + 10;
  bubble.style.width = `${size}px`;
  bubble.style.height = `${size}px`;
  bubble.style.left = `${x}px`;
  bubble.style.top = `${y}px`;

  document.body.appendChild(bubble);

  setTimeout(() => {
    bubble.remove();
  }, 4000);
}

document.addEventListener('mousemove', (e) => {
  createBubble(e.clientX, e.clientY);
});

使用 Canvas 绘制气泡

通过 HTML5 Canvas 实现更高效的批量气泡渲染。

HTML 结构

<canvas id="bubbleCanvas"></canvas>

JavaScript 实现

const canvas = document.getElementById('bubbleCanvas');
const ctx = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const bubbles = [];

class Bubble {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.size = Math.random() * 20 + 10;
    this.speed = Math.random() * 2 + 1;
    this.opacity = Math.random() * 0.5 + 0.3;
    this.angle = Math.random() * 360;
  }

  update() {
    this.y -= this.speed;
    this.angle += 2;
    if (this.y < -this.size) {
      this.y = canvas.height + this.size;
    }
  }

  draw() {
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;
    ctx.fill();
  }
}

function init() {
  for (let i = 0; i < 50; i++) {
    bubbles.push(new Bubble(
      Math.random() * canvas.width,
      Math.random() * canvas.height
    ));
  }
}

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  bubbles.forEach(bubble => {
    bubble.update();
    bubble.draw();
  });
  requestAnimationFrame(animate);
}

window.addEventListener('resize', () => {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
});

init();
animate();

使用 CSS 伪元素创建气泡背景

纯 CSS 方案实现静态气泡背景效果。

CSS 实现

body {
  position: relative;
  overflow: hidden;
  height: 100vh;
}

body::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at 30% 30%, 
    rgba(255, 255, 255, 0.8) 0%, 
    rgba(255, 255, 255, 0) 20%);
  background-size: 100px 100px;
  opacity: 0.4;
  pointer-events: none;
}

使用 GSAP 实现高级动画

通过 GSAP 动画库创建更复杂的气泡效果。

JavaScript 实现

js 实现气泡

import { gsap } from 'gsap';

function createAnimatedBubble() {
  const bubble = document.createElement('div');
  bubble.className = 'bubble';

  const size = Math.random() * 40 + 20;
  bubble.style.width = `${size}px`;
  bubble.style.height = `${size}px`;

  document.body.appendChild(bubble);

  gsap.fromTo(bubble, 
    { 
      x: Math.random() * window.innerWidth,
      y: window.innerHeight + size,
      opacity: 0.8 
    },
    {
      y: -size,
      opacity: 0,
      duration: Math.random() * 3 + 2,
      ease: 'power1.out',
      onComplete: () => bubble.remove()
    }
  );
}

setInterval(createAnimatedBubble, 300);

以上方案可根据具体需求选择使用,DOM 方案适合少量气泡交互,Canvas 方案适合大量气泡性能优化,GSAP 方案适合复杂动画效果。

标签: 气泡js
分享给朋友:

相关文章

js实现

js实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let co…

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document.…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(f…

js实现求导

js实现求导

实现数值求导的方法 在JavaScript中实现求导通常采用数值方法,因为JavaScript不是符号计算语言。以下是常见的数值微分方法: 中心差分法 中心差分法提供较高精度的导数近似:…