当前位置:首页 > VUE

用vue实现敲瓶子

2026-02-23 15:52:54VUE

实现敲瓶子动画的Vue组件

创建Vue组件实现敲击瓶子动画效果,需要结合CSS动画和用户交互事件。

组件模板结构

<template>
  <div class="bottle-container">
    <div 
      class="bottle" 
      :class="{ 'bottle-hit': isHit }"
      @click="hitBottle"
    >
      <div class="bottle-neck"></div>
      <div class="bottle-body"></div>
    </div>
    <div class="sound-indicator" v-if="showSound">
      ~~~
    </div>
  </div>
</template>

组件脚本逻辑

<script>
export default {
  data() {
    return {
      isHit: false,
      showSound: false
    }
  },
  methods: {
    hitBottle() {
      this.isHit = true;
      this.showSound = true;

      setTimeout(() => {
        this.isHit = false;
      }, 500);

      setTimeout(() => {
        this.showSound = false;
      }, 1000);
    }
  }
}
</script>

样式设计

<style scoped>
.bottle-container {
  position: relative;
  display: inline-block;
}

.bottle {
  position: relative;
  width: 60px;
  height: 120px;
  cursor: pointer;
  transition: transform 0.1s;
}

.bottle-neck {
  position: absolute;
  top: 0;
  left: 20px;
  width: 20px;
  height: 30px;
  background: #4CAF50;
  border-radius: 5px 5px 0 0;
}

.bottle-body {
  position: absolute;
  top: 30px;
  width: 60px;
  height: 90px;
  background: #4CAF50;
  border-radius: 0 0 10px 10px;
}

.bottle-hit {
  animation: hit 0.5s ease;
}

.sound-indicator {
  position: absolute;
  top: -30px;
  left: 70px;
  font-size: 24px;
  animation: fade 1s forwards;
}

@keyframes hit {
  0% { transform: rotate(0deg); }
  25% { transform: rotate(-15deg); }
  50% { transform: rotate(10deg); }
  75% { transform: rotate(-5deg); }
  100% { transform: rotate(0deg); }
}

@keyframes fade {
  0% { opacity: 1; }
  100% { opacity: 0; }
}
</style>

增强交互效果

可以添加声音效果增强体验,需要引入音频文件:

// 在methods中修改hitBottle方法
hitBottle() {
  this.isHit = true;
  this.showSound = true;

  // 播放声音
  const audio = new Audio(require('@/assets/bottle-hit.mp3'));
  audio.play();

  setTimeout(() => {
    this.isHit = false;
  }, 500);

  setTimeout(() => {
    this.showSound = false;
  }, 1000);
}

多瓶子排列实现

创建多个瓶子实例:

<template>
  <div class="bottle-row">
    <Bottle v-for="(bottle, index) in 5" :key="index" />
  </div>
</template>

<style>
.bottle-row {
  display: flex;
  gap: 20px;
}
</style>

响应式设计

添加响应式尺寸调整:

@media (max-width: 600px) {
  .bottle {
    width: 40px;
    height: 80px;
  }

  .bottle-neck {
    width: 15px;
    height: 20px;
    left: 12.5px;
  }

  .bottle-body {
    top: 20px;
    width: 40px;
    height: 60px;
  }
}

用vue实现敲瓶子

标签: 瓶子vue
分享给朋友:

相关文章

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: da…

vue实现积分

vue实现积分

Vue 实现积分功能 在 Vue 中实现积分功能通常涉及前端展示、交互逻辑以及与后端 API 的通信。以下是常见的实现方法和步骤: 积分展示组件 创建一个积分展示组件,用于显示当前用户的积分。可以使…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…