当前位置:首页 > VUE

vue 实现跳动爱心

2026-03-08 06:32:22VUE

实现跳动爱心的Vue组件

在Vue中实现跳动爱心效果可以通过CSS动画和动态绑定样式来实现。以下是具体实现方法:

vue 实现跳动爱心

基础HTML结构

<template>
  <div class="heart-container">
    <div 
      class="heart" 
      :style="{ transform: `scale(${scale})` }"
      @click="animateHeart"
    ></div>
  </div>
</template>

CSS样式部分

.heart-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.heart {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  transform: rotate(45deg);
  cursor: pointer;
  transition: transform 0.3s ease;
}

.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;
}

Vue组件逻辑

<script>
export default {
  data() {
    return {
      scale: 1,
      isBeating: false
    }
  },
  methods: {
    animateHeart() {
      if (this.isBeating) return;

      this.isBeating = true;
      this.scale = 1.2;

      setTimeout(() => {
        this.scale = 1;
        setTimeout(() => {
          this.scale = 1.2;
          setTimeout(() => {
            this.scale = 1;
            this.isBeating = false;
          }, 300);
        }, 300);
      }, 300);
    }
  }
}
</script>

自动跳动效果

如需实现自动跳动效果,可以添加以下代码:

vue 实现跳动爱心

mounted() {
  setInterval(() => {
    this.animateHeart();
  }, 2000);
}

进阶动画效果

使用CSS keyframes实现更流畅的动画:

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

.heart.animate {
  animation: heartbeat 1s infinite;
}

然后在Vue中动态添加class:

methods: {
  toggleAnimation() {
    this.isBeating = !this.isBeating;
  }
}
<div 
  class="heart" 
  :class="{ animate: isBeating }"
  @click="toggleAnimation"
></div>

以上方法提供了从简单到复杂的跳动爱心实现方式,可以根据项目需求选择合适的方案。CSS动画方案性能更优,适合复杂的动画效果;JavaScript控制方案则更灵活,便于与用户交互结合。

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

相关文章

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue最佳实现

vue最佳实现

Vue 最佳实践 组件设计 组件应遵循单一职责原则,每个组件只负责一个功能模块。命名采用 PascalCase(如 UserProfile.vue),模板中使用 kebab-case(如 <us…

vue实现gridlayout

vue实现gridlayout

Vue 实现 Grid Layout 使用 CSS Grid 布局 CSS Grid 是一种强大的布局系统,可以直接在 Vue 组件的样式中使用。通过定义网格容器和网格项,可以快速实现复杂的布局结构。…

vue实现门户

vue实现门户

Vue 实现门户网站的关键步骤 搭建基础框架 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理多页面路由。门户通常需要响应式布局,可引入 UI 库如 Element P…