当前位置:首页 > VUE

vue实现顶部轮播图

2026-02-24 09:33:05VUE

使用Swiper实现轮播图

安装Swiper库

npm install swiper@6

引入Swiper样式和组件

<template>
  <div class="banner-container">
    <swiper ref="mySwiper" :options="swiperOptions">
      <swiper-slide v-for="(item, index) in banners" :key="index">
        <img :src="item.imageUrl" class="banner-image" />
      </swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
    </swiper>
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/swiper-bundle.min.css'
import SwiperCore, { Pagination, Autoplay } from 'swiper'

SwiperCore.use([Pagination, Autoplay])

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      banners: [
        { imageUrl: 'https://example.com/banner1.jpg' },
        { imageUrl: 'https://example.com/banner2.jpg' },
        { imageUrl: 'https://example.com/banner3.jpg' }
      ],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true
      }
    }
  }
}
</script>

<style scoped>
.banner-container {
  width: 100%;
  height: 300px;
}
.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.swiper-pagination-bullet-active {
  background: #fff;
}
</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="'Slide ' + index">
      </div>
    </div>
    <button class="prev" @click="prev">❮</button>
    <button class="next" @click="next">❯</button>
  </div>
</template>

<script>
export default {
  props: {
    slides: {
      type: Array,
      required: true,
      default: () => [
        { image: 'https://example.com/slide1.jpg' },
        { image: 'https://example.com/slide2.jpg' },
        { image: 'https://example.com/slide3.jpg' }
      ]
    },
    interval: {
      type: Number,
      default: 3000
    }
  },
  data() {
    return {
      currentIndex: 0,
      timer: null
    }
  },
  computed: {
    slideStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`
      }
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    this.stopAutoPlay()
  },
  methods: {
    startAutoPlay() {
      this.timer = setInterval(this.next, this.interval)
    },
    stopAutoPlay() {
      clearInterval(this.timer)
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    }
  }
}
</script>

<style>
.carousel {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
}
.slides {
  display: flex;
  height: 100%;
  transition: transform 0.5s ease;
}
.slide {
  min-width: 100%;
  height: 100%;
}
.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.prev, .next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0,0,0,0.5);
  color: white;
  border: none;
  padding: 10px;
  cursor: pointer;
  z-index: 10;
}
.prev {
  left: 10px;
}
.next {
  right: 10px;
}
</style>

响应式设计要点

设置不同断点的样式

@media (max-width: 768px) {
  .banner-container {
    height: 200px;
  }
}

@media (max-width: 480px) {
  .banner-container {
    height: 150px;
  }
}

性能优化建议

图片懒加载实现

<template>
  <img 
    v-for="(item, index) in banners" 
    :key="index" 
    :src="currentIndex === index ? item.imageUrl : ''" 
    :alt="'Banner ' + index"
    class="banner-image"
  />
</template>

预加载重要图片

mounted() {
  this.banners.forEach(banner => {
    const img = new Image()
    img.src = banner.imageUrl
  })
}

vue实现顶部轮播图

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

相关文章

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现级联

vue实现级联

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

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客…