vue实现表白
Vue实现表白页面
使用Vue可以创建一个动态、交互式的表白页面。以下是实现方法:
创建Vue项目 使用Vue CLI创建新项目:
vue create love-page
cd love-page
页面结构设计 在App.vue中构建基本结构:
<template>
<div id="app">
<div class="heart" @click="showMessage"></div>
<div v-if="show" class="message">
<h1>{{ message }}</h1>
<button @click="changeHeart">❤️</button>
</div>
</div>
</template>
添加交互逻辑
<script>
export default {
data() {
return {
show: false,
message: '我喜欢你!',
heartColor: 'red'
}
},
methods: {
showMessage() {
this.show = true
},
changeHeart() {
this.heartColor = this.heartColor === 'red' ? 'pink' : 'red'
}
}
}
</script>
样式设计
<style>
.heart {
width: 100px;
height: 100px;
background-color: v-bind(heartColor);
transform: rotate(45deg);
margin: 100px auto;
cursor: pointer;
position: relative;
}
.heart:before, .heart:after {
content: '';
width: 100px;
height: 100px;
background-color: v-bind(heartColor);
border-radius: 50%;
position: absolute;
}
.heart:before {
top: -50px;
left: 0;
}
.heart:after {
top: 0;
left: -50px;
}
.message {
text-align: center;
animation: fadeIn 2s;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
</style>
进阶效果实现
添加动画效果 安装animate.css库:

npm install animate.css
在main.js中引入:
import 'animate.css'
使用动画类名:
<h1 class="animate__animated animate__bounceIn">{{ message }}</h1>
添加背景音乐
<audio autoplay loop>
<source src="./assets/love.mp3" type="audio/mpeg">
</audio>
添加照片墙

<div class="photo-wall">
<img
v-for="(photo, index) in photos"
:key="index"
:src="photo"
@click="enlargePhoto(index)"
class="photo"
>
</div>
部署上线
构建项目
npm run build
部署到GitHub Pages 安装gh-pages:
npm install gh-pages --save-dev
在package.json中添加:
"scripts": {
"deploy": "gh-pages -d dist"
}
运行部署命令:
npm run deploy
以上方法结合了Vue的响应式特性和CSS动画效果,可以创建出富有情感表达的表白页面。根据实际需求可以进一步添加更多个性化元素和交互功能。






