当前位置:首页 > VUE

vue实现刷卡

2026-02-10 11:10:58VUE

Vue 实现刷卡效果

实现刷卡效果通常涉及动画和交互设计,以下是一个基于 Vue 的实现方案:

安装依赖 确保项目中已安装 vuevue-router(如需页面切换效果)。若需复杂动画,可引入 animate.cssgsap

基础动画实现 通过 Vue 的过渡系统结合 CSS 实现滑动效果:

<template>
  <div class="swipe-container">
    <transition name="swipe">
      <div v-if="showCard" class="card" @touchstart="startSwipe" @touchmove="moveSwipe" @touchend="endSwipe">
        <!-- 卡片内容 -->
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showCard: true,
      startX: 0,
      moveX: 0
    }
  },
  methods: {
    startSwipe(e) {
      this.startX = e.touches[0].clientX
    },
    moveSwipe(e) {
      this.moveX = e.touches[0].clientX - this.startX
      e.currentTarget.style.transform = `translateX(${this.moveX}px)`
    },
    endSwipe() {
      if (Math.abs(this.moveX) > 100) {
        this.showCard = false
        // 可在此处触发后续逻辑
      } else {
        this.$refs.card.style.transform = 'translateX(0)'
      }
    }
  }
}
</script>

<style>
.swipe-enter-active, .swipe-leave-active {
  transition: all 0.5s ease;
}
.swipe-enter, .swipe-leave-to {
  transform: translateX(100%);
  opacity: 0;
}
.card {
  width: 300px;
  height: 200px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>

进阶手势控制 对于更精细的手势控制,可引入 hammer.jsinteract.js

import Hammer from 'hammerjs'

mounted() {
  const hammer = new Hammer(this.$refs.card)
  hammer.on('swipe', (event) => {
    if (event.direction === Hammer.DIRECTION_RIGHT) {
      // 右滑逻辑
    } else if (event.direction === Hammer.DIRECTION_LEFT) {
      // 左滑逻辑
    }
  })
}

组件化实现 封装为可复用组件:

<!-- SwipeCard.vue -->
<template>
  <div class="swipe-card" ref="card" :style="cardStyle">
    <slot></slot>
  </div>
</template>

<script>
export default {
  props: {
    threshold: { type: Number, default: 100 }
  },
  data() {
    return {
      deltaX: 0,
      isSwiping: false
    }
  },
  computed: {
    cardStyle() {
      return {
        transform: `translateX(${this.deltaX}px)`,
        transition: this.isSwiping ? 'none' : 'transform 0.3s ease'
      }
    }
  },
  methods: {
    handleSwipe(direction) {
      this.$emit('swipe', direction)
    }
  },
  mounted() {
    // 添加手势事件监听
  }
}
</script>

注意事项

  1. 移动端需考虑 touch 事件,PC 端需兼容 mouse 事件
  2. 性能优化:使用 transform 而非 left/top 属性
  3. 可结合 Vuex 管理卡片状态
  4. 复杂场景建议使用现成轮子如 vue-swipe-actions 等组件库

扩展功能

vue实现刷卡

  • 添加弹性动画效果
  • 实现堆叠卡片(tinder 式)
  • 集成 API 数据加载
  • 添加撤销功能

以上方案可根据实际需求调整动画细节和交互逻辑。

标签: vue
分享给朋友:

相关文章

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…