当前位置:首页 > VUE

vue实现按卡片轮播

2026-03-06 16:13:37VUE

Vue实现卡片轮播的几种方法

使用Swiper.js实现卡片轮播

安装Swiper依赖:

npm install swiper

在Vue组件中使用:

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(card, index) in cards" :key="index">
        <!-- 卡片内容 -->
        <div class="card">
          <h3>{{ card.title }}</h3>
          <p>{{ card.content }}</p>
        </div>
      </div>
    </div>
    <!-- 分页器 -->
    <div class="swiper-pagination"></div>
  </div>
</template>

<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.css'

export default {
  data() {
    return {
      cards: [
        { title: '卡片1', content: '内容1' },
        { title: '卡片2', content: '内容2' },
        { title: '卡片3', content: '内容3' }
      ]
    }
  },
  mounted() {
    new Swiper('.swiper-container', {
      slidesPerView: 3,
      spaceBetween: 30,
      pagination: {
        el: '.swiper-pagination',
        clickable: true
      }
    })
  }
}
</script>

<style>
.swiper-container {
  width: 100%;
  height: 300px;
}
.card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>

使用纯CSS实现简单轮播

<template>
  <div class="carousel">
    <div class="carousel-container" :style="{ transform: `translateX(${offset}px)` }">
      <div class="card" v-for="(card, index) in cards" :key="index">
        <h3>{{ card.title }}</h3>
        <p>{{ card.content }}</p>
      </div>
    </div>
    <button @click="prev">上一张</button>
    <button @click="next">下一张</button>
  </div>
</template>

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

<style>
.carousel {
  width: 300px;
  overflow: hidden;
  position: relative;
}
.carousel-container {
  display: flex;
  transition: transform 0.5s ease;
}
.card {
  flex: 0 0 300px;
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  margin-right: 20px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>

使用Vue专用轮播组件

安装vue-awesome-swiper:

npm install vue-awesome-swiper swiper

使用示例:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(card, index) in cards" :key="index">
      <div class="card">
        <h3>{{ card.title }}</h3>
        <p>{{ card.content }}</p>
      </div>
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      cards: [
        { title: '卡片1', content: '内容1' },
        { title: '卡片2', content: '内容2' },
        { title: '卡片3', content: '内容3' }
      ],
      swiperOption: {
        slidesPerView: 3,
        spaceBetween: 30,
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        }
      }
    }
  }
}
</script>

使用Vant UI的轮播组件

安装Vant:

npm install vant

使用示例:

vue实现按卡片轮播

<template>
  <van-swipe :loop="false" :width="300">
    <van-swipe-item v-for="(card, index) in cards" :key="index">
      <div class="card">
        <h3>{{ card.title }}</h3>
        <p>{{ card.content }}</p>
      </div>
    </van-swipe-item>
  </van-swipe>
</template>

<script>
import { Swipe, SwipeItem } from 'vant'

export default {
  components: {
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem
  },
  data() {
    return {
      cards: [
        { title: '卡片1', content: '内容1' },
        { title: '卡片2', content: '内容2' },
        { title: '卡片3', content: '内容3' }
      ]
    }
  }
}
</script>

关键注意事项

  • 卡片宽度需要根据实际需求调整,确保与轮播容器匹配
  • 对于响应式设计,可以使用媒体查询或Swiper的breakpoints参数
  • 自动轮播功能可通过设置autoplay参数实现
  • 触摸滑动和鼠标拖动功能默认在大多数轮播库中已启用
  • 考虑添加过渡动画效果提升用户体验

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

相关文章

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…

vue事件实现

vue事件实现

Vue 事件实现方法 在Vue中,事件处理是通过v-on指令或@简写来实现的。以下是几种常见的事件处理方式: 使用v-on指令绑定事件 <button v-on:click="handleC…

vue实现webshell

vue实现webshell

Vue 实现 WebShell 使用 Vue 实现 WebShell 需要结合前端与后端的交互,通常通过 WebSocket 或 HTTP 协议与服务器通信。以下是一个基于 Vue 和 WebSock…

vue实现幻灯

vue实现幻灯

Vue实现幻灯片的基础方法 使用Vue实现幻灯片功能可以通过多种方式完成,以下是几种常见方案: 方案一:基于Vue的过渡动画 通过Vue的<transition>或<transit…