当前位置:首页 > VUE

vue实现焦点轮播

2026-02-18 17:43:41VUE

Vue实现焦点轮播的方法

使用Vue的transition组件

在Vue中可以通过transition组件和CSS过渡效果实现轮播动画。定义多个轮播项,通过v-for循环渲染,利用v-show或v-if控制当前显示的项。

<template>
  <div class="carousel">
    <transition name="fade">
      <div v-for="(item, index) in items" :key="index" v-show="currentIndex === index">
        <img :src="item.image" :alt="item.title">
      </div>
    </transition>
    <button @click="prev">上一张</button>
    <button @click="next">下一张</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      items: [
        { image: 'image1.jpg', title: '图片1' },
        { image: 'image2.jpg', title: '图片2' },
        { image: 'image3.jpg', title: '图片3' }
      ]
    }
  },
  methods: {
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.items.length) % this.items.length
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.items.length
    }
  }
}
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

使用第三方库vue-awesome-swiper

vue-awesome-swiper是基于Swiper的Vue组件,提供了丰富的轮播功能配置。

安装依赖:

npm install swiper vue-awesome-swiper --save

使用示例:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(item, index) in items" :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 {
      items: [
        { image: 'image1.jpg', title: '图片1' },
        { image: 'image2.jpg', title: '图片2' },
        { image: 'image3.jpg', title: '图片3' }
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        }
      }
    }
  }
}
</script>

自动轮播功能实现

通过setInterval实现自动轮播,注意在组件销毁时清除定时器。

export default {
  data() {
    return {
      currentIndex: 0,
      timer: null,
      interval: 3000
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    this.stopAutoPlay()
  },
  methods: {
    startAutoPlay() {
      this.timer = setInterval(() => {
        this.next()
      }, this.interval)
    },
    stopAutoPlay() {
      if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
    }
  }
}

响应式设计

可以通过监听窗口大小变化调整轮播图尺寸。

export default {
  data() {
    return {
      windowWidth: 0
    }
  },
  mounted() {
    this.windowWidth = window.innerWidth
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.windowWidth = window.innerWidth
    }
  }
}

指示器和小圆点

添加轮播图指示器,显示当前轮播位置。

<div class="indicators">
  <span 
    v-for="(item, index) in items" 
    :key="index" 
    :class="{ active: currentIndex === index }"
    @click="goTo(index)"
  ></span>
</div>

<style>
.indicators span {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin: 0 5px;
  border-radius: 50%;
  background-color: #ccc;
  cursor: pointer;
}
.indicators span.active {
  background-color: #333;
}
</style>

vue实现焦点轮播

标签: 焦点vue
分享给朋友:

相关文章

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式…

vue实现tag

vue实现tag

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

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…