当前位置:首页 > JavaScript

js实现气泡效果

2026-03-01 09:33:56JavaScript

使用CSS和JavaScript创建气泡效果

气泡效果可以通过CSS动画和JavaScript动态生成元素来实现。以下是一个简单的实现方法:

CSS样式部分

js实现气泡效果

.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实现部分

js实现气泡效果

function createBubble(event) {
  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 = `${event.clientX - size/2}px`;
  bubble.style.top = `${event.clientY - size/2}px`;

  document.body.appendChild(bubble);

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

document.addEventListener('mousemove', createBubble);

使用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(x, y) {
    this.x = x;
    this.y = y;
    this.size = Math.random() * 20 + 5;
    this.speed = Math.random() * 2 + 1;
    this.opacity = Math.random() * 0.5 + 0.3;
    this.angle = Math.random() * Math.PI * 2;
  }

  update() {
    this.y -= this.speed;
    this.x += Math.sin(this.angle) * 0.5;
    this.angle += 0.01;
    this.opacity -= 0.005;
  }

  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 animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  bubbles.forEach((bubble, index) => {
    bubble.update();
    bubble.draw();

    if(bubble.opacity <= 0) {
      bubbles.splice(index, 1);
    }
  });

  requestAnimationFrame(animate);
}

document.addEventListener('mousemove', (e) => {
  bubbles.push(new Bubble(e.clientX, e.clientY));
});

animate();

气泡效果的优化建议

调整气泡大小、速度和透明度范围可以创建不同的视觉效果。增加随机颜色可以增强视觉吸引力:

// 在Bubble类中添加颜色属性
this.color = `hsl(${Math.random() * 360}, 100%, 80%)`;

// 修改draw方法
ctx.fillStyle = `hsla(${this.color}, ${this.opacity})`;

对于性能优化,可以限制同时存在的气泡数量,或者在移动端减少气泡生成频率。

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

相关文章

h5所能实现的效果

h5所能实现的效果

H5能实现的效果 H5(HTML5)作为最新的HTML标准,具备强大的功能和丰富的API支持,能够实现多种现代Web应用效果。以下是H5的主要能力分类: 多媒体支持 H5原生支持音频和视频播放,无需…

uniapp 气泡

uniapp 气泡

uniapp 气泡实现方法 在 uniapp 中实现气泡效果可以通过多种方式,以下是常见的几种方法: 使用 CSS 样式实现气泡 通过 CSS 的 border 和 ::after 伪元素可以创建气…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果通常涉及动态样式绑定、动画过渡或第三方库的使用。以下是几种常见实现方式: 动态类名绑定 通过 v-bind:class 或简写 :class 实…

vue实现凤凰效果

vue实现凤凰效果

Vue 实现凤凰效果 凤凰效果通常指一种视觉动画,模拟凤凰展翅、火焰燃烧等动态效果。在 Vue 中实现这种效果,可以结合 CSS 动画、Canvas 或第三方动画库(如 GSAP)。以下是几种实现方式…

vue实现烟花效果

vue实现烟花效果

Vue 实现烟花效果的方法 在Vue中实现烟花效果可以通过结合Canvas绘图和动画技术来完成。以下是一种常见的实现方式: 创建Vue组件 新建一个名为Fireworks.vue的组件,核心逻辑如下…

vue实现筛选效果

vue实现筛选效果

Vue 实现筛选效果的方法 基于计算属性的筛选 在 Vue 中可以通过计算属性实现数据的动态筛选。定义一个计算属性,根据用户输入的筛选条件返回过滤后的数据列表。 <template>…