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

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

相关文章

网页制作css图片切换

网页制作css图片切换

CSS图片切换的实现方法 使用CSS实现图片切换可以通过多种方式完成,以下是几种常见的方法: 纯CSS轮播图 通过CSS动画和关键帧实现自动轮播效果。定义一个容器,内部放置多个图片,利用ani…

css网页制作漂浮图片

css网页制作漂浮图片

使用CSS实现漂浮图片效果 通过CSS的animation属性和@keyframes规则可以轻松实现图片漂浮效果。以下是几种常见实现方式: 基础漂浮动画 通过关键帧定义图片的垂直移动,模拟漂浮感:…

vue实现拖动图片验证

vue实现拖动图片验证

Vue 实现拖动图片验证功能 核心思路 拖动图片验证通常需要用户将图片拖动到指定区域或按正确顺序排列。实现时需要处理拖拽事件、验证逻辑和视觉反馈。 实现步骤 1. 基础组件结构 <templ…

react项目如何

react项目如何

创建React项目 使用create-react-app工具快速初始化项目。确保已安装Node.js(建议版本≥12.0.0),运行以下命令生成新项目: npx create-react-app…

jquery图片

jquery图片

jQuery 图片操作 jQuery 提供了多种方法来处理图片,包括加载、显示、隐藏、动画效果等。以下是一些常见的 jQuery 图片操作方法: 动态加载图片 使用 jQuery 的 attr()…

uniapp项目教程

uniapp项目教程

uniapp项目教程 uniapp 是一个基于 Vue.js 的跨平台开发框架,支持一次开发,多端发布(包括 iOS、Android、H5、小程序等)。以下是详细的教程指南,帮助快速上手 uniapp…