当前位置:首页 > VUE

使用Vue实现洗牌动画

2026-01-20 13:05:36VUE

使用CSS和Vue实现洗牌动画

洗牌动画可以通过CSS过渡和Vue的动态数据绑定实现。以下是实现步骤:

准备数据

data() {
  return {
    cards: [
      { id: 1, value: 'A', suit: '♥' },
      { id: 2, value: '2', suit: '♦' },
      // 其他卡牌...
    ],
    isShuffling: false
  }
}

CSS样式

.card {
  width: 80px;
  height: 120px;
  position: absolute;
  transition: all 0.5s ease;
  background: white;
  border-radius: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  transform-origin: center;
}

洗牌方法

methods: {
  shuffle() {
    this.isShuffling = true;

    // 随机打乱数组
    for (let i = this.cards.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
    }

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

模板部分

<div class="deck">
  <div 
    v-for="(card, index) in cards" 
    :key="card.id"
    class="card"
    :style="{
      left: `${index * 10}px`,
      transform: isShuffling ? 
        `translate(${Math.random() * 100 - 50}px, ${Math.random() * 100 - 50}px) rotate(${Math.random() * 60 - 30}deg)` : 
        `translate(${index * 10}px, 0) rotate(0deg)`
    }"
  >
    <span>{{ card.value }}</span>
    <span>{{ card.suit }}</span>
  </div>
</div>

<button @click="shuffle">洗牌</button>

使用GSAP实现更流畅的动画

如果需要更复杂的动画效果,可以使用GSAP库:

安装GSAP

npm install gsap

使用GSAP实现动画

import { gsap } from 'gsap';

methods: {
  shuffleWithGSAP() {
    const cards = this.$el.querySelectorAll('.card');

    gsap.to(cards, {
      duration: 0.5,
      x: () => Math.random() * 200 - 100,
      y: () => Math.random() * 200 - 100,
      rotation: () => Math.random() * 360,
      ease: "power2.out",
      onComplete: () => {
        // 重新排列卡牌
        gsap.to(cards, {
          duration: 0.5,
          x: (i) => i * 10,
          y: 0,
          rotation: 0
        });
      }
    });

    // 同时打乱数据顺序
    this.cards.sort(() => Math.random() - 0.5);
  }
}

使用Vue Transition Group

Vue的TransitionGroup组件可以处理列表元素的进入/离开动画:

<transition-group name="shuffle" tag="div" class="deck">
  <div 
    v-for="card in cards" 
    :key="card.id"
    class="card"
  >
    {{ card.value }}{{ card.suit }}
  </div>
</transition-group>
.shuffle-move {
  transition: transform 0.5s;
}

添加3D效果

通过CSS 3D变换可以增强视觉效果:

.card {
  transform-style: preserve-3d;
  transition: transform 0.5s;
}

.card:hover {
  transform: rotateY(180deg);
}

以上方法可以根据需求组合使用,CSS过渡适合简单动画,GSAP适合复杂动画,TransitionGroup适合列表重排动画。

使用Vue实现洗牌动画

标签: 动画Vue
分享给朋友:

相关文章

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件可…

Vue函数实现

Vue函数实现

Vue 函数式组件的实现 Vue 函数式组件是一种无状态、无实例的组件形式,适用于简单渲染逻辑的场景。其核心特点是性能高效,适合纯展示型需求。 定义方式 export default {…

Vue实现思路

Vue实现思路

Vue 实现思路 Vue 是一种流行的前端框架,用于构建用户界面。其核心思想是通过数据驱动和组件化开发,使开发过程更加高效和可维护。以下是 Vue 实现的一些关键思路: 数据驱动视图 Vue 采用…

Vue数据依赖实现

Vue数据依赖实现

Vue数据依赖实现原理 Vue的数据依赖实现基于响应式系统,核心是通过Object.defineProperty或Proxy拦截数据读写操作,结合发布-订阅模式自动追踪依赖和触发更新。 核心机制 依…

vue实现gif动画

vue实现gif动画

Vue 中实现 GIF 动画的方法 在 Vue 中实现 GIF 动画可以通过多种方式完成,具体取决于需求和使用场景。以下是几种常见的方法: 直接使用 <img> 标签加载 GIF 最简…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要包括内置组件和第三方库。 使用 <transition> 和 <transition-group> Vue 内置…