当前位置:首页 > 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实现中的计算方法

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中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const BaseC…

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project c…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…