返回

Vue3+Typescript实现满天心飘落动效:七夕限定,爱的表达

前端

我正在参加「码上掘金挑战赛」详情请看:码上掘金挑战赛来了!

前言

很久没有输出文章了,趁着活动,水一篇吧,整点劲大的,毕竟大力出奇迹!

近年来,在研究在线IDE颇有收获,打算写个专栏分享一些心得,最近刚好看到一个蛮有意思的案例——七夕节满天心飘落动效,刚好适合拿来练练手。

准备工作

在开始之前,我们需要准备以下工具和环境:

  • Vue3
  • Typescript
  • Vite
  • CSS动画库(如Animate.css)

实现步骤

  1. 创建一个新的Vue3项目
vue create vue3-full-heart
  1. 安装必要的依赖
npm install -D vite
npm install -D @vue/cli-plugin-typescript
npm install vue-router
npm install @types/vue-router
  1. main.ts文件中添加对Typescript的支持
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: App
    }
  ]
})

const app = createApp(App)

app.use(router)

app.mount('#app')
  1. App.vue文件中添加如下代码
<template>
  <div id="app">
    <div class="heart-container">
      <div class="heart" v-for="i in 100" :key="i"></div>
    </div>
  </div>
</template>

<style>
#app {
  height: 100vh;
  width: 100vw;
  background-color: #000;
}

.heart-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.heart {
  position: absolute;
  top: -100px;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  background-color: #f00;
  border-radius: 50%;
  animation: heart-fall 1s infinite linear;
}

@keyframes heart-fall {
  0% {
    top: -100px;
  }

  100% {
    top: 100vh;
  }
}
</style>
  1. src/assets/css/main.css文件中添加如下代码
.heart {
  animation: heart-fall 1s infinite linear;
}

@keyframes heart-fall {
  0% {
    top: -100px;
  }

  100% {
    top: 100vh;
  }
}

运行效果

现在,您可以运行项目并看到满天心飘落的效果。

结语

希望这篇文章能帮助您实现Vue3+Typescript中实现满天心飘落动效。如果您有任何问题,请随时留言。

最后,祝您七夕节快乐!