当前位置:首页 > VUE

vue实现气泡效果

2026-01-14 07:40:12VUE

实现气泡效果的方法

在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法:

使用纯CSS动画

通过CSS的@keyframestransform属性创建气泡上升动画:

<template>
  <div class="bubble-container">
    <div v-for="(bubble, index) in bubbles" :key="index" class="bubble" :style="bubble.style"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bubbles: []
    }
  },
  mounted() {
    setInterval(() => {
      this.createBubble()
    }, 300)
  },
  methods: {
    createBubble() {
      const size = Math.random() * 30 + 10
      const bubble = {
        size,
        style: {
          width: `${size}px`,
          height: `${size}px`,
          left: `${Math.random() * 100}%`,
          animationDuration: `${Math.random() * 3 + 2}s`
        }
      }
      this.bubbles.push(bubble)
      setTimeout(() => {
        this.bubbles.shift()
      }, 5000)
    }
  }
}
</script>

<style>
.bubble-container {
  position: relative;
  height: 300px;
  overflow: hidden;
}

.bubble {
  position: absolute;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.6);
  border-radius: 50%;
  animation: rise linear infinite;
}

@keyframes rise {
  0% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
  100% {
    transform: translateY(-300px) scale(0.5);
    opacity: 0;
  }
}
</style>

使用GSAP动画库

GSAP提供更强大的动画控制能力:

vue实现气泡效果

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

<script>
import { gsap } from 'gsap'

export default {
  mounted() {
    setInterval(() => {
      this.createBubble()
    }, 500)
  },
  methods: {
    createBubble() {
      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}%`

      this.$refs.container.appendChild(bubble)

      gsap.to(bubble, {
        y: -300,
        opacity: 0,
        scale: 0.5,
        duration: Math.random() * 3 + 2,
        onComplete: () => {
          bubble.remove()
        }
      })
    }
  }
}
</script>

<style>
.bubble-container {
  position: relative;
  height: 300px;
  overflow: hidden;
}

.bubble {
  position: absolute;
  bottom: 0;
  background-color: rgba(0, 150, 255, 0.6);
  border-radius: 50%;
}
</style>

使用Vue过渡效果

利用Vue的<transition-group>实现气泡动画:

<template>
  <transition-group name="bubble" tag="div" class="bubble-container">
    <div 
      v-for="(bubble, index) in bubbles" 
      :key="index" 
      class="bubble" 
      :style="bubble.style"
    ></div>
  </transition-group>
</template>

<script>
export default {
  data() {
    return {
      bubbles: []
    }
  },
  mounted() {
    setInterval(() => {
      this.createBubble()
    }, 500)
  },
  methods: {
    createBubble() {
      const size = Math.random() * 30 + 10
      this.bubbles.push({
        style: {
          width: `${size}px`,
          height: `${size}px`,
          left: `${Math.random() * 100}%`
        }
      })

      setTimeout(() => {
        this.bubbles.shift()
      }, 3000)
    }
  }
}
</script>

<style>
.bubble-container {
  position: relative;
  height: 300px;
  overflow: hidden;
}

.bubble {
  position: absolute;
  bottom: 0;
  background-color: rgba(255, 200, 0, 0.6);
  border-radius: 50%;
  transition: all 2s ease-out;
}

.bubble-enter-active {
  animation: rise 2s forwards;
}

@keyframes rise {
  0% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
  100% {
    transform: translateY(-300px) scale(0.5);
    opacity: 0;
  }
}
</style>

使用第三方组件库

例如使用vue-particles库快速实现气泡效果:

vue实现气泡效果

  1. 安装库:

    npm install vue-particles
  2. 在组件中使用:

    
    <template>
    <div class="container">
     <vue-particles
       color="#ffffff"
       :particleOpacity="0.7"
       :particlesNumber="50"
       shapeType="circle"
       :particleSize="4"
       linesColor="#ffffff"
       :linesWidth="1"
       :lineLinked="true"
       :lineOpacity="0.4"
       :linesDistance="150"
       :moveSpeed="2"
       :hoverEffect="true"
       hoverMode="grab"
       :clickEffect="true"
       clickMode="push"
     />
    </div>
    </template>
import VueParticles from 'vue-particles' export default { components: { VueParticles } } .container { height: 100vh; background: linear-gradient(to bottom, #1e5799, #2989d8); } ```

这些方法可以根据具体需求选择,CSS动画适合简单效果,GSAP适合复杂动画,Vue过渡适合组件化场景,第三方库适合快速实现。

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

相关文章

vue实现翻页效果

vue实现翻页效果

Vue实现翻页效果的方法 使用v-for和计算属性 通过计算属性动态计算当前页显示的数据,结合v-for渲染分页内容。定义currentPage和pageSize控制分页逻辑。 <te…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-…

vue实现吸附效果

vue实现吸附效果

实现吸附效果的基本思路 吸附效果通常指当页面滚动到某个位置时,元素固定在视口的特定位置。Vue中可以通过监听滚动事件结合CSS的position: sticky或动态样式切换实现。 使用CSS的p…

h5实现左右滚动效果

h5实现左右滚动效果

使用CSS实现横向滚动 在HTML5中实现左右滚动效果,可以通过CSS的overflow-x属性配合white-space或flex布局实现。以下是一个基础示例: <div class="sc…

js实现点击效果

js实现点击效果

实现点击效果的JavaScript方法 使用addEventListener绑定点击事件 通过document.getElementById或document.querySelector获取DOM元素…

vue 实现分页效果

vue 实现分页效果

使用 Element UI 实现分页 Element UI 提供了现成的分页组件 el-pagination,适合快速集成到 Vue 项目中。 安装 Element UI: npm install…