当前位置:首页 > VUE

本地实现vue轮播

2026-02-19 04:26:38VUE

使用vue-awesome-swiper实现轮播

安装vue-awesome-swiper库

npm install swiper vue-awesome-swiper --save

在组件中引入并使用

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(item, index) in slides" :key="index">
      <img :src="item.image" alt="">
    </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 {
      slides: [
        { image: require('@/assets/slide1.jpg') },
        { image: require('@/assets/slide2.jpg') },
        { image: require('@/assets/slide3.jpg') }
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination'
        },
        autoplay: {
          delay: 3000
        },
        loop: true
      }
    }
  }
}
</script>

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

使用纯Vue实现简单轮播

创建基础轮播组件

<template>
  <div class="carousel">
    <div class="slides" :style="slideStyle">
      <div 
        v-for="(slide, index) in slides" 
        :key="index" 
        class="slide"
      >
        <img :src="slide.image" alt="">
      </div>
    </div>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      slides: [
        { image: require('@/assets/slide1.jpg') },
        { image: require('@/assets/slide2.jpg') },
        { image: require('@/assets/slide3.jpg') }
      ]
    }
  },
  computed: {
    slideStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`
      }
    }
  },
  methods: {
    prev() {
      this.currentIndex = 
        this.currentIndex === 0 
          ? this.slides.length - 1 
          : this.currentIndex - 1
    },
    next() {
      this.currentIndex = 
        this.currentIndex === this.slides.length - 1 
          ? 0 
          : this.currentIndex + 1
    }
  },
  mounted() {
    setInterval(this.next, 3000)
  }
}
</script>

<style scoped>
.carousel {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}
.slides {
  display: flex;
  width: 100%;
  height: 100%;
  transition: transform 0.5s ease;
}
.slide {
  flex: 0 0 100%;
  height: 100%;
}
.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

添加过渡效果

在纯Vue实现基础上添加过渡动画

<template>
  <div class="carousel">
    <transition-group 
      name="slide" 
      tag="div" 
      class="slides"
    >
      <div 
        v-for="(slide, index) in slides" 
        v-show="index === currentIndex"
        :key="index" 
        class="slide"
      >
        <img :src="slide.image" alt="">
      </div>
    </transition-group>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

<style scoped>
.slide-enter-active, 
.slide-leave-active {
  transition: opacity 0.5s, transform 0.5s;
  position: absolute;
  width: 100%;
}
.slide-enter {
  opacity: 0;
  transform: translateX(100%);
}
.slide-leave-to {
  opacity: 0;
  transform: translateX(-100%);
}
</style>

响应式轮播实现

添加响应式断点处理

data() {
  return {
    windowWidth: window.innerWidth,
    slidesToShow: 1
  }
},
created() {
  window.addEventListener('resize', this.handleResize)
  this.handleResize()
},
destroyed() {
  window.removeEventListener('resize', this.handleResize)
},
methods: {
  handleResize() {
    this.windowWidth = window.innerWidth
    if (this.windowWidth >= 1200) {
      this.slidesToShow = 3
    } else if (this.windowWidth >= 768) {
      this.slidesToShow = 2
    } else {
      this.slidesToShow = 1
    }
  }
}

无限循环轮播优化

修改纯Vue实现中的计算方法

本地实现vue轮播

methods: {
  prev() {
    this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
  },
  next() {
    this.currentIndex = (this.currentIndex + 1) % this.slides.length
  }
}

标签: vue
分享给朋友:

相关文章

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…