当前位置:首页 > VUE

vue实现轮播页

2026-02-19 23:47:39VUE

vue实现轮播页

使用第三方库(如Swiper)

安装Swiper库:

npm install swiper

在Vue组件中引入Swiper:

vue实现轮播页

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(slide, index) in slides" :key="index">
        <img :src="slide.image" :alt="slide.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/swiper-bundle.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,
      pagination: {
        el: '.swiper-pagination',
        clickable: true
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev'
      }
    });
  }
};
</script>

<style>
.swiper-container {
  width: 100%;
  height: 300px;
}
</style>

自定义轮播组件

通过Vue的过渡效果和定时器实现简单轮播:

vue实现轮播页

<template>
  <div class="carousel">
    <transition :name="transitionName">
      <div v-if="show" :key="currentIndex">
        <img :src="slides[currentIndex].image" :alt="slides[currentIndex].title">
      </div>
    </transition>
    <div class="controls">
      <button @click="prev">Prev</button>
      <button @click="next">Next</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      slides: [
        { image: 'image1.jpg', title: 'Slide 1' },
        { image: 'image2.jpg', title: 'Slide 2' },
        { image: 'image3.jpg', title: 'Slide 3' }
      ],
      currentIndex: 0,
      show: true,
      transitionName: 'slide-left',
      timer: null
    };
  },
  mounted() {
    this.startAutoPlay();
  },
  methods: {
    prev() {
      this.transitionName = 'slide-right';
      this.show = false;
      setTimeout(() => {
        this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
        this.show = true;
      }, 300);
    },
    next() {
      this.transitionName = 'slide-left';
      this.show = false;
      setTimeout(() => {
        this.currentIndex = (this.currentIndex + 1) % this.slides.length;
        this.show = true;
      }, 300);
    },
    startAutoPlay() {
      this.timer = setInterval(this.next, 3000);
    },
    stopAutoPlay() {
      clearInterval(this.timer);
    }
  },
  beforeDestroy() {
    this.stopAutoPlay();
  }
};
</script>

<style>
.carousel {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}

.slide-left-enter-active, .slide-left-leave-active,
.slide-right-enter-active, .slide-right-leave-active {
  transition: transform 0.3s ease;
}

.slide-left-enter {
  transform: translateX(100%);
}

.slide-left-leave-to {
  transform: translateX(-100%);
}

.slide-right-enter {
  transform: translateX(-100%);
}

.slide-right-leave-to {
  transform: translateX(100%);
}
</style>

使用Vue专用轮播组件(如vue-awesome-swiper)

安装vue-awesome-swiper:

npm install vue-awesome-swiper swiper

使用示例:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" :alt="slide.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/swiper-bundle.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,
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    };
  }
};
</script>

<style>
.swiper-container {
  width: 100%;
  height: 300px;
}
</style>

标签: vue轮播页
分享给朋友:

相关文章

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { letters…

vue实现新闻

vue实现新闻

Vue 实现新闻功能 使用 Vue 实现新闻功能通常涉及新闻列表展示、新闻详情页、分类筛选等模块。以下是实现的基本思路和代码示例。 新闻列表展示 通过 Vue 组件展示新闻列表,通常使用 v-fo…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…