当前位置:首页 > VUE

vue实现水管道交错

2026-02-24 08:22:47VUE

Vue 实现水管道交错效果

在 Vue 中实现水管道交错效果,可以通过 CSS 动画和 Vue 的动态绑定结合实现。以下是具体实现方法:

使用 CSS 动画和 Vue 动态样式

定义水管道的基本样式和动画效果,通过 Vue 控制动画的播放状态和方向。

<template>
  <div class="water-pipes">
    <div 
      v-for="(pipe, index) in pipes" 
      :key="index" 
      class="pipe" 
      :style="{
        'animation-delay': `${index * 0.2}s`,
        'animation-direction': pipe.direction
      }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pipes: [
        { direction: 'alternate' },
        { direction: 'alternate-reverse' },
        { direction: 'alternate' },
        { direction: 'alternate-reverse' }
      ]
    }
  }
}
</script>

<style>
.water-pipes {
  display: flex;
  justify-content: space-around;
  height: 200px;
}

.pipe {
  width: 40px;
  background: linear-gradient(to bottom, #4facfe 0%, #00f2fe 100%);
  animation: wave 2s infinite ease-in-out;
}

@keyframes wave {
  0%, 100% {
    transform: scaleY(1);
  }
  50% {
    transform: scaleY(1.5);
  }
}
</style>

使用 SVG 和 Vue 动态路径

通过 SVG 绘制水管路径,并利用 Vue 动态调整路径参数实现交错效果。

<template>
  <svg width="400" height="200" viewBox="0 0 400 200">
    <path 
      v-for="(pipe, index) in pipes" 
      :key="index" 
      :d="pipe.path" 
      fill="none" 
      stroke="#00f2fe" 
      stroke-width="10"
      :style="{
        'animation-delay': `${index * 0.3}s`
      }"
      class="pipe-wave"
    />
  </svg>
</template>

<script>
export default {
  data() {
    return {
      pipes: [
        { path: 'M50,50 C100,100 150,0 200,50' },
        { path: 'M50,150 C100,100 150,200 200,150' }
      ]
    }
  }
}
</script>

<style>
.pipe-wave {
  animation: wave 3s infinite ease-in-out;
}

@keyframes wave {
  0%, 100% {
    stroke-dashoffset: 0;
  }
  50% {
    stroke-dashoffset: 100;
  }
}
</style>

使用 Canvas 和 Vue 动态绘制

通过 Canvas API 动态绘制水管道,结合 Vue 的数据响应实现动画效果。

<template>
  <canvas ref="canvas" width="400" height="200"></canvas>
</template>

<script>
export default {
  data() {
    return {
      pipes: [
        { x: 50, y: 50, width: 20, height: 100, speed: 0.05, offset: 0 },
        { x: 150, y: 50, width: 20, height: 100, speed: 0.08, offset: 50 },
        { x: 250, y: 50, width: 20, height: 100, speed: 0.06, offset: 30 }
      ]
    }
  },
  mounted() {
    this.animate();
  },
  methods: {
    animate() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      const animateFrame = () => {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        this.pipes.forEach(pipe => {
          pipe.offset += pipe.speed;
          const waveHeight = Math.sin(pipe.offset) * 20;
          ctx.fillStyle = '#4facfe';
          ctx.fillRect(
            pipe.x, 
            pipe.y + waveHeight, 
            pipe.width, 
            pipe.height
          );
        });
        requestAnimationFrame(animateFrame);
      };
      animateFrame();
    }
  }
}
</script>

使用第三方库(如 GSAP)

结合 GSAP 动画库实现更复杂的水管道交错动画效果。

<template>
  <div ref="pipes" class="water-pipes">
    <div v-for="(pipe, index) in pipes" :key="index" class="pipe"></div>
  </div>
</template>

<script>
import { gsap } from 'gsap';

export default {
  data() {
    return {
      pipes: new Array(4).fill(null)
    }
  },
  mounted() {
    this.animatePipes();
  },
  methods: {
    animatePipes() {
      const pipes = this.$refs.pipes.children;
      gsap.to(pipes, {
        scaleY: 1.5,
        duration: 2,
        stagger: 0.2,
        yoyo: true,
        repeat: -1,
        ease: 'sine.inOut'
      });
    }
  }
}
</script>

<style>
.water-pipes {
  display: flex;
  justify-content: space-around;
  height: 200px;
}

.pipe {
  width: 40px;
  background: linear-gradient(to bottom, #4facfe 0%, #00f2fe 100%);
}
</style>

vue实现水管道交错

标签: 水管vue
分享给朋友:

相关文章

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

vue实现listview

vue实现listview

Vue 实现 ListView 的方法 基础列表渲染 使用 v-for 指令可以轻松实现基础列表渲染。在 Vue 中,v-for 用于遍历数组或对象,生成动态列表。 <template>…