当前位置:首页 > VUE

vue实现滑动卡片切换

2026-02-24 18:52:44VUE

Vue 实现滑动卡片切换

使用 Vue 和 CSS 实现基础滑动效果

通过 Vue 的 v-for 和 CSS 过渡效果实现卡片滑动切换。定义一个数组存储卡片数据,使用 v-for 渲染卡片列表。

<template>
  <div class="card-container">
    <div 
      v-for="(card, index) in cards" 
      :key="index" 
      class="card" 
      :style="{ transform: `translateX(${currentIndex * -100}%)` }"
    >
      {{ card.content }}
    </div>
  </div>
  <button @click="prev">上一张</button>
  <button @click="next">下一张</button>
</template>

<script>
export default {
  data() {
    return {
      cards: [
        { content: '卡片1' },
        { content: '卡片2' },
        { content: '卡片3' }
      ],
      currentIndex: 0
    };
  },
  methods: {
    prev() {
      this.currentIndex = Math.max(0, this.currentIndex - 1);
    },
    next() {
      this.currentIndex = Math.min(this.cards.length - 1, this.currentIndex + 1);
    }
  }
};
</script>

<style>
.card-container {
  display: flex;
  overflow: hidden;
  width: 300px;
}
.card {
  flex: 0 0 100%;
  transition: transform 0.3s ease;
  padding: 20px;
  background: #eee;
  margin: 10px 0;
}
</style>

添加触摸事件支持

为移动端添加触摸事件支持,通过 touchstarttouchmovetouchend 实现滑动切换。

<template>
  <div 
    class="card-container"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    <!-- 卡片内容同上 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      touchStartX: 0,
      touchEndX: 0
    };
  },
  methods: {
    handleTouchStart(e) {
      this.touchStartX = e.touches[0].clientX;
    },
    handleTouchMove(e) {
      this.touchEndX = e.touches[0].clientX;
    },
    handleTouchEnd() {
      if (this.touchEndX < this.touchStartX - 50) {
        this.next();
      } else if (this.touchEndX > this.touchStartX + 50) {
        this.prev();
      }
    }
  }
};
</script>

使用第三方库实现高级效果

引入 swipervue-awesome-swiper 库实现更丰富的滑动效果。

vue实现滑动卡片切换

安装 swiper

npm install swiper vue-awesome-swiper

使用示例:

vue实现滑动卡片切换

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

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      cards: [
        { content: '卡片1' },
        { content: '卡片2' },
        { content: '卡片3' }
      ],
      swiperOptions: {
        slidesPerView: 1,
        spaceBetween: 10,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    };
  }
};
</script>

动画优化与性能调整

通过 CSS will-change 属性优化动画性能,避免不必要的重绘。

.card {
  will-change: transform;
}

响应式设计适配

根据屏幕宽度动态调整卡片显示数量,通过计算属性或媒体查询实现。

computed: {
  swiperOptions() {
    return {
      slidesPerView: window.innerWidth < 768 ? 1 : 2,
      spaceBetween: 10
    };
  }
}

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

相关文章

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…