当前位置:首页 > VUE

vue实现骰子

2026-02-10 04:27:33VUE

Vue 实现骰子功能

创建基本骰子组件

使用 Vue 单文件组件实现骰子的基本结构,包括模板、样式和逻辑。骰子的六个面可以通过 CSS 3D 变换实现立体效果。

<template>
  <div class="dice-container">
    <div class="dice" :class="'show-' + currentFace">
      <div class="face face-1">1</div>
      <div class="face face-2">2</div>
      <div class="face face-3">3</div>
      <div class="face face-4">4</div>
      <div class="face face-5">5</div>
      <div class="face face-6">6</div>
    </div>
    <button @click="rollDice">Roll Dice</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentFace: 1
    }
  },
  methods: {
    rollDice() {
      this.currentFace = Math.floor(Math.random() * 6) + 1
    }
  }
}
</script>

<style scoped>
.dice-container {
  perspective: 1000px;
  margin: 50px;
}

.dice {
  width: 100px;
  height: 100px;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 1s;
}

.face {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 2px solid #000;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  background: white;
}

/* 骰子各面定位 */
.face-1 { transform: rotateY(0deg) translateZ(50px); }
.face-2 { transform: rotateY(90deg) translateZ(50px); }
.face-3 { transform: rotateY(180deg) translateZ(50px); }
.face-4 { transform: rotateY(-90deg) translateZ(50px); }
.face-5 { transform: rotateX(90deg) translateZ(50px); }
.face-6 { transform: rotateX(-90deg) translateZ(50px); }

/* 显示当前面 */
.show-1 { transform: rotateY(0deg); }
.show-2 { transform: rotateY(-90deg); }
.show-3 { transform: rotateY(-180deg); }
.show-4 { transform: rotateY(90deg); }
.show-5 { transform: rotateX(-90deg); }
.show-6 { transform: rotateX(90deg); }
</style>

添加动画效果

为骰子滚动添加更逼真的动画效果,使用 CSS 关键帧动画模拟骰子旋转过程。

vue实现骰子

.dice {
  animation: roll 1s ease-out;
}

@keyframes roll {
  0% { transform: rotateX(0deg) rotateY(0deg); }
  100% { transform: rotateX(720deg) rotateY(360deg); }
}

实现多个骰子

扩展组件以支持多个骰子同时滚动,并计算总点数。

vue实现骰子

<template>
  <div>
    <div class="dices">
      <div 
        v-for="(dice, index) in dices" 
        :key="index" 
        class="dice" 
        :class="'show-' + dice.value"
      >
        <div class="face face-1">1</div>
        <div class="face face-2">2</div>
        <div class="face face-3">3</div>
        <div class="face face-4">4</div>
        <div class="face face-5">5</div>
        <div class="face face-6">6</div>
      </div>
    </div>
    <button @click="rollAll">Roll All</button>
    <p>Total: {{ total }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dices: [
        { value: 1 },
        { value: 1 },
        { value: 1 }
      ]
    }
  },
  computed: {
    total() {
      return this.dices.reduce((sum, dice) => sum + dice.value, 0)
    }
  },
  methods: {
    rollAll() {
      this.dices.forEach(dice => {
        dice.value = Math.floor(Math.random() * 6) + 1
      })
    }
  }
}
</script>

添加音效

为骰子滚动添加音效增强用户体验,使用 HTML5 Audio API。

<template>
  <button @click="rollWithSound">Roll With Sound</button>
</template>

<script>
export default {
  methods: {
    rollWithSound() {
      const audio = new Audio('dice_sound.mp3')
      audio.play()
      this.rollDice()
    }
  }
}
</script>

响应式设计

确保骰子在不同设备上都能正常显示,添加响应式 CSS。

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

  .face {
    font-size: 20px;
  }
}

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

相关文章

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

不用vue实现spa

不用vue实现spa

使用原生 JavaScript 实现 SPA 通过监听 URL 变化动态加载内容,结合 history.pushState 或 hashchange 事件实现路由切换。 // 路由配置 const…

vue 实现设置密码

vue 实现设置密码

Vue 实现设置密码功能 在 Vue 中实现设置密码功能通常涉及表单输入、验证和提交逻辑。以下是实现的基本步骤和代码示例。 创建密码表单组件 使用 Vue 的单文件组件(SFC)创建一个密码设置表单…