当前位置:首页 > 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实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量…

vue底层实现

vue底层实现

Vue.js 的底层实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现细节: 响应式系统 Vue…