当前位置:首页 > VUE

下雪特效vue实现

2026-03-08 22:55:11VUE

使用 CSS 动画实现雪花效果

创建一个 Vue 组件,利用 CSS 动画生成随机下落的雪花。

模板部分

<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: `${5 + Math.random() * 10}s`,
            opacity: Math.random(),
            width: `${5 + Math.random() * 10}px`,
            height: `${5 + Math.random() * 10}px`
          }
        });
      }
    }
  }
};
</script>

样式部分

<style scoped>
.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);
  }
}
</style>

使用 Canvas 实现更复杂效果

若需要更真实的雪花飘落效果,可使用 Canvas 绘制。

下雪特效vue实现

模板部分

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

脚本部分

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

    const ctx = canvas.getContext("2d");
    const snowflakes = Array(100).fill().map(() => ({
      x: Math.random() * canvas.width,
      y: Math.random() * canvas.height,
      radius: Math.random() * 5 + 1,
      speed: Math.random() * 2 + 1,
      wind: Math.random() * 0.5 - 0.25
    }));

    function animate() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      snowflakes.forEach(flake => {
        flake.y += flake.speed;
        flake.x += flake.wind;
        if (flake.y > canvas.height) flake.y = 0;
        if (flake.x > canvas.width) flake.x = 0;
        if (flake.x < 0) flake.x = canvas.width;

        ctx.beginPath();
        ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
        ctx.fillStyle = "white";
        ctx.fill();
      });
      requestAnimationFrame(animate);
    }
    animate();
  }
};
</script>

样式部分

下雪特效vue实现

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

使用第三方库(如 vue-particles)

安装 vue-particles 快速实现粒子效果:

npm install vue-particles

注册组件

import VueParticles from "vue-particles";
Vue.use(VueParticles);

在模板中使用

<template>
  <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="2"
    :hoverEffect="false"
  />
</template>

以上方法可根据需求选择,CSS 动画适合轻量级效果,Canvas 提供更灵活控制,而第三方库能快速集成复杂特效。

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

相关文章

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…