当前位置:首页 > 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 动画库实现更复杂的水管道交错动画效果。

vue实现水管道交错

<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 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue实现发帖

vue实现发帖

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

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…

vue实现双击

vue实现双击

Vue 实现双击事件的方法 在Vue中实现双击事件可以通过以下几种方式完成,具体选择取决于项目需求和开发习惯。 使用 @dblclick 指令 Vue提供了内置的@dblclick指令,可以…

vue实现点击提交

vue实现点击提交

Vue 实现点击提交功能 在 Vue 中实现点击提交功能通常涉及表单处理和事件绑定。以下是几种常见的实现方式: 使用 v-on 或 @ 绑定点击事件 通过 v-on:click 或简写 @click…

laravel vue实现登录

laravel vue实现登录

Laravel 和 Vue.js 实现登录功能 后端配置(Laravel) 安装 Laravel Sanctum 用于 API 认证 composer require laravel/sanctu…