当前位置:首页 > 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>

预加载重要图片

vue实现顶部轮播图

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

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

相关文章

vue实现分支

vue实现分支

Vue 实现分支的方法 在 Vue 项目中实现分支功能通常涉及条件渲染、动态组件或路由控制。以下是几种常见的实现方式: 条件渲染(v-if/v-show) 使用 Vue 的指令根据条件显示不同内容:…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…

vue 实现上传

vue 实现上传

Vue 实现文件上传 使用 Vue 实现文件上传可以通过原生 HTML 的 <input type="file"> 或第三方库(如 axios、vue-upload-component)完…

vue实现权限

vue实现权限

Vue 实现权限管理的方法 在 Vue 项目中实现权限管理通常涉及路由控制、按钮权限和接口权限。以下是几种常见的实现方式: 路由权限控制 通过动态路由的方式,根据用户权限加载不同的路由配置。可以使用…

vue实现树目录

vue实现树目录

Vue 实现树形目录 在 Vue 中实现树形目录可以通过递归组件或第三方库(如 element-ui 的 el-tree)完成。以下是两种常见实现方式: 递归组件实现 递归组件适合自定义程度高的树形…

不用vue实现spa

不用vue实现spa

使用原生 JavaScript 实现 SPA 通过监听 URL 变化动态加载内容,结合 history.pushState 或 hashchange 事件实现路由切换。 // 路由配置 const…