CSS动效

1. 卡片切换动画

html 复制代码
<template>
  <div class="page">
    <h1>卡片切换动画</h1>
    <van-button type="primary" @click="handleSwitch">切换</van-button>
    <div class="container">
      <div class="list" :class="{ enter: activeListIndex === 1, leave: activeListIndex !== 1 }">
        <div v-for="item in list1" :key="item.value" class="card">{{ item.label }}</div>
      </div>
      <div class="list" :class="{ enter: activeListIndex === 2, leave: activeListIndex !== 2 }">
        <div v-for="item in list2" :key="item.value" class="card">{{ item.label }}</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const list1 = ref([
  {
    label: '卡片1',
    value: 1
  },
  {
    label: '卡片2',
    value: 2
  },
  {
    label: '卡片3',
    value: 3
  }
])

const list2 = ref([
  {
    label: 'card4',
    value: 4
  },
  {
    label: 'card5',
    value: 5
  },
  {
    label: 'card6',
    value: 6
  }
])

const activeListIndex = ref(1)
function handleSwitch() {
  activeListIndex.value = activeListIndex.value === 1 ? 2 : 1
}
</script>

<style lang="scss" scoped>
.page {
  width: 100%;
  height: 100%;
  padding: 10px;
  color: #ffffff;
  background-color: black;

  .container {
    width: 100%;
    height: 80px;
    border: 1px solid #ccc;
    margin-top: 10px;
    padding: 0 10px;
    position: relative;
    overflow: hidden;

    .list {
      width: 100%;
      height: 100%;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      overflow-x: auto;
      gap: 10px;
      transition: transform 0.5s ease-in-out, opacity 0.5s ease-in-out;
      backface-visibility: hidden;
      position: absolute;

      &.enter {
        opacity: 1;
        transform: translateY(0);
        z-index: 2;
      }

      &.leave {
        opacity: 0;
        transform: translateY(100%);
        z-index: 1;
      }
    }

    .card {
      width: 60px;
      height: 60px;
      background-color: #404040;
      border-radius: 6rem;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
相关推荐
潇凝子潇9 分钟前
使用英伟达免费调用多家大模型API
java·前端·javascript
旷世奇才李先生22 分钟前
Vue 3\+Vite\+Pinia实战:前端工程化与组件化开发全指南
前端·vue.js
Beginner x_u27 分钟前
前端八股整理(手写 01)|Promise 超时控制、红绿灯与 Promise.all
前端·javascript·promise
万少10 小时前
Vibe Coding不停歇,移动端 TRAE SOLO 让你用手机也能编程啦
前端·javascript·后端
kyriewen1110 小时前
WebAssembly:前端界的“外挂”,让C++代码在浏览器里跑起来
开发语言·前端·javascript·c++·单元测试·ecmascript
烛衔溟11 小时前
TypeScript 接口的基本使用 —— 定义对象形状
前端·javascript·typescript
铁皮饭盒12 小时前
成为AI全栈 - 第3课:路由 RESTful Elysia 状态码 设计规范
前端·后端·全栈
顾昂_12 小时前
Web 性能优化完全指南
前端·面试·性能优化
IT乐手12 小时前
Claude Code + Qwen 的配置方法
javascript·claude
前端程序媛-Tian12 小时前
前端 AI 提效实战:从 0 到 1 打造团队专属 AI 代码评审工具
前端·人工智能·ai