当前位置:首页 > VUE

vue实现翻牌效果案例

2026-02-21 09:47:19VUE

Vue 实现翻牌效果案例

翻牌效果常用于卡片翻转、抽奖、记忆游戏等场景。以下是一个基于 Vue 3 的翻牌效果实现方法,结合 CSS 3D 变换和 Vue 的响应式特性。

基础实现

  1. 创建 Vue 组件结构 在 Vue 单文件组件中,定义卡片的正反面内容和翻转状态。

    <template>
      <div class="flip-card" @click="flipCard">
        <div class="flip-card-inner" :class="{ 'flipped': isFlipped }">
          <div class="flip-card-front">
            <h3>正面内容</h3>
          </div>
          <div class="flip-card-back">
            <h3>背面内容</h3>
          </div>
        </div>
      </div>
    </template>
    
    <script setup>
    import { ref } from 'vue';
    const isFlipped = ref(false);
    const flipCard = () => {
      isFlipped.value = !isFlipped.value;
    };
    </script>
  2. 添加 CSS 动画样式 通过 CSS 的 transformtransition 实现 3D 翻转效果。

    .flip-card {
      perspective: 1000px;
      width: 200px;
      height: 300px;
    }
    
    .flip-card-inner {
      position: relative;
      width: 100%;
      height: 100%;
      transition: transform 0.6s;
      transform-style: preserve-3d;
    }
    
    .flip-card-inner.flipped {
      transform: rotateY(180deg);
    }
    
    .flip-card-front,
    .flip-card-back {
      position: absolute;
      width: 100%;
      height: 100%;
      backface-visibility: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
      border: 1px solid #ccc;
      border-radius: 8px;
    }
    
    .flip-card-back {
      transform: rotateY(180deg);
      background-color: #f0f0f0;
    }

进阶优化

  1. 动态内容绑定 通过 props 传递正反面内容,使组件可复用。

    <template>
      <div class="flip-card" @click="flipCard">
        <div class="flip-card-inner" :class="{ 'flipped': isFlipped }">
          <div class="flip-card-front">
            <slot name="front">{{ frontText }}</slot>
          </div>
          <div class="flip-card-back">
            <slot name="back">{{ backText }}</slot>
          </div>
        </div>
      </div>
    </template>
    
    <script setup>
    const props = defineProps({
      frontText: String,
      backText: String,
    });
    </script>
  2. 自动翻转控制 添加 autoFlip 属性支持自动翻转,通过 setTimeout 控制。

    <script setup>
    import { ref, watch } from 'vue';
    
    const props = defineProps({
      autoFlip: Boolean,
      flipDuration: { type: Number, default: 3000 },
    });
    
    watch(() => props.autoFlip, (newVal) => {
      if (newVal) {
        setTimeout(() => {
          isFlipped.value = !isFlipped.value;
        }, props.flipDuration);
      }
    });
    </script>
  3. 性能优化 使用 will-change 提升动画性能,避免频繁重绘。

    .flip-card-inner {
      will-change: transform;
    }

实际应用示例

  1. 记忆游戏场景 在记忆游戏中,可通过 v-for 渲染多个卡片,并绑定唯一标识符。

    <template>
      <div class="game-board">
        <FlipCard
          v-for="card in cards"
          :key="card.id"
          :front-text="card.question"
          :back-text="card.answer"
          @click="handleCardClick(card.id)"
        />
      </div>
    </template>
  2. 抽奖场景 结合 setInterval 实现连续翻转,模拟抽奖效果。

    vue实现翻牌效果案例

    const startLottery = () => {
      const interval = setInterval(() => {
        flipRandomCard();
      }, 100);
      setTimeout(() => clearInterval(interval), 2000);
    };

注意事项

  • 浏览器兼容性
    CSS 的 perspectivetransform-style 在旧版本浏览器可能需要前缀(如 -webkit-)。
  • 移动端适配
    添加 touchstart 事件支持移动端点击。
  • 无障碍访问
    为卡片添加 aria-labelrole="button" 提升可访问性。

标签: 案例效果
分享给朋友:

相关文章

vue实现翻页效果

vue实现翻页效果

使用Vue实现翻页效果 在Vue中实现翻页效果通常需要结合分页组件和数据处理逻辑。以下是几种常见方法: 基础分页组件实现 <template> <div>…

vue实现基金效果

vue实现基金效果

Vue 实现基金效果 在 Vue 中实现基金效果,通常指的是模拟基金的增长、波动或可视化展示。以下是几种常见的实现方式: 数据绑定与动态更新 通过 Vue 的数据绑定特性,可以动态展示基金净值的变化…

react如何添加效果

react如何添加效果

React 添加动画效果的方法 使用 CSS 过渡动画 在 React 组件中直接通过 className 或 style 属性应用 CSS 过渡效果。定义 CSS 类包含 transition 属性…

vue实现过渡效果

vue实现过渡效果

Vue 过渡效果实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于在元素或组件进入/离开 DOM 时应用过渡效果。以…

vue 实现toogle效果

vue 实现toogle效果

使用 v-if 和 v-else 实现切换 通过 Vue 的指令 v-if 和 v-else 可以轻松实现元素的显示与隐藏切换。 <template> <button @c…

vue实现多选效果

vue实现多选效果

Vue 实现多选效果的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选效果。适用于复选框组(checkbox)或下拉多选(select multi…