当前位置:首页 > JavaScript

js 实现气泡

2026-03-01 00:16:09JavaScript

气泡效果实现方法

在JavaScript中实现气泡效果通常涉及DOM操作和CSS样式设置。以下是几种常见方法:

使用CSS和JavaScript创建基础气泡

HTML结构:

<div class="bubble-container">
  <div class="bubble"></div>
</div>

CSS样式:

.bubble {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: rgba(0, 150, 255, 0.6);
  position: absolute;
  animation: float 3s infinite ease-in-out;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

JavaScript控制:

const container = document.querySelector('.bubble-container');
const bubble = document.querySelector('.bubble');

function createBubble() {
  const newBubble = bubble.cloneNode();
  newBubble.style.left = `${Math.random() * 100}%`;
  newBubble.style.opacity = Math.random() * 0.5 + 0.3;
  newBubble.style.width = `${Math.random() * 30 + 20}px`;
  newBubble.style.height = newBubble.style.width;
  container.appendChild(newBubble);

  setTimeout(() => {
    newBubble.remove();
  }, 3000);
}

setInterval(createBubble, 500);

使用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() {
    this.x = Math.random() * canvas.width;
    this.y = canvas.height + Math.random() * 100;
    this.size = Math.random() * 30 + 10;
    this.speed = Math.random() * 2 + 1;
    this.opacity = Math.random() * 0.5 + 0.3;
  }

  update() {
    this.y -= this.speed;
    if (this.y < -this.size) {
      this.y = canvas.height + this.size;
      this.x = Math.random() * canvas.width;
    }
  }

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

function init() {
  for (let i = 0; i < 50; i++) {
    bubbles.push(new Bubble());
  }
}

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

init();
animate();

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

使用CSS动画和变换

纯CSS方案结合少量JavaScript触发:

<div class="bubble-animation"></div>

CSS样式:

.bubble-animation {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}

.bubble {
  position: absolute;
  bottom: -50px;
  background-color: rgba(0, 150, 255, 0.5);
  border-radius: 50%;
  animation: rise 4s infinite;
}

@keyframes rise {
  0% {
    transform: translateY(0) scale(0.8);
    opacity: 0.8;
  }
  100% {
    transform: translateY(-400px) scale(1.2);
    opacity: 0;
  }
}

JavaScript控制:

function generateBubbles() {
  const container = document.querySelector('.bubble-animation');
  const bubbleCount = 20;

  for (let i = 0; i < bubbleCount; i++) {
    const bubble = document.createElement('div');
    bubble.classList.add('bubble');
    const size = Math.random() * 40 + 10;
    bubble.style.width = `${size}px`;
    bubble.style.height = `${size}px`;
    bubble.style.left = `${Math.random() * 100}%`;
    bubble.style.animationDelay = `${Math.random() * 5}s`;
    container.appendChild(bubble);
  }
}

generateBubbles();

使用第三方库实现高级效果

使用anime.js库创建更复杂的动画:

import anime from 'animejs';

function createAnimeBubbles() {
  const container = document.getElementById('bubble-container');

  for (let i = 0; i < 15; i++) {
    const bubble = document.createElement('div');
    bubble.className = 'anime-bubble';
    container.appendChild(bubble);

    anime({
      targets: bubble,
      translateY: [-50, -300],
      translateX: () => anime.random(-30, 30),
      scale: [0.8, 1.2],
      opacity: [0.8, 0],
      duration: 3000,
      delay: i * 200,
      easing: 'easeOutQuad',
      complete: () => {
        bubble.remove();
        createAnimeBubbles();
      }
    });
  }
}

createAnimeBubbles();

响应式气泡效果

确保气泡在不同屏幕尺寸下正常工作:

function responsiveBubbles() {
  const canvas = document.getElementById('responsiveCanvas');
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;

  // 之前的Canvas气泡代码
  // ...
}

window.addEventListener('resize', responsiveBubbles);
window.addEventListener('load', responsiveBubbles);

以上方法提供了从简单到复杂的多种实现方式,可根据项目需求选择合适的方案。DOM操作适合少量气泡,Canvas适合大量气泡性能要求高的场景,第三方库则能实现更复杂的动画效果。

js 实现气泡

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

相关文章

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

js 实现倒计时

js 实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('imag…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式…

js实现换肤

js实现换肤

使用CSS变量实现换肤 通过CSS变量可以轻松实现主题切换功能。CSS变量在根元素中定义,通过JavaScript动态修改这些变量值。 :root { --primary-color: #34…