当前位置:首页 > VUE

vue 实现跳动爱心

2026-03-28 17:14:28VUE

Vue 实现跳动爱心效果

可以通过 CSS 动画结合 Vue 的动态绑定实现爱心跳动效果。以下是具体实现方法:

基础爱心形状

使用 CSS 绘制爱心形状,可以通过伪元素创建:

<template>
  <div class="heart-container">
    <div class="heart" :style="heartStyle"></div>
  </div>
</template>

<style>
.heart {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  transform: rotate(45deg);
}

.heart::before,
.heart::after {
  content: "";
  width: 100px;
  height: 100px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
}

.heart::before {
  top: -50px;
  left: 0;
}

.heart::after {
  top: 0;
  left: -50px;
}
</style>

添加跳动动画

通过 CSS 动画实现缩放效果:

@keyframes heartbeat {
  0% {
    transform: rotate(45deg) scale(1);
  }
  25% {
    transform: rotate(45deg) scale(1.1);
  }
  50% {
    transform: rotate(45deg) scale(1);
  }
  75% {
    transform: rotate(45deg) scale(1.1);
  }
  100% {
    transform: rotate(45deg) scale(1);
  }
}

.heart {
  animation: heartbeat 1.5s infinite;
}

Vue 动态控制

添加响应式数据控制动画状态:

<script>
export default {
  data() {
    return {
      isBeating: true
    }
  },
  computed: {
    heartStyle() {
      return {
        animation: this.isBeating ? 'heartbeat 1.5s infinite' : 'none'
      }
    }
  },
  methods: {
    toggleHeartbeat() {
      this.isBeating = !this.isBeating
    }
  }
}
</script>

点击交互效果

添加点击事件使爱心跳动更明显:

<template>
  <div class="heart-container" @click="pulseHeart">
    <div class="heart" :style="heartStyle"></div>
  </div>
</template>

<script>
export default {
  methods: {
    pulseHeart() {
      this.isBeating = false
      setTimeout(() => {
        this.isBeating = true
      }, 100)
    }
  }
}
</script>

进阶效果

可以添加随机颜色和位置变化:

data() {
  return {
    colors: ['#ff0000', '#ff69b4', '#ff1493', '#ff00ff'],
    currentColor: '#ff0000'
  }
},
methods: {
  changeColor() {
    const randomIndex = Math.floor(Math.random() * this.colors.length)
    this.currentColor = this.colors[randomIndex]
  }
}

然后在样式中使用动态绑定:

<div class="heart" 
     :style="{ 
       'background-color': currentColor,
       'animation': isBeating ? 'heartbeat 1.5s infinite' : 'none'
     }">
</div>

性能优化

对于多个爱心的情况,使用 Vue 的列表渲染:

vue 实现跳动爱心

<template>
  <div>
    <div 
      v-for="(heart, index) in hearts" 
      :key="index" 
      class="heart-item"
      @click="pulseHeart(index)"
    >
      <div class="heart" :style="getHeartStyle(index)"></div>
    </div>
  </div>
</template>

以上方法结合了 Vue 的响应式特性和 CSS 动画,可以创建出流畅的爱心跳动效果。

标签: 爱心vue
分享给朋友:

相关文章

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <tem…

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="bu…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue 实现ztree

vue 实现ztree

在Vue中实现类似ZTree的功能,可以通过集成第三方库或自定义组件完成。以下是两种常见方法: 使用vue-tree组件 vue-tree是一个轻量级的Vue树形组件,支持复选框、懒加载等功能,适合…