当前位置:首页 > 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;
}

洗牌方法

使用Vue实现洗牌动画

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

使用Vue实现洗牌动画

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 中实现画布功能可以通过 HTML5 的 <canvas> 元素结合 Vue 的响应式特性来完成。以下是几种常见的实现方式: 使用原生 <canv…

Vue中实现路由

Vue中实现路由

Vue 路由的实现方法 在 Vue 中实现路由通常使用 Vue Router 库。以下是具体的实现步骤: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Router: n…

vue实现border动画

vue实现border动画

实现边框动画的方法 在Vue中实现边框动画可以通过CSS动画或过渡效果结合Vue的动态样式绑定来实现。以下是几种常见的方法: 使用CSS过渡和Vue的class绑定 通过动态添加或移除CSS类来触发…

Vue实现音乐列表

Vue实现音乐列表

Vue 实现音乐列表的方法 数据准备与绑定 在 Vue 中实现音乐列表,首先需要准备音乐数据。可以通过数组形式存储音乐信息,例如歌曲名称、歌手、封面图片和播放链接。数据可以存储在组件的 data 属性…

vue实现动画代码

vue实现动画代码

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,包括内置过渡组件、CSS 动画库集成以及第三方动画库结合。以下是几种常见实现方法: 使用 Vue 内置过渡组件 Vue 的 <tran…

Vue拖拽怎么实现

Vue拖拽怎么实现

Vue拖拽实现方法 使用Vue实现拖拽功能可以通过原生HTML5的拖拽API或第三方库如vuedraggable来实现。以下是两种常见的方法: 使用HTML5拖拽API HTML5提供了原生的拖拽A…