当前位置:首页 > JavaScript

js实现气泡效果

2026-04-04 10:14:35JavaScript

使用CSS和JavaScript创建气泡效果

通过CSS动画结合JavaScript动态生成元素,可以实现气泡上升的视觉效果。以下是实现方法:

HTML基础结构

js实现气泡效果

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

CSS样式设置

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

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

JavaScript实现逻辑

js实现气泡效果

function createBubble() {
  const container = document.getElementById('bubble-container');
  const bubble = document.createElement('div');
  bubble.className = 'bubble';

  // 随机大小
  const size = Math.random() * 30 + 10;
  bubble.style.width = `${size}px`;
  bubble.style.height = `${size}px`;

  // 随机位置
  bubble.style.left = `${Math.random() * 100}%`;

  // 随机动画时长
  const duration = Math.random() * 3 + 2;
  bubble.style.animationDuration = `${duration}s`;

  container.appendChild(bubble);

  // 动画结束后移除元素
  bubble.addEventListener('animationend', () => {
    bubble.remove();
  });
}

// 定期生成气泡
setInterval(createBubble, 300);

鼠标交互气泡效果

实现跟随鼠标移动或点击产生气泡的交互效果:

document.addEventListener('mousemove', (e) => {
  const bubble = document.createElement('div');
  bubble.className = 'bubble';

  // 设置位置为鼠标位置
  bubble.style.left = `${e.clientX}px`;
  bubble.style.top = `${e.clientY}px`;

  // 随机大小和动画
  const size = Math.random() * 20 + 5;
  bubble.style.width = `${size}px`;
  bubble.style.height = `${size}px`;
  bubble.style.animationDuration = `${Math.random() * 2 + 1}s`;

  document.body.appendChild(bubble);

  // 动画结束后移除
  bubble.addEventListener('animationend', () => {
    bubble.remove();
  });
});

Canvas高性能气泡实现

对于需要大量气泡的场景,使用Canvas性能更佳:

const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.style.position = 'fixed';
canvas.style.top = '0';
canvas.style.left = '0';
canvas.style.pointerEvents = 'none';
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const ctx = canvas.getContext('2d');
const bubbles = [];

class Bubble {
  constructor() {
    this.x = Math.random() * canvas.width;
    this.y = canvas.height + Math.random() * 50;
    this.size = Math.random() * 20 + 5;
    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 + Math.random() * 50;
      this.x = Math.random() * canvas.width;
    }
  }

  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();
  }
}

// 初始化气泡
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);
}

animate();

这些方法可以根据具体需求调整参数,如气泡大小、速度、透明度等,以获得理想的视觉效果。Canvas实现适合大量粒子场景,而CSS动画方案更适合简单的UI增强效果。

标签: 气泡效果
分享给朋友:

相关文章

uniapp 卡片效果

uniapp 卡片效果

uniapp 实现卡片效果的方法 使用 view 和 CSS 样式 通过 view 组件结合 CSS 样式可以快速实现卡片效果。设置圆角、阴影和边距来增强视觉层次感。 <view cl…

vue实现过渡效果

vue实现过渡效果

Vue 过渡效果实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于在元素或组件进入/离开 DOM 时应用过渡效果。以…

vue实现检索效果

vue实现检索效果

Vue实现检索功能的方法 在Vue中实现检索功能通常需要结合数据绑定、计算属性和方法。以下是几种常见的实现方式: 基础实现(基于计算属性) <template> <div>…

vue实现钟表效果

vue实现钟表效果

Vue 实现钟表效果 创建基础结构 在 Vue 项目中创建一个组件,用于显示钟表。组件的模板部分包含钟表的外观,如时针、分针、秒针和钟面。 <template> <div cl…

vue实现爆炸图效果

vue实现爆炸图效果

实现爆炸图效果的方法 爆炸图(Exploded View)常用于展示产品或组件的拆解效果,以下是基于Vue的实现方案: 使用CSS 3D变换实现基础爆炸效果 通过CSS的transform属性控制元…

vue实现图片轮播效果

vue实现图片轮播效果

使用Vue实现图片轮播效果 基本轮播实现 安装vue-awesome-swiper库,这是一个基于Swiper的Vue轮播组件。通过npm或yarn安装: npm install swiper vu…