当前位置:首页 > 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如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed: {…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…