当前位置:首页 > VUE

vue如何实现树叶飘落

2026-01-21 13:29:15VUE

Vue 实现树叶飘落效果

使用 CSS 动画和动态组件

在 Vue 中实现树叶飘落效果可以通过 CSS 动画结合动态组件的方式完成。创建一个树叶组件,使用 CSS 定义飘落动画路径。

vue如何实现树叶飘落

<template>
  <div class="leaf" :style="leafStyle"></div>
</template>

<script>
export default {
  props: {
    delay: {
      type: Number,
      default: 0
    }
  },
  computed: {
    leafStyle() {
      return {
        left: `${Math.random() * 100}%`,
        animationDelay: `${this.delay}s`
      }
    }
  }
}
</script>

<style>
.leaf {
  position: fixed;
  top: -50px;
  width: 30px;
  height: 30px;
  background-image: url('leaf.png');
  background-size: contain;
  animation: falling 10s linear infinite;
}

@keyframes falling {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  100% {
    transform: translate(calc(-50px + 100px * var(--random-x)), calc(100vh + 50px)) rotate(360deg);
  }
}
</style>

使用 GSAP 动画库

对于更复杂的路径动画,可以使用 GSAP 库实现更自然的飘落效果。安装 GSAP 后创建动画控制器。

vue如何实现树叶飘落

import { gsap } from 'gsap'

export default {
  mounted() {
    this.animateLeaf()
  },
  methods: {
    animateLeaf() {
      gsap.to(this.$el, {
        duration: 10 + Math.random() * 5,
        y: window.innerHeight + 100,
        x: () => Math.random() * 200 - 100,
        rotation: 360,
        ease: "none",
        onComplete: () => {
          this.resetPosition()
          this.animateLeaf()
        }
      })
    },
    resetPosition() {
      gsap.set(this.$el, {
        y: -100,
        x: Math.random() * window.innerWidth
      })
    }
  }
}

使用 Canvas 绘制

对于大量树叶效果,使用 Canvas 性能更优。创建一个 Canvas 组件并实现绘制逻辑。

export default {
  data() {
    return {
      leaves: [],
      ctx: null
    }
  },
  mounted() {
    this.initCanvas()
    this.generateLeaves(20)
    requestAnimationFrame(this.animate)
  },
  methods: {
    initCanvas() {
      const canvas = this.$refs.canvas
      canvas.width = window.innerWidth
      canvas.height = window.innerHeight
      this.ctx = canvas.getContext('2d')
    },
    generateLeaves(count) {
      for (let i = 0; i < count; i++) {
        this.leaves.push({
          x: Math.random() * this.$refs.canvas.width,
          y: -50 - Math.random() * 200,
          speed: 1 + Math.random() * 3,
          rotation: 0,
          rotationSpeed: Math.random() * 0.2 - 0.1,
          size: 20 + Math.random() * 20
        })
      }
    },
    animate() {
      this.ctx.clearRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height)

      this.leaves.forEach(leaf => {
        leaf.y += leaf.speed
        leaf.x += Math.sin(leaf.y * 0.01) * 2
        leaf.rotation += leaf.rotationSpeed

        this.ctx.save()
        this.ctx.translate(leaf.x, leaf.y)
        this.ctx.rotate(leaf.rotation)
        this.ctx.drawImage(this.leafImage, -leaf.size/2, -leaf.size/2, leaf.size, leaf.size)
        this.ctx.restore()

        if (leaf.y > this.$refs.canvas.height) {
          leaf.y = -50
          leaf.x = Math.random() * this.$refs.canvas.width
        }
      })

      requestAnimationFrame(this.animate)
    }
  }
}

性能优化技巧

对于大量动画元素,使用 requestAnimationFrame 比 CSS 动画性能更好。限制同时显示的树叶数量,回收超出屏幕的树叶对象。

考虑使用 WebGL 或第三方库如 Three.js 实现更复杂的 3D 飘落效果。对于移动端,减少动画元素数量并测试性能表现。

分享给朋友:

相关文章

vue如何实现注册

vue如何实现注册

实现用户注册功能 在Vue中实现注册功能通常需要结合前端表单和后端API交互。以下是完整的实现方案: 创建注册表单组件 注册表单需要包含用户名、邮箱、密码等基本字段,并进行表单验证: <…

vue前端折线如何实现

vue前端折线如何实现

使用 Vue 实现折线图的方法 安装图表库 推荐使用 ECharts 或 Chart.js 等主流图表库。以 ECharts 为例,通过 npm 安装: npm install echarts vu…

vue自动登录如何实现

vue自动登录如何实现

实现自动登录的基本思路 自动登录通常通过结合本地存储(如localStorage或cookie)和token验证机制实现。用户首次登录成功后,服务器返回的认证token会被保存在客户端,下次打开应用时…

js如何实现继承

js如何实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例能够访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

如何实现vue

如何实现vue

安装 Vue.js 使用 npm 或 yarn 安装 Vue.js。确保已安装 Node.js 环境。 npm install vue # 或 yarn add vue 创建 Vue 实例 在 HT…

vue如何实现

vue如何实现

Vue 实现方法 Vue 提供了多种方式来实现功能,具体取决于需求。以下是一些常见场景的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。在组件中可以通过 pro…