当前位置:首页 > 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 库实现更丰富的滑动效果。

安装 swiper

npm install swiper vue-awesome-swiper

使用示例:

<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中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm insta…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项…

vue 动画实现

vue 动画实现

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