当前位置:首页 > 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 实现预览效果的方法 使用 v-html 指令实现简单预览 在 Vue 中可以通过 v-html 指令直接将 HTML 字符串渲染到页面上,适用于简单的富文本预览场景。 <templat…

vue实现效果

vue实现效果

Vue 实现常见效果的方法 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现表单元素与数据的双向绑定。例如输入框与数据的实时同步: <input v-model…

jquery 效果

jquery 效果

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。jQuery 提供了丰富的效果(Effects)功能,可以轻松实现元素的动态显…

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升…

vue实现吸附效果

vue实现吸附效果

实现吸附效果的基本思路 吸附效果通常指当页面滚动到某个位置时,元素固定在视口的特定位置。Vue中可以通过监听滚动事件结合CSS的position: sticky或动态样式切换实现。 使用CSS的po…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果通常涉及动态样式绑定、动画过渡或第三方库的使用。以下是几种常见实现方式: 动态类名绑定 通过 v-bind:class 或简写 :class…