当前位置:首页 > VUE

vue实现探探卡片

2026-02-17 19:37:28VUE

Vue 实现探探卡片效果

探探的卡片滑动效果主要依赖手势交互和动画过渡,以下是基于 Vue 的实现方法:

核心组件结构

<template>
  <div class="card-container" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd">
    <div 
      v-for="(card, index) in cards" 
      :key="card.id"
      :class="['card', { 'active': index === 0 }]"
      :style="{
        transform: index === 0 ? cardTransform : '',
        zIndex: cards.length - index
      }"
    >
      <img :src="card.image" />
    </div>
  </div>
</template>

手势交互逻辑

data() {
  return {
    cards: [...], // 卡片数据
    startX: 0,
    startY: 0,
    moveX: 0,
    moveY: 0,
    cardTransform: '',
    isDragging: false
  }
},
methods: {
  handleTouchStart(e) {
    this.startX = e.touches[0].clientX
    this.startY = e.touches[0].clientY
    this.isDragging = true
  },

  handleTouchMove(e) {
    if (!this.isDragging) return
    this.moveX = e.touches[0].clientX - this.startX
    this.moveY = e.touches[0].clientY - this.startY

    const rotation = this.moveX * 0.1
    this.cardTransform = `translate(${this.moveX}px, ${this.moveY}px) rotate(${rotation}deg)`
  },

  handleTouchEnd() {
    if (!this.isDragging) return
    this.isDragging = false

    // 判断滑动距离决定是否移除卡片
    if (Math.abs(this.moveX) > 100) {
      this.removeCard()
    } else {
      this.resetCardPosition()
    }
  },

  removeCard() {
    this.cards.shift()
    this.resetCardPosition()
  },

  resetCardPosition() {
    this.cardTransform = ''
    this.moveX = 0
    this.moveY = 0
  }
}

CSS 样式关键部分

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

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

.card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

高级功能扩展

  1. 添加喜欢/不喜欢按钮
    
    methods: {
    like() {
     this.cardTransform = 'translate(200px, 0) rotate(30deg)'
     setTimeout(() => this.removeCard(), 300)
    },

dislike() { this.cardTransform = 'translate(-200px, 0) rotate(-30deg)' setTimeout(() => this.removeCard(), 300) } }


2. 卡片堆叠效果
```css
.card:not(.active) {
  transform: scale(0.9) translateY(20px);
  opacity: 0.8;
}
  1. 添加动画过渡
    watch: {
    cards(newVal) {
     if (newVal.length < 3) {
       // 加载更多卡片
     }
    }
    }

性能优化建议

  • 使用 Vue 的 <transition-group> 处理卡片进出动画
  • 对卡片图片进行懒加载
  • 使用 requestAnimationFrame 优化动画性能
  • 限制同时渲染的卡片数量(通常3-5张)

以上实现包含了探探卡片的核心交互逻辑,可根据实际需求调整样式和动画细节。

vue实现探探卡片

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

相关文章

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CS…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template&g…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…