当前位置:首页 > VUE

vue实现扩散圆

2026-03-28 20:41:01VUE

实现扩散圆效果

在Vue中实现扩散圆效果可以通过CSS动画结合动态数据绑定完成。以下是两种常见实现方式:

vue实现扩散圆

CSS动画实现

通过@keyframes定义缩放动画,结合Vue的v-bind动态控制样式:

vue实现扩散圆

<template>
  <div class="circle-container">
    <div 
      v-for="(circle, index) in circles" 
      :key="index"
      class="ripple-circle"
      :style="{
        width: `${circle.size}px`,
        height: `${circle.size}px`,
        animation: `ripple ${circle.duration}s ease-out infinite`
      }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      circles: [
        { size: 100, duration: 3 },
        { size: 150, duration: 4 },
        { size: 200, duration: 5 }
      ]
    }
  }
}
</script>

<style>
.ripple-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(0, 150, 255, 0.6);
  transform: scale(0);
  opacity: 0;
}

@keyframes ripple {
  0% {
    transform: scale(0.8);
    opacity: 0.8;
  }
  100% {
    transform: scale(2);
    opacity: 0;
  }
}
</style>

JavaScript动态控制

通过requestAnimationFrame实现更精细的控制:

<template>
  <div class="canvas-container">
    <canvas ref="canvas" width="400" height="400"></canvas>
  </div>
</template>

<script>
export default {
  mounted() {
    this.initCanvas();
  },
  methods: {
    initCanvas() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      let circles = [];

      function createCircle() {
        circles.push({
          x: 200,
          y: 200,
          radius: 10,
          alpha: 1,
          speed: 2
        });
      }

      function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        circles.forEach((circle, index) => {
          ctx.beginPath();
          ctx.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2);
          ctx.strokeStyle = `rgba(0, 150, 255, ${circle.alpha})`;
          ctx.lineWidth = 2;
          ctx.stroke();

          circle.radius += circle.speed;
          circle.alpha -= 0.01;

          if (circle.alpha <= 0) {
            circles.splice(index, 1);
          }
        });

        requestAnimationFrame(animate);
      }

      setInterval(createCircle, 500);
      animate();
    }
  }
}
</script>

优化建议

  1. 性能优化:对于大量扩散圆,建议使用Canvas而非DOM元素
  2. 参数化:通过props接收颜色、速度等参数使组件可复用
  3. 清除机制:添加beforeDestroy生命周期清理动画帧
  4. 交互增强:可绑定@click事件实现点击位置扩散效果

两种方式分别适合不同场景:CSS方案实现简单,Canvas方案性能更好且可控性更强。

标签: vue
分享给朋友:

相关文章

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…