当前位置:首页 > VUE

vue实现探探卡片

2026-01-17 03:10:22VUE

Vue 实现探探卡片效果

探探卡片的交互效果主要包括卡片堆叠、左右滑动、缩放和旋转动画。以下是基于 Vue 3 的实现方法:

核心组件结构

使用 v-for 渲染卡片列表,通过 CSS 绝对定位实现堆叠效果:

<template>
  <div class="cards-container">
    <div 
      v-for="(card, index) in cards" 
      :key="card.id"
      :style="getCardStyle(index)"
      @touchstart="startDrag"
      @touchmove="onDrag"
      @touchend="endDrag"
      class="card"
    >
      <!-- 卡片内容 -->
    </div>
  </div>
</template>

卡片样式与堆叠

通过 CSS 实现基础卡片样式和层叠效果:

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

.card {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  background-size: cover;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
}

拖拽交互逻辑

实现触摸事件处理函数,计算拖拽偏移量:

vue实现探探卡片

setup() {
  const cards = ref([...]);
  const startX = ref(0);
  const currentX = ref(0);
  const isDragging = ref(false);
  const currentCardIndex = ref(0);

  const startDrag = (e) => {
    startX.value = e.touches[0].clientX;
    isDragging.value = true;
  };

  const onDrag = (e) => {
    if (!isDragging.value) return;
    currentX.value = e.touches[0].clientX - startX.value;
    // 更新卡片位置
  };

  const endDrag = () => {
    if (Math.abs(currentX.value) > 100) {
      // 判断左滑或右滑
      removeCard();
    } else {
      // 回弹动画
      resetCardPosition();
    }
    isDragging.value = false;
  };

  return { cards, startDrag, onDrag, endDrag };
}

动画效果实现

动态计算卡片变换样式:

const getCardStyle = (index) => {
  const scale = 1 - index * 0.05;
  const yOffset = index * 10;
  let rotate = 0;

  if (index === 0 && isDragging.value) {
    rotate = currentX.value * 0.1;
  }

  return {
    transform: `
      translateY(${yOffset}px)
      scale(${scale})
      rotate(${rotate}deg)
      translateX(${index === 0 ? currentX.value : 0}px)
    `,
    zIndex: cards.value.length - index
  };
};

卡片移除与新增

实现滑动后的卡片移除逻辑:

const removeCard = () => {
  cards.value.shift();
  currentCardIndex.value++;
  currentX.value = 0;

  // 可以在这里添加新卡片
  if (cards.value.length < 3) {
    addNewCard();
  }
};

const addNewCard = () => {
  cards.value.push({
    id: generateId(),
    // 新卡片数据
  });
};

优化建议

  1. 使用 CSS will-change 属性优化动画性能:

    vue实现探探卡片

    .card {
      will-change: transform;
    }
  2. 添加滑动阈值判断,当滑动距离超过一定值时自动完成动画

  3. 实现卡片飞出的抛物线动画效果

  4. 添加双击点赞等附加交互功能

完整实现需要考虑移动端和桌面端的兼容性,建议使用 @vueuse/gesture 等手势库简化交互逻辑处理。

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

相关文章

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…

vue最佳实现

vue最佳实现

Vue 最佳实践 组件设计 组件应遵循单一职责原则,每个组件只负责一个功能模块。命名采用 PascalCase(如 UserProfile.vue),模板中使用 kebab-case(如 <us…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…