当前位置:首页 > 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结构:

js 实现气泡

<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样式:

js 实现气泡

.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如何实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例能够访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js画图实现

js画图实现

使用Canvas API绘制图形 Canvas是HTML5提供的绘图API,通过JavaScript操作Canvas元素可以绘制各种图形。以下是一个简单的示例: <canvas id="myC…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…

js实现上传图片

js实现上传图片

使用HTML5的File API实现图片上传 HTML5的File API允许通过JavaScript访问用户选择的文件。需要创建一个文件输入元素,并监听其change事件。 <input t…

js实现图片放大

js实现图片放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性实现图片放大效果。当鼠标悬停时,图片会平滑放大。 const img = document…

js 实现递归

js 实现递归

递归的基本概念 递归是指函数直接或间接调用自身的过程。在JavaScript中,递归通常用于解决可以分解为相似子问题的问题,如阶乘、斐波那契数列、树形结构遍历等。 递归的实现要点 基线条件(Base…