当前位置:首页 > 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 CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue单选实现

vue单选实现

Vue 单选实现 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…

vue实现例子

vue实现例子

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

vue实现录音

vue实现录音

Vue 实现录音功能 在 Vue 中实现录音功能可以通过浏览器提供的 MediaRecorder API 完成。以下是具体实现方法: 引入依赖 确保项目支持现代浏览器 API,无需额外安装库…