当前位置:首页 > 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 el 实现

vue el 实现

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

vue实现按钮

vue实现按钮

Vue 实现按钮的方法 使用原生 HTML 按钮 在 Vue 模板中可以直接使用 HTML 的 <button> 元素,通过 v-on 或 @ 绑定点击事件。 <template&…

vue实现例子

vue实现例子

以下是一些常见的 Vue 实现例子,涵盖基础功能到进阶应用场景: 基础数据绑定 使用 v-model 实现双向数据绑定: <template> <div> &l…

node vue实现

node vue实现

Node.js 与 Vue.js 实现方案 环境搭建 安装 Node.js(建议 LTS 版本),通过 npm 或 yarn 初始化项目。Vue.js 可通过 Vue CLI 快速搭建: npm…

vue路由实现滑动

vue路由实现滑动

实现 Vue 路由滑动效果的方法 使用 Vue Router 结合 CSS 过渡动画 在 Vue Router 的路由视图组件 <router-view> 上添加过渡效果,结合 CSS 实…

vue rooter 实现原理

vue rooter 实现原理

Vue Router 实现原理 Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。其核心实现原理包括路由匹配、组件渲染和导航守卫等机制。 路由匹配与动态路由 V…