当前位置:首页 > VUE

vue实现线条动画效果

2026-01-21 22:48:38VUE

使用 SVG 和 CSS 实现线条动画

在 Vue 中实现线条动画可以通过 SVG 结合 CSS 或 JavaScript 动画库完成。SVG 的路径动画效果流畅且易于控制。

定义一个 SVG 路径,通过 stroke-dasharraystroke-dashoffset 属性实现描边动画效果。CSS 关键帧控制动画过程。

vue实现线条动画效果

<template>
  <svg width="200" height="200" viewBox="0 0 200 200">
    <path 
      class="animated-line" 
      d="M10 100 Q 100 10 190 100" 
      fill="none" 
      stroke="#42b983" 
      stroke-width="2"
    />
  </svg>
</template>

<style>
.animated-line {
  stroke-dasharray: 500;
  stroke-dashoffset: 500;
  animation: draw 2s linear forwards;
}

@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}
</style>

动态控制动画触发

通过 Vue 的响应式数据或生命周期钩子动态触发动画,例如在组件挂载后开始动画。

<template>
  <svg width="200" height="200" viewBox="0 0 200 200">
    <path 
      ref="line" 
      d="M10 100 Q 100 10 190 100" 
      fill="none" 
      stroke="#42b983" 
      stroke-width="2"
    />
  </svg>
</template>

<script>
export default {
  mounted() {
    const path = this.$refs.line;
    const length = path.getTotalLength();
    path.style.strokeDasharray = length;
    path.style.strokeDashoffset = length;

    setTimeout(() => {
      path.style.transition = 'stroke-dashoffset 2s ease-in-out';
      path.style.strokeDashoffset = '0';
    }, 500);
  }
};
</script>

使用 GreenSock (GSAP) 库实现复杂动画

对于更复杂的路径动画,GSAP 提供精细的时间轴控制和缓动函数。

vue实现线条动画效果

<template>
  <svg width="200" height="200" viewBox="0 0 200 200">
    <path 
      ref="line" 
      d="M10 100 Q 100 10 190 100" 
      fill="none" 
      stroke="#42b983" 
      stroke-width="2"
    />
  </svg>
</template>

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

export default {
  mounted() {
    const path = this.$refs.line;
    const length = path.getTotalLength();

    gsap.set(path, {
      strokeDasharray: length,
      strokeDashoffset: length
    });

    gsap.to(path, {
      strokeDashoffset: 0,
      duration: 1.5,
      ease: "power2.inOut"
    });
  }
};
</script>

响应式路径数据绑定

通过 Vue 的数据绑定动态更新路径数据,实现交互式线条动画。

<template>
  <div>
    <svg width="200" height="200" viewBox="0 0 200 200">
      <path 
        :d="pathData" 
        fill="none" 
        stroke="#42b983" 
        stroke-width="2"
        ref="line"
      />
    </svg>
    <button @click="updatePath">更新路径</button>
  </div>
</template>

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

export default {
  data() {
    return {
      pathData: "M10 100 Q 100 10 190 100"
    };
  },
  methods: {
    updatePath() {
      this.pathData = "M10 50 Q 100 150 190 50";
      this.$nextTick(() => {
        const path = this.$refs.line;
        const length = path.getTotalLength();
        gsap.fromTo(path,
          { strokeDashoffset: length },
          { strokeDashoffset: 0, duration: 1 }
        );
      });
    }
  }
};
</script>

多段线条顺序动画

通过 GSAP 的时间轴功能实现多段线条的顺序动画效果。

<template>
  <svg width="300" height="200" viewBox="0 0 300 200">
    <path 
      v-for="(path, index) in paths" 
      :key="index" 
      :ref="`line-${index}`" 
      :d="path" 
      fill="none" 
      stroke="#42b983" 
      stroke-width="2"
    />
  </svg>
</template>

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

export default {
  data() {
    return {
      paths: [
        "M10 50 L100 50",
        "M100 50 L100 150",
        "M100 150 L200 150"
      ]
    };
  },
  mounted() {
    const tl = gsap.timeline();

    this.paths.forEach((_, index) => {
      const path = this.$refs[`line-${index}`][0];
      const length = path.getTotalLength();

      gsap.set(path, {
        strokeDasharray: length,
        strokeDashoffset: length
      });

      tl.to(path, {
        strokeDashoffset: 0,
        duration: 0.8
      }, index * 0.3);
    });
  }
};
</script>

标签: 线条效果
分享给朋友:

相关文章

vue实现对话框效果

vue实现对话框效果

Vue 对话框实现方法 使用组件化方式封装对话框 创建一个独立的对话框组件(如Dialog.vue),通过v-model控制显示隐藏: <template> <div clas…

vue实现翻书效果

vue实现翻书效果

Vue实现翻书效果的方法 翻书效果可以通过CSS动画和Vue的动态绑定实现,以下是几种常见的实现方式: 使用CSS transform和transition 通过CSS的transform属性实现翻…

vue实现折叠效果

vue实现折叠效果

使用 v-show 或 v-if 实现基础折叠 通过 Vue 的指令 v-show 或 v-if 控制元素的显示与隐藏。v-show 通过 CSS 的 display 属性切换,v-if 直接操作 D…

vue 实现分页效果

vue 实现分页效果

使用 Element UI 实现分页 Element UI 提供了现成的分页组件 el-pagination,适合快速集成到 Vue 项目中。 安装 Element UI: npm install…

vue实现轮播效果

vue实现轮播效果

使用Swiper插件实现轮播 安装Swiper及相关依赖: npm install swiper vue-awesome-swiper 组件代码示例: <template> <…

vue实现开关效果

vue实现开关效果

使用Vue实现开关效果 使用v-model绑定数据 通过v-model绑定一个布尔值来控制开关状态。当用户点击开关时,布尔值会自动更新。 <template> <div cla…