当前位置:首页 > VUE

vue实现卡片左右滑动

2026-02-21 02:38:31VUE

Vue实现卡片左右滑动

使用Touch事件和CSS过渡

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

vue实现卡片左右滑动

<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是一个流行的滑动库,提供了丰富的配置选项和响应式设计,适合快速实现复杂的滑动效果。

vue实现卡片左右滑动

<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实现分类功能的方法 使用v-for和计算属性 通过计算属性对数据进行分类处理,结合v-for渲染分类后的列表。示例代码: <template> <div>…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue实现换页

vue实现换页

Vue实现页面切换的方法 在Vue中实现页面切换通常可以通过Vue Router来完成。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。以下是几种常见的实现方式:…