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

原生实现方案

基本轮播组件:

<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>

响应式处理

监听窗口变化:

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中:

vue项目实现图片轮播

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

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

相关文章

h5实现图片压缩

h5实现图片压缩

使用Canvas API压缩图片 HTML5的Canvas API可以通过调整画布尺寸和图像质量来实现图片压缩。创建一个Canvas元素,将图片绘制到Canvas上,再通过toDataURL或toBl…

Vue实现图片 放大

Vue实现图片 放大

Vue实现图片放大功能 使用v-viewer插件 安装v-viewer插件: npm install v-viewer 在main.js中引入并配置: import Viewer from 'v-…

js实现图片拖动

js实现图片拖动

实现图片拖动的步骤 使用JavaScript实现图片拖动功能,可以通过HTML5的拖放API结合事件监听实现。以下是具体方法: 设置HTML结构 为图片添加draggable属性,并设置唯一ID以便…

实现图片旋转js

实现图片旋转js

使用CSS transform属性旋转图片 通过CSS的transform属性可以轻松实现图片旋转。以下是一个简单的示例代码: const image = document.getElement…

js实现图片拖拽

js实现图片拖拽

实现图片拖拽的基本步骤 HTML 结构需要包含可拖拽的图片元素,并设置 draggable 属性为 true: <img id="dragImage" src="image.jpg" drag…

jquery项目

jquery项目

jQuery项目简介 jQuery是一个快速、简洁的JavaScript库,简化了HTML文档遍历、事件处理、动画和Ajax交互。尽管现代前端框架(如React、Vue)逐渐流行,jQuery仍广泛用…