当前位置:首页 > VUE

vue项目实现图片轮播

2026-01-23 11:43:59VUE

使用 Vue 实现图片轮播

基于 vue-awesome-swiper 实现

安装依赖:

npm install swiper vue-awesome-swiper

组件代码示例:

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(item, index) in imgList" :key="index">
      <img :src="item" class="swiper-img">
    </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 {
      imgList: [
        'image1.jpg',
        'image2.jpg',
        'image3.jpg'
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination'
        },
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true
      }
    }
  }
}
</script>

<style>
.swiper-img {
  width: 100%;
  height: auto;
}
</style>

原生实现方案

基本轮播组件:

vue项目实现图片轮播

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

<script>
export default {
  data() {
    return {
      images: ['img1.jpg', 'img2.jpg', 'img3.jpg'],
      currentIndex: 0,
      timer: null
    }
  },
  computed: {
    slideStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`
      }
    }
  },
  methods: {
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length
    },
    autoPlay() {
      this.timer = setInterval(this.next, 3000)
    }
  },
  mounted() {
    this.autoPlay()
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

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

实现功能增强

添加指示器:

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

<script>
export default {
  methods: {
    goTo(index) {
      this.currentIndex = index
    }
  }
}
</script>

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

响应式处理

监听窗口变化:

vue项目实现图片轮播

mounted() {
  window.addEventListener('resize', this.handleResize)
  this.handleResize()
},
methods: {
  handleResize() {
    this.carouselWidth = this.$el.offsetWidth
  }
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize)
}

性能优化

图片懒加载:

<img v-lazy="item" class="swiper-img">

需要安装并配置vue-lazyload:

npm install vue-lazyload

在main.js中:

import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload, {
  preLoad: 1.3,
  loading: 'loading.gif',
  attempt: 1
})

标签: 项目图片
分享给朋友:

相关文章

vue实现项目

vue实现项目

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 创建 Vue 项目 使用 Vue CLI 快…

uniapp前端项目

uniapp前端项目

Uniapp 前端项目开发指南 Uniapp 是一个基于 Vue.js 的跨平台开发框架,支持一次开发,多端发布(如微信小程序、H5、App 等)。以下是关于 Uniapp 前端项目的关键信息: 环…

H5图片实现地图

H5图片实现地图

实现H5图片地图的方法 在H5中实现图片地图功能,可以通过以下方式完成。图片地图允许用户在图片的特定区域点击,触发不同的交互效果。 使用HTML的<map>和<area>标签…

vue ui实现创建vue项目

vue ui实现创建vue项目

使用 Vue UI 创建 Vue 项目的步骤 Vue CLI 提供了一个图形化界面(Vue UI)来创建和管理 Vue 项目。以下是详细的操作步骤: 安装 Vue CLI 确保已安装 Node.js…

vue实现放大图片

vue实现放大图片

实现图片放大功能的方法 在Vue中实现图片放大功能,可以通过多种方式实现。以下是几种常见的方法: 使用CSS transform属性 通过CSS的transform: scale()属性实现图片放…

vue项目实现录音

vue项目实现录音

实现录音功能的基本思路 在Vue项目中实现录音功能通常需要借助浏览器提供的Web Audio API或第三方库。核心步骤包括获取麦克风权限、创建录音对象、处理音频数据以及保存录音文件。 使用Web…