当前位置:首页 > VUE

下雪特效vue实现

2026-02-17 10:35:04VUE

下雪特效的 Vue 实现方法

使用 CSS 动画实现基础下雪效果

通过 CSS 的 @keyframes 动画和 transform 属性创建雪花飘落效果。在 Vue 的模板中添加多个雪花元素,每个雪花通过随机生成的样式实现不同的飘落路径和速度。

<template>
  <div class="snow-container">
    <div v-for="(snow, index) in snowflakes" :key="index" class="snowflake" :style="snow.style"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      snowflakes: [],
      snowflakeCount: 50 // 雪花数量
    };
  },
  mounted() {
    this.createSnowflakes();
  },
  methods: {
    createSnowflakes() {
      for (let i = 0; i < this.snowflakeCount; i++) {
        this.snowflakes.push({
          style: {
            left: `${Math.random() * 100}%`,
            animationDuration: `${Math.random() * 3 + 2}s`,
            animationDelay: `${Math.random() * 5}s`,
            opacity: Math.random(),
            width: `${Math.random() * 10 + 5}px`,
            height: `${Math.random() * 10 + 5}px`
          }
        });
      }
    }
  }
};
</script>

<style>
.snow-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1000;
}

.snowflake {
  position: absolute;
  background-color: white;
  border-radius: 50%;
  animation: fall linear infinite;
}

@keyframes fall {
  to {
    transform: translateY(100vh) rotate(360deg);
  }
}
</style>

使用 Canvas 实现高性能下雪效果

对于更复杂的场景或大量雪花,Canvas 是更好的选择。通过 Vue 的 ref 获取 Canvas 元素,并在 mounted 钩子中绘制雪花。

<template>
  <canvas ref="snowCanvas" class="snow-canvas"></canvas>
</template>

<script>
export default {
  mounted() {
    const canvas = this.$refs.snowCanvas;
    const ctx = canvas.getContext('2d');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const snowflakes = [];
    const snowflakeCount = 100;

    for (let i = 0; i < snowflakeCount; i++) {
      snowflakes.push({
        x: Math.random() * canvas.width,
        y: Math.random() * canvas.height,
        radius: Math.random() * 4 + 1,
        speed: Math.random() * 3 + 1,
        opacity: Math.random()
      });
    }

    function drawSnowflakes() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.fillStyle = 'white';

      snowflakes.forEach(flake => {
        ctx.beginPath();
        ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
        ctx.globalAlpha = flake.opacity;
        ctx.fill();

        flake.y += flake.speed;
        if (flake.y > canvas.height) {
          flake.y = 0;
          flake.x = Math.random() * canvas.width;
        }
      });

      requestAnimationFrame(drawSnowflakes);
    }

    drawSnowflakes();
  }
};
</script>

<style>
.snow-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1000;
}
</style>

使用第三方库实现高级效果

如果需要更丰富的效果,可以使用专门的动画库如 particles.jsvue-particles

安装 vue-particles

npm install vue-particles

在 Vue 项目中注册并使用:

import VueParticles from 'vue-particles'
Vue.use(VueParticles)

在模板中添加组件:

<template>
  <div>
    <vue-particles
      color="#ffffff"
      :particleOpacity="0.7"
      :particlesNumber="80"
      shapeType="circle"
      :particleSize="4"
      linesColor="#ffffff"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    />
  </div>
</template>

响应式调整

对于 Canvas 方案,需要监听窗口大小变化并调整 Canvas 尺寸:

下雪特效vue实现

mounted() {
  // ...初始化代码...
  window.addEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    const canvas = this.$refs.snowCanvas;
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
  }
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
}

标签: 特效vue
分享给朋友:

相关文章

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue模版实现

vue模版实现

Vue 模板实现方法 Vue 模板是 Vue.js 的核心特性之一,用于声明式地将 DOM 绑定至底层 Vue 实例的数据。以下是几种常见的 Vue 模板实现方式: 单文件组件(SFC) 使用 .v…

vue 实现table

vue 实现table

Vue 实现 Table 的方法 使用原生 HTML 表格 通过 Vue 的 v-for 指令动态渲染表格数据,适合简单表格场景。 <template> <table>…

vue 实现多选框

vue 实现多选框

Vue 实现多选框的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选框的选择功能。每个复选框的 value 属性会被添加到数组中,取消勾选时则从数…