当前位置:首页 > 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 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…

vue实现hexo

vue实现hexo

Vue 集成 Hexo 的实现方法 Hexo 是一个静态博客框架,而 Vue 是一个前端框架。将 Vue 集成到 Hexo 中可以通过以下方式实现: 在 Hexo 中使用 Vue 组件 通过 Hex…

vue实现tooltips

vue实现tooltips

Vue 实现 Tooltips 的方法 使用原生 HTML title 属性 在 Vue 模板中直接使用 HTML 原生 title 属性实现简单的提示效果。这种方式无需额外依赖,但功能较为基础,无法…

vue排他思想实现

vue排他思想实现

Vue 排他思想实现 排他思想在 Vue 中通常指多个元素中只能有一个被选中或激活的状态。可以通过数据驱动和条件渲染实现。 使用 v-model 和计算属性 定义一个数据属性存储当前选中项的索引或标…

vue实现数据渲染

vue实现数据渲染

Vue 数据渲染的实现方式 Vue.js 提供了多种数据渲染的方式,包括插值表达式、指令和计算属性等。以下是常见的实现方法: 插值表达式 使用双大括号 {{ }} 将数据绑定到模板中,数据变化时会自…