当前位置:首页 > 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();

响应式气泡效果

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

js 实现气泡

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实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

js实现下拉菜单

js实现下拉菜单

使用HTML和CSS创建基础结构 HTML部分需要包含一个触发下拉的按钮和隐藏的下拉菜单内容: <div class="dropdown"> <button class="dr…

js实现定位

js实现定位

使用Geolocation API获取当前位置 在JavaScript中,可以通过浏览器内置的Geolocation API获取用户的地理位置信息。该API需要用户授权才能访问位置数据。 if (n…

js实现的游戏

js实现的游戏

JavaScript 游戏开发基础 JavaScript 是开发网页游戏的流行选择,因其无需插件即可在浏览器中运行。以下是一些关键技术和资源: HTML5 Canvas Canvas 提供了绘制图形…

js实现删除

js实现删除

使用 splice 方法删除数组元素 splice 方法可以删除数组中的元素,并返回被删除的元素。它接受两个参数:起始索引和要删除的元素数量。 const array = [1, 2, 3, 4…