当前位置:首页 > VUE

vue实现卡片左右滑动

2026-02-21 02:38:31VUE

Vue实现卡片左右滑动

使用Touch事件和CSS过渡

通过监听touchstart、touchmove和touchend事件,结合CSS的transform属性实现滑动效果。这种方法适合需要精细控制滑动行为的场景。

<template>
  <div 
    class="card-container"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    :style="{ transform: `translateX(${translateX}px)` }"
  >
    <div class="card" v-for="(card, index) in cards" :key="index">
      {{ card.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cards: [
        { content: 'Card 1' },
        { content: 'Card 2' },
        { content: 'Card 3' }
      ],
      startX: 0,
      currentX: 0,
      translateX: 0
    }
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX
    },
    handleTouchMove(e) {
      this.currentX = e.touches[0].clientX
      this.translateX = this.currentX - this.startX
    },
    handleTouchEnd() {
      if (this.translateX > 100) {
        // 向右滑动超过阈值,执行操作
      } else if (this.translateX < -100) {
        // 向左滑动超过阈值,执行操作
      }
      this.translateX = 0
    }
  }
}
</script>

<style>
.card-container {
  display: flex;
  transition: transform 0.3s ease;
}
.card {
  width: 200px;
  height: 300px;
  margin: 10px;
  background: #eee;
  flex-shrink: 0;
}
</style>

使用第三方库(如Swiper.js)

Swiper.js是一个流行的滑动库,提供了丰富的配置选项和响应式设计,适合快速实现复杂的滑动效果。

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(card, index) in cards" :key="index">
      <div class="card">
        {{ card.content }}
      </div>
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/swiper-bundle.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      cards: [
        { content: 'Card 1' },
        { content: 'Card 2' },
        { content: 'Card 3' }
      ],
      swiperOptions: {
        slidesPerView: 'auto',
        spaceBetween: 10,
        freeMode: true
      }
    }
  }
}
</script>

<style>
.card {
  width: 200px;
  height: 300px;
  background: #eee;
}
</style>

使用CSS Scroll Snap

CSS Scroll Snap是一种原生CSS解决方案,不需要JavaScript即可实现滑动效果,适合简单的滑动需求。

<template>
  <div class="scroll-container">
    <div class="card" v-for="(card, index) in cards" :key="index">
      {{ card.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cards: [
        { content: 'Card 1' },
        { content: 'Card 2' },
        { content: 'Card 3' }
      ]
    }
  }
}
</script>

<style>
.scroll-container {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 10px;
}
.card {
  width: 200px;
  height: 300px;
  background: #eee;
  flex-shrink: 0;
  scroll-snap-align: start;
}
</style>

使用Vue Draggable

如果需要实现可拖拽的卡片滑动效果,可以使用vue-draggable库。

<template>
  <draggable 
    v-model="cards" 
    class="drag-container" 
    item-key="id"
    @start="drag=true" 
    @end="drag=false"
  >
    <template #item="{element}">
      <div class="card">
        {{ element.content }}
      </div>
    </template>
  </draggable>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  components: { draggable },
  data() {
    return {
      cards: [
        { id: 1, content: 'Card 1' },
        { id: 2, content: 'Card 2' },
        { id: 3, content: 'Card 3' }
      ]
    }
  }
}
</script>

<style>
.drag-container {
  display: flex;
}
.card {
  width: 200px;
  height: 300px;
  margin: 10px;
  background: #eee;
}
</style>

每种方法都有其适用场景:原生实现适合轻量级需求,Swiper.js适合复杂交互,CSS Scroll Snap适合简单布局,而Vue Draggable适合需要重新排序的场景。根据项目需求选择最合适的方法。

vue实现卡片左右滑动

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

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template&g…

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transit…

vue实现webapp

vue实现webapp

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