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

vue实现探探卡片


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 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue 实现loading

vue 实现loading

Vue 实现 Loading 的方法 使用 v-if 和 v-show 控制显示 在 Vue 中可以通过 v-if 或 v-show 控制 loading 组件的显示与隐藏。v-if 会动态创建或销毁…