当前位置:首页 > 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 是一个轻量级的动画库,适合实现复杂的线条动画效果。

<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 则适合需要高度自定义的场景。

vue实现线条动画效果

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

相关文章

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 动态数据绑定展示 通过 Vue 的响应式特性,实时展示数据变化。例如,表单输入与预览同步: <template> <div> <…

vue实现放大效果

vue实现放大效果

使用 CSS 过渡实现放大效果 通过 Vue 的 v-bind:class 或 v-bind:style 动态绑定 CSS 类或样式,结合 CSS 的 transform: scale() 实现平滑过…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-…

uniapp 卡片效果

uniapp 卡片效果

uniapp 实现卡片效果的方法 使用 view 和 CSS 样式 通过 view 组件结合 CSS 样式可以快速实现卡片效果。设置圆角、阴影和边距来增强视觉层次感。 <view class…

vue实现滚动效果

vue实现滚动效果

Vue 实现滚动效果的方法 使用原生滚动方法 通过 Vue 的 ref 获取 DOM 元素,调用原生 scrollTo 或 scrollIntoView 方法实现滚动。 <template&g…

vue实现抽屉效果

vue实现抽屉效果

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