当前位置:首页 > 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库。

vue实现卡片左右滑动

<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 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…