当前位置:首页 > VUE

vue实现扩散圆圈

2026-02-19 12:53:50VUE

Vue 实现扩散圆圈效果

扩散圆圈效果可以通过 CSS 动画结合 Vue 的动态样式绑定实现。以下是一个完整的实现方案:

vue实现扩散圆圈

基本实现思路

  1. 使用 Vue 的 v-for 动态生成多个圆圈元素
  2. 通过 CSS 动画控制圆圈的缩放和透明度变化
  3. 利用 Vue 的数据绑定控制动画参数

具体实现代码

<template>
  <div class="container">
    <div 
      v-for="(circle, index) in circles" 
      :key="index"
      class="ripple-circle"
      :style="{
        width: `${circle.size}px`,
        height: `${circle.size}px`,
        left: `${circle.x}px`,
        top: `${circle.y}px`,
        animationDelay: `${index * 0.3}s`
      }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      circles: [],
      maxCircles: 5,
      currentIndex: 0
    }
  },
  methods: {
    addCircle(event) {
      if (this.circles.length >= this.maxCircles) {
        this.circles.shift()
      }

      this.circles.push({
        x: event.clientX - 25,
        y: event.clientY - 25,
        size: 50,
        active: true
      })
    }
  },
  mounted() {
    window.addEventListener('click', this.addCircle)
  },
  beforeDestroy() {
    window.removeEventListener('click', this.addCircle)
  }
}
</script>

<style>
.container {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.ripple-circle {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(66, 165, 245, 0.6);
  transform: scale(0);
  animation: ripple 1.5s ease-out;
  pointer-events: none;
}

@keyframes ripple {
  to {
    transform: scale(3);
    opacity: 0;
  }
}
</style>

实现说明

  1. 创建 Vue 组件并设置初始数据,包括存储圆圈的数组、最大圆圈数量和当前索引
  2. 在模板中使用 v-for 循环渲染圆圈元素,通过动态样式绑定位置和大小
  3. 添加点击事件监听器,在点击位置生成新的圆圈
  4. 使用 CSS 关键帧动画实现扩散效果,圆圈从初始大小逐渐放大并淡出
  5. 限制同时显示的圆圈数量,避免性能问题

自定义选项

可以通过修改以下参数来自定义效果:

  • 修改 maxCircles 控制同时显示的圆圈数量
  • 调整 CSS 中的 animation-duration 改变动画速度
  • 更改 background-color 调整圆圈颜色
  • 修改 ripple 动画中的 scale 值控制最终扩散大小

优化版本(自动连续动画)

如果需要自动连续播放的扩散效果,可以使用以下修改:

// 在 mounted 中添加定时器
mounted() {
  this.interval = setInterval(() => {
    this.circles.push({
      x: Math.random() * window.innerWidth,
      y: Math.random() * window.innerHeight,
      size: 30 + Math.random() * 70,
      active: true
    })

    if (this.circles.length > this.maxCircles) {
      this.circles.shift()
    }
  }, 800)
},
beforeDestroy() {
  clearInterval(this.interval)
}

注意事项

  1. 确保容器有明确的尺寸(如示例中的 height: 100vh
  2. 动画性能优化:圆圈数量不宜过多,建议控制在10个以内
  3. 移动端适配可能需要添加 touch 事件监听

vue实现扩散圆圈

标签: 圆圈vue
分享给朋友:

相关文章

项目基于vue实现

项目基于vue实现

基于Vue实现项目的关键方法 搭建基础环境 安装Node.js和npm/yarn,确保开发环境具备运行Vue的能力。通过Vue CLI快速初始化项目结构,选择所需配置(如Babel、Router、Vu…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <d…

vue前端分页怎么实现

vue前端分页怎么实现

前端分页实现方法 在Vue中实现前端分页通常涉及数据分割和页面控制逻辑。以下是常见的实现方式: 使用计算属性分页 通过计算属性对数据进行切片处理: computed: { paginatedD…

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…