当前位置:首页 > VUE

vue实现线条动画效果

2026-02-22 13:54:46VUE

Vue 实现线条动画效果

在 Vue 中实现线条动画效果可以通过多种方式完成,以下是几种常见的方法:

使用 SVG 和 CSS 动画

SVG 是创建线条动画的理想工具,结合 CSS 动画可以实现平滑的效果。

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

<script>
export default {
  mounted() {
    this.$refs.path.style.animation = "dash 3s linear forwards";
  },
};
</script>

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

使用 GSAP 库

GSAP 是一个强大的动画库,可以轻松实现复杂的线条动画效果。

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

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

export default {
  mounted() {
    gsap.to(this.$refs.path, {
      strokeDashoffset: 0,
      duration: 3,
      ease: "power1.inOut",
    });
  },
};
</script>

使用 Canvas 和 Vue

Canvas 提供了更底层的绘图能力,适合需要高度自定义的线条动画。

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

<script>
export default {
  mounted() {
    const canvas = this.$refs.canvas;
    const ctx = canvas.getContext("2d");
    let progress = 0;

    const animate = () => {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.beginPath();
      ctx.moveTo(10, 100);
      ctx.quadraticCurveTo(100, 10 * progress, 190, 100);
      ctx.strokeStyle = "#42b983";
      ctx.lineWidth = 2;
      ctx.stroke();

      if (progress < 1) {
        progress += 0.01;
        requestAnimationFrame(animate);
      }
    };

    animate();
  },
};
</script>

使用 Vue 过渡效果

Vue 的过渡系统可以结合 CSS 实现简单的线条动画。

<template>
  <div>
    <div class="line-container">
      <div class="line" :class="{ 'line-animate': animate }"></div>
    </div>
    <button @click="animate = !animate">Toggle Animation</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      animate: false,
    };
  },
};
</script>

<style>
.line-container {
  width: 200px;
  height: 2px;
  background: #ddd;
  margin: 20px 0;
}

.line {
  height: 100%;
  width: 0;
  background: #42b983;
  transition: width 1s ease;
}

.line-animate {
  width: 100%;
}
</style>

使用第三方库(如 anime.js)

anime.js 是一个轻量级的动画库,适合实现复杂的线条动画效果。

vue实现线条动画效果

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

<script>
import anime from "animejs";

export default {
  mounted() {
    anime({
      targets: this.$refs.path,
      strokeDashoffset: [1000, 0],
      duration: 3000,
      easing: "easeInOutSine",
    });
  },
};
</script>

以上方法可以根据具体需求选择适合的实现方式。SVG 和 CSS 动画适合简单的线条动画,GSAP 和 anime.js 适合复杂的动画效果,Canvas 则适合需要高度自定义的场景。

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

相关文章

h5页面滑动效果实现

h5页面滑动效果实现

实现H5页面滑动效果的方法 H5页面滑动效果可以通过多种方式实现,包括原生JavaScript、CSS动画或第三方库。以下是几种常见的方法: 使用CSS Scroll Snap CSS Scroll…

vue实现抽屉效果

vue实现抽屉效果

Vue 实现抽屉效果的方法 使用 Vue 过渡动画和 CSS 在 Vue 中可以通过 v-if 或 v-show 控制抽屉的显示隐藏,结合 <transition> 组件实现平滑动画效果。…

vue实现切换效果

vue实现切换效果

Vue 实现切换效果的方法 使用 v-if 和 v-else 指令 通过条件渲染实现切换效果,适合简单的内容切换。利用 v-if 和 v-else 控制元素的显示与隐藏。 <template&…

vue实现不了效果

vue实现不了效果

在 Vue 中实现特定效果时遇到问题,通常与代码逻辑、数据绑定或生命周期管理有关。以下是常见排查方向和解决方案: 检查数据绑定 确保数据已正确声明并在模板中绑定。Vue 的响应式系统依赖 data…

vue实现点击高亮效果

vue实现点击高亮效果

实现点击高亮效果的方法 在Vue中实现点击高亮效果可以通过多种方式完成,以下是几种常见的方法: 方法一:使用动态class绑定 通过v-bind:class或简写:class动态切换高亮样式类。…

vue实现翻牌效果案例

vue实现翻牌效果案例

Vue 实现翻牌效果案例 翻牌效果通常用于展示卡片翻转动画,常见于记忆游戏或展示正反面内容。以下是基于 Vue 3 的实现方案: 基础翻牌效果实现 创建翻转动画的核心是利用 CSS 的 transf…