当前位置:首页 > VUE

vue实现卡片叠加滑动

2026-02-22 13:00:04VUE

Vue 实现卡片叠加滑动效果

实现卡片叠加滑动效果可以通过 CSS 结合 Vue 的动态数据绑定和过渡动画完成。以下是具体实现方法:

依赖准备 确保项目中已安装 Vue 及相关动画库(如 vue-transition 或直接使用原生 CSS 过渡)。

基本数据结构 在 Vue 的 data 中定义卡片数组和当前激活的卡片索引:

data() {
  return {
    cards: [
      { id: 1, title: 'Card 1' },
      { id: 2, title: 'Card 2' },
      { id: 3, title: 'Card 3' }
    ],
    activeIndex: 0
  }
}

模板结构 使用 v-for 渲染卡片,并通过 z-indextransform 控制叠加效果:

<div class="card-container">
  <div 
    v-for="(card, index) in cards"
    :key="card.id"
    class="card"
    :style="{
      zIndex: cards.length - index,
      transform: getCardTransform(index)
    }"
    @click="activateCard(index)"
  >
    {{ card.title }}
  </div>
</div>

样式设计 通过 CSS 实现叠加和滑动动画:

.card-container {
  position: relative;
  width: 300px;
  height: 200px;
  margin: 0 auto;
}

.card {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
  cursor: pointer;
}

动态位置计算 在 Vue 方法中计算每张卡片的偏移量:

methods: {
  getCardTransform(index) {
    const offset = 20; // 叠加偏移量
    const activeOffset = index === this.activeIndex ? 0 : 10;
    return `translate(${index * offset}px, ${index * offset - activeOffset}px)`;
  },
  activateCard(index) {
    this.activeIndex = index;
  }
}

进阶交互 添加滑动动画和手势控制(需引入 hammerjs 等库):

import Hammer from 'hammerjs';

mounted() {
  const container = document.querySelector('.card-container');
  const hammer = new Hammer(container);
  hammer.on('swipeleft', () => {
    this.activeIndex = Math.min(this.activeIndex + 1, this.cards.length - 1);
  });
  hammer.on('swiperight', () => {
    this.activeIndex = Math.max(this.activeIndex - 1, 0);
  });
}

响应式调整 通过 Vue 的 computed 属性实现响应式布局:

computed: {
  visibleCards() {
    return this.cards.slice(
      Math.max(0, this.activeIndex - 2),
      this.activeIndex + 1
    ).reverse();
  }
}

最终效果

vue实现卡片叠加滑动

  • 卡片会以堆叠形式展示,最上层卡片完全可见
  • 点击或滑动时切换激活卡片
  • 非激活卡片自动下沉并偏移形成立体效果

通过调整 CSS 的 transform 属性和过渡时间,可以进一步优化动画流畅度。对于移动端,建议增加触摸事件处理以提高交互体验。

标签: 卡片vue
分享给朋友:

相关文章

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue computed 实现

vue computed 实现

computed 的基本用法 在 Vue 中,computed 用于声明计算属性,这些属性基于响应式依赖进行缓存。只有当依赖发生变化时,计算属性才会重新计算。 new Vue({ data: {…

vue翻页实现

vue翻页实现

Vue翻页实现方法 基础分页组件实现 使用v-for和计算属性实现基础分页逻辑: <template> <div> <ul> <li…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…