当前位置:首页 > 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. 限制同时显示的圆圈数量,避免性能问题

自定义选项

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

vue实现扩散圆圈

  • 修改 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 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show 通过…

vue实现发帖

vue实现发帖

使用 Vue 实现发帖功能 前端实现 创建发帖表单组件,包含标题和内容输入框,以及提交按钮。使用 v-model 绑定表单数据。 <template> <div>…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…

vue实现例子

vue实现例子

以下是一些常见的 Vue 实现例子,涵盖基础功能到进阶应用场景: 基础数据绑定 使用 v-model 实现双向数据绑定: <template> <div> &l…

vue实现dialog

vue实现dialog

Vue 实现 Dialog 的方法 使用 Vue 原生组件实现 Vue 可以通过组件化的方式实现 Dialog,以下是一个简单的实现示例: <template> <div>…