当前位置:首页 > 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变换可以增强视觉效果:

使用Vue实现洗牌动画

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

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

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

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

相关文章

Vue实现多站点

Vue实现多站点

Vue实现多站点的方案 Vue.js可以通过多种方式实现多站点架构,具体方案取决于项目需求和规模。以下是常见的几种实现方法: 基于动态路由的SPA方案 适用于中小型多站点项目,所有站点共享同一个Vu…

jquery 动画

jquery 动画

jQuery 动画基础 jQuery 提供了一系列动画方法,用于实现元素的动态效果,如淡入淡出、滑动、自定义动画等。 常用动画方法 fadeIn() / fadeOut(): 元素的淡入和…

Vue数据依赖实现

Vue数据依赖实现

Vue数据依赖实现原理 Vue的数据依赖实现基于响应式系统,核心是通过Object.defineProperty或Proxy拦截数据变化,结合发布-订阅模式实现依赖收集和更新触发。 核心机制 依赖收…

Vue实现word导入

Vue实现word导入

Vue实现Word导入的方法 在Vue项目中实现Word文档导入功能,通常需要借助第三方库或插件。以下是几种常见的实现方式: 使用docx-parser库 安装docx-parser库: npm…

uniapp 退场动画

uniapp 退场动画

实现退场动画的方法 在UniApp中实现退场动画可以通过CSS动画、Vue过渡效果或第三方动画库完成。以下是几种常见方法: 使用CSS动画 通过定义@keyframes和animation属性实现退…

vue动画实现折叠

vue动画实现折叠

使用 Vue Transition 实现折叠动画 在 Vue 中可以通过 <transition> 组件和 CSS 过渡实现折叠效果。核心原理是动态切换元素的 height 属性,配合 o…