当前位置:首页 > VUE

vue实现轮播展示

2026-01-19 20:31:07VUE

使用 Vue 实现轮播展示

基于 Swiper 插件实现

安装 Swiper 依赖:

npm install swiper

在 Vue 组件中引入 Swiper:

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(item, index) in slides" :key="index">
        <img :src="item.image" :alt="item.title">
      </div>
    </div>
    <div class="swiper-pagination"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

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

export default {
  data() {
    return {
      slides: [
        { image: 'image1.jpg', title: 'Slide 1' },
        { image: 'image2.jpg', title: 'Slide 2' },
        { image: 'image3.jpg', title: 'Slide 3' }
      ]
    }
  },
  mounted() {
    new Swiper('.swiper-container', {
      loop: true,
      autoplay: {
        delay: 3000,
        disableOnInteraction: false
      },
      pagination: {
        el: '.swiper-pagination',
        clickable: true
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev'
      }
    })
  }
}
</script>

<style>
.swiper-container {
  width: 100%;
  height: 400px;
}
.swiper-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

纯 Vue 实现轮播

不使用第三方库的简单实现:

<template>
  <div class="carousel">
    <div class="slides" :style="slideStyle">
      <div class="slide" v-for="(item, index) in slides" :key="index">
        <img :src="item.image" :alt="item.title">
      </div>
    </div>
    <button class="prev" @click="prevSlide">‹</button>
    <button class="next" @click="nextSlide">›</button>
    <div class="dots">
      <span 
        v-for="(dot, index) in slides" 
        :key="index" 
        :class="{ active: currentIndex === index }"
        @click="goToSlide(index)"
      ></span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { image: 'image1.jpg', title: 'Slide 1' },
        { image: 'image2.jpg', title: 'Slide 2' },
        { image: 'image3.jpg', title: 'Slide 3' }
      ]
    }
  },
  computed: {
    slideStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`
      }
    }
  },
  methods: {
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    },
    goToSlide(index) {
      this.currentIndex = index
    }
  },
  mounted() {
    setInterval(this.nextSlide, 3000)
  }
}
</script>

<style>
.carousel {
  position: relative;
  width: 100%;
  height: 400px;
  overflow: hidden;
}
.slides {
  display: flex;
  height: 100%;
  transition: transform 0.5s ease;
}
.slide {
  min-width: 100%;
}
.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.prev, .next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0,0,0,0.5);
  color: white;
  border: none;
  padding: 10px;
  cursor: pointer;
  z-index: 10;
}
.prev {
  left: 10px;
}
.next {
  right: 10px;
}
.dots {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
}
.dots span {
  width: 10px;
  height: 10px;
  margin: 0 5px;
  border-radius: 50%;
  background: #ccc;
  cursor: pointer;
}
.dots span.active {
  background: #333;
}
</style>

使用 Vue-Awesome-Swiper 组件

安装依赖:

npm install vue-awesome-swiper swiper

组件实现:

<template>
  <swiper :options="swiperOption" ref="mySwiper">
    <swiper-slide v-for="(item, index) in slides" :key="index">
      <img :src="item.image" :alt="item.title">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
    <div class="swiper-button-prev" slot="button-prev"></div>
    <div class="swiper-button-next" slot="button-next"></div>
  </swiper>
</template>

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

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      slides: [
        { image: 'image1.jpg', title: 'Slide 1' },
        { image: 'image2.jpg', title: 'Slide 2' },
        { image: 'image3.jpg', title: 'Slide 3' }
      ],
      swiperOption: {
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    }
  }
}
</script>

<style>
.swiper-container {
  width: 100%;
  height: 400px;
}
.swiper-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

响应式轮播实现

在 Swiper 配置中添加响应式断点:

swiperOption: {
  // ...其他配置
  breakpoints: {
    640: {
      slidesPerView: 1
    },
    768: {
      slidesPerView: 2
    },
    1024: {
      slidesPerView: 3
    }
  }
}

vue实现轮播展示

标签: vue
分享给朋友:

相关文章

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="w…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 29…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…