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

拖拽交互逻辑

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

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 属性优化动画性能:

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

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

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

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

vue实现探探卡片

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

相关文章

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterv…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…